/*
 * Copyright 2015-2016 Red Hat, Inc, and individual contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jboss.hal.testsuite.test.configuration.systemproperty;

import org.jboss.arquillian.core.api.annotation.Inject;
import org.jboss.arquillian.graphene.page.Page;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.hal.testsuite.Console;
import org.jboss.hal.testsuite.Random;
import org.jboss.hal.testsuite.creaper.ManagementClientProvider;
import org.jboss.hal.testsuite.creaper.ResourceVerifier;
import org.jboss.hal.testsuite.fragment.AddResourceDialogFragment;
import org.jboss.hal.testsuite.fragment.FormFragment;
import org.jboss.hal.testsuite.fragment.TableFragment;
import org.jboss.hal.testsuite.page.configuration.SystemPropertyPage;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.wildfly.extras.creaper.core.online.OnlineManagementClient;
import org.wildfly.extras.creaper.core.online.operations.Operations;
import org.wildfly.extras.creaper.core.online.operations.Values;

import static org.jboss.hal.dmr.ModelDescriptionConstants.NAME;
import static org.jboss.hal.dmr.ModelDescriptionConstants.VALUE;
import static org.jboss.hal.testsuite.test.configuration.systemproperty.SystemPropertyFixtures.*;
import static org.junit.Assert.assertEquals;

@RunWith(Arquillian.class)
public class SystemPropertyTest {

    private static final OnlineManagementClient client = ManagementClientProvider.createOnlineManagementClient();
    private static final Operations operations = new Operations(client);

    @BeforeClass
    public static void beforeClass() throws Exception {
        operations.add(systemPropertyAddress(READ_NAME), Values.empty().and(VALUE, READ_VALUE));
        operations.add(systemPropertyAddress(UPDATE_NAME), Values.empty().and(VALUE, UPDATE_VALUE));
        operations.add(systemPropertyAddress(DELETE_NAME), Values.empty().and(VALUE, DELETE_VALUE));
    }

    @AfterClass
    public static void tearDown() throws Exception {
        operations.removeIfExists(systemPropertyAddress(CREATE_NAME));
        operations.removeIfExists(systemPropertyAddress(READ_NAME));
        operations.removeIfExists(systemPropertyAddress(UPDATE_NAME));
        operations.removeIfExists(systemPropertyAddress(DELETE_NAME));
    }

    @Page private SystemPropertyPage page;
    @Inject private Console console;
    private TableFragment table;
    private FormFragment form;

    @Before
    public void setUp() throws Exception {
        page.navigate();

        form = page.getForm();
        table = page.getTable();
        table.bind(form);
    }

    @Test
    public void create() throws Exception {
        AddResourceDialogFragment dialog = table.add();
        dialog.getForm().text(NAME, CREATE_NAME);
        dialog.getForm().text(VALUE, CREATE_VALUE);
        dialog.add();

        console.verifySuccess();
        new ResourceVerifier(systemPropertyAddress(CREATE_NAME), client)
                .verifyExists();
    }

    @Test
    public void read() throws Exception {
        table.select(READ_NAME);
        assertEquals(READ_VALUE, form.value(VALUE));
    }

    @Test
    public void update() throws Exception {
        String value = Random.name();
        table.select(UPDATE_NAME);
        form.edit();
        form.text(VALUE, value);
        form.save();

        console.verifySuccess();
        new ResourceVerifier(systemPropertyAddress(UPDATE_NAME), client)
                .verifyAttribute(VALUE, value);

    }

    @Test
    public void delete() throws Exception {
        table.remove(DELETE_NAME);

        console.verifySuccess();
        new ResourceVerifier(systemPropertyAddress(DELETE_NAME), client).verifyDoesNotExist();
    }
}

@@ -878,16 +878,63 @@
 onsole;%0A
+import org.jboss.hal.testsuite.CrudOperations;%0A
 import o
@@ -1033,132 +1033,8 @@
 er;%0A
-import org.jboss.hal.testsuite.creaper.ResourceVerifier;%0Aimport org.jboss.hal.testsuite.fragment.AddResourceDialogFragment;%0A
 impo
@@ -2818,20 +2818,22 @@
 %7D%0A%0A    @
-Page
+Inject
  private
@@ -2837,30 +2837,22 @@
 ate 
-SystemPropertyPage pag
+Console consol
 e;%0A 
@@ -2867,37 +2867,84 @@
 ct private C
-onsole consol
+rudOperations crud;%0A    @Page private SystemPropertyPage pag
 e;%0A    priva
@@ -3243,80 +3243,89 @@
     
-AddResourceDialogFragment dialog = table.add();%0A        dialog.getF
+crud.create(systemPropertyAddress(CREATE_NAME), table, form -%3E %7B%0A            f
 orm
-()
 .tex
@@ -3358,24 +3358,16 @@
     
-dialog.getF
+    f
 orm
-()
 .tex
@@ -3402,159 +3402,13 @@
     
-dialog.add();%0A%0A        console.verifySuccess();%0A        new ResourceVerifier(systemPropertyAddress(CREATE_NAME), client)%0A                .verifyExists(
+    %7D
 );%0A 
@@ -3445,33 +3445,16 @@
 d read()
- throws Exception
  %7B%0A     
@@ -3607,304 +3607,120 @@
     
-String value = Random.name();%0A        table.select(UPDATE_NAME);%0A        form.edit();%0A        form.text(VALUE, value);%0A        form.save();%0A%0A        console.verifySuccess();%0A        new ResourceVerifier(systemPropertyAddress(UPDATE_NAME), client)%0A                .verifyAttribute(VALUE, value
+table.select(UPDATE_NAME);%0A        crud.update(systemPropertyAddress(UPDATE_NAME), form, VALUE, Random.name()
 );%0A
-%0A
     
@@ -3788,97 +3788,19 @@
     
-table.remove(DELETE_NAME);%0A%0A        console.verifySuccess();%0A        new ResourceVerifier
+crud.delete
 (sys
@@ -3836,35 +3836,26 @@
 E), 
-client).verifyDoesNotExist(
+table, DELETE_NAME
 );%0A 

Use CrudOperations in system properties tests