package com.mudalov.safe.impl;


import com.mudalov.safe.cache.TestCacheFactory;
import org.junit.Assert;
import org.junit.Test;

import java.util.Date;

public class CachedCommandExecutionTest {

    @Test
    public void testExecute() {
        AbstractCachedCommand<Date> getDate = new AbstractCachedCommand<Date>() {
            @Override
            public String getKey() {
                return "sum";
            }

            @Override
            public Date action() throws Exception {
                return new Date();
            }
        };
        // success, cache updated
        Date result = SafeCommands.create(getDate).execute();
        AbstractCachedCommand<Date> failingCommand = new AbstractCachedCommand<Date>() {
            @Override
            public String getKey() {
                return "sum";
            }

            @Override
            public Date action() throws Exception {
                throw new Exception();
            }
        };
        Date failedServiceResult = SafeCommands.create(failingCommand).execute();
        Assert.assertEquals("Expect cached result", result, failedServiceResult);
    }

    @Test
    public void testCustomCache() {
        AbstractCachedCommand<String> helloWorld = new AbstractCachedCommand<String>() {
            @Override
            public String getKey() {
                return "key";
            }

            @Override
            public String action() throws Exception {
                return "Hello, world!";
            }
        };
        // success, cache updated
        SafeCommands.create(helloWorld, "customCache").execute();
        AbstractCachedCommand<String> failingCommand = new AbstractCachedCommand<String>() {
            @Override
            public String getKey() {
                return "key";
            }

            @Override
            public String action() throws Exception {
                throw new Exception();
            }
        };
        String failedServiceResult = SafeCommands.create(failingCommand, "customCache").execute();
        Assert.assertEquals("Expect result from Test Cache", TestCacheFactory.CacheValue, failedServiceResult);
    }

}

@@ -2185,10 +2185,1893 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void testSeparateCacheRegions() %7B%0A        AbstractCachedCommand%3CString%3E helloWorld = new AbstractCachedCommand%3CString%3E() %7B%0A            @Override%0A            public String getKey() %7B%0A                return %22key%22;%0A            %7D%0A%0A            @Override%0A            public String action() throws Exception %7B%0A                return %22Hello, world!%22;%0A            %7D%0A        %7D;%0A        AbstractCachedCommand%3CString%3E helloWorld2 = new AbstractCachedCommand%3CString%3E() %7B%0A            @Override%0A            public String getKey() %7B%0A                return %22key%22;%0A            %7D%0A%0A            @Override%0A            public String action() throws Exception %7B%0A                return %22Hello, world!!%22;%0A            %7D%0A        %7D;%0A        // success, cache updated%0A        SafeCommands.create(helloWorld, %22group1%22).execute();%0A        SafeCommands.create(helloWorld2, %22group2%22).execute();%0A        AbstractCachedCommand%3CString%3E failingCommand = new AbstractCachedCommand%3CString%3E() %7B%0A            @Override%0A            public String getKey() %7B%0A                return %22key%22;%0A            %7D%0A%0A            @Override%0A            public String action() throws Exception %7B%0A                throw new Exception();%0A            %7D%0A        %7D;%0A        AbstractCachedCommand%3CString%3E failingCommand2 = new AbstractCachedCommand%3CString%3E() %7B%0A            @Override%0A            public String getKey() %7B%0A                return %22key%22;%0A            %7D%0A%0A            @Override%0A            public String action() throws Exception %7B%0A                throw new Exception();%0A            %7D%0A        %7D;%0A        String failedServiceResult = SafeCommands.create(failingCommand, %22group1%22).execute();%0A        Assert.assertEquals(%22Hello, world!%22, failedServiceResult);%0A        failedServiceResult = SafeCommands.create(failingCommand2, %22group2%22).execute();%0A        Assert.assertEquals(%22Hello, world!!%22, failedServiceResult);%0A%0A    %7D%0A%0A%0A
 %7D%0A

Add test for separate cache regions name conflict