package org.jboss.hal.testsuite.test.configuration.infinispan;

import org.apache.commons.lang3.RandomStringUtils;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.hal.testsuite.category.Shared;
import org.jboss.hal.testsuite.creaper.ResourceVerifier;
import org.jboss.hal.testsuite.fragment.config.infinispan.CacheWizard;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.runner.RunWith;
import org.wildfly.extras.creaper.core.online.operations.OperationException;
import org.wildfly.extras.creaper.core.online.operations.Values;

import java.io.IOException;

/**
 * @author mkrajcov <mkrajcov@redhat.com>
 */
@RunWith(Arquillian.class)
@Category(Shared.class)
public class LocalCacheTestCase extends AbstractCacheTestCase {

    @Override
    @Test
    public void createCache() throws Exception {
        String name = "cn_" + RandomStringUtils.randomAlphanumeric(5);
        CacheWizard wizard = page.content().addCache();

        boolean result = wizard.name(name)
                .finish();

        Assert.assertTrue("Window should be closed", result);
        administration.reloadIfRequired();
        new ResourceVerifier(cacheAddress, client).verifyExists();

        page.content().getResourceManager().removeResourceAndConfirm(name);
        administration.reloadIfRequired();
        new ResourceVerifier(cacheAddress, client).verifyDoesNotExist();
    }

    @Override
    protected CacheType getCacheType() {
        return CacheType.LOCAL;
    }

    @Before
    public void before_() {
        page.local();
        page.selectCache(cacheName);
    }

    public void addCache() throws IOException {
        operations.add(cacheAddress, Values.of("mode", "SYNC"));
        operations.add(transactionAddress);
        operations.add(storeAddress, Values.of("class", "org.infinispan.configuration.cache.SingleFileStoreConfigurationBuilder"));
        operations.add(lockingAddress);
    }

    public void deleteCache() throws IOException, OperationException {
        operations.removeIfExists(cacheAddress);
    }
}


@@ -487,16 +487,82 @@
 unWith;%0A
+import org.wildfly.extras.creaper.core.online.operations.Address;%0A
 import o
@@ -1035,16 +1035,120 @@
 ric(5);%0A
+        Address localCacheAddress = ABSTRACT_CACHE_ADDRESS.and(getCacheType().getAddressName(), name);%0A%0A
         
@@ -1192,24 +1192,42 @@
 ddCache();%0A%0A
+        try %7B%0A    
         bool
@@ -1273,16 +1273,20 @@
         
+   
+ 
 .finish(
@@ -1285,24 +1285,28 @@
 .finish();%0A%0A
+    
         Asse
@@ -1355,32 +1355,36 @@
 esult);%0A        
+   
+ 
 administration.r
@@ -1402,32 +1402,36 @@
 ired();%0A        
+    
 new ResourceVeri
@@ -1427,33 +1427,38 @@
 esourceVerifier(
-c
+localC
 acheAddress, cli
@@ -1475,24 +1475,28 @@
 yExists();%0A%0A
+    
         page
@@ -1559,32 +1559,36 @@
 (name);%0A        
+  
+  
 administration.r
@@ -1606,32 +1606,36 @@
 ired();%0A        
+    
 new ResourceVeri
@@ -1639,17 +1639,22 @@
 erifier(
-c
+localC
 acheAddr
@@ -1687,16 +1687,105 @@
 Exist();
+%0A%0A        %7D finally %7B%0A            operations.removeIfExists(localCacheAddress);%0A        %7D
 %0A    %7D%0A%0A

use the right cache.