// Path: airomem-core/src/main/java/pl/setblack/airomem/core/PrevalanceContext.java
// public class PrevalanceContext {
// 
//     /**
//      * Use this time object whenewer actual time was needed in command.
//      */
//     public final Instant time;
// 
//     public final boolean safe;
// 
//     public PrevalanceContext(final Date date) {
//         this(date.toInstant(), false);
//     }
// 
//     private PrevalanceContext(final Instant time, final boolean safe) {
//         this.time = time;
//         this.safe = safe;
//     }
// 
//     public PrevalanceContext safe() {
//         return new PrevalanceContext(this.time, true);
//     }
// 
//     public PrevalanceContext unsafe() {
//         return new PrevalanceContext(this.time, false);
//     }
// }
// 
// Path: airomem-core/src/test/java/pl/setblack/airomem/core/StorableObject.java
// public class StorableObject implements Serializable {
// 
//     public final HashMap<String, String> internalMap;
// 
//     private transient Map<String, String> immutable;
// 
//     public StorableObject(HashMap<String, String> intenralMap) {
//         this.internalMap = intenralMap;
//         this.immutable = Collections.unmodifiableMap(intenralMap);
//     }
// 
// 
//     public synchronized Map<String, String> getImmutable() {
//         if (this.immutable == null) {
//             this.immutable = Collections.unmodifiableMap(internalMap);
//         }
//         return immutable;
//     }
// 
//     public static HashMap createTestHashMap() {
//         final HashMap<String, String> result = new HashMap<>();
//         for (int i = 0; i < 10; i++) {
//             result.put("key:" + i, "val:" + i);
//         }
//         return result;
//     }
// 
//     public static StorableObject createTestObject() {
//         return new StorableObject(createTestHashMap());
//     }
// }
// 
// Path: airomem-core/src/main/java/pl/setblack/airomem/core/VoidContextCommand.java
// public interface VoidContextCommand<T> extends ContextCommand<T, Void> {
// 
//     @Override
//     default Void execute(T system, PrevalanceContext context) {
//         this.executeVoid(system, context);
//         return null;
//     }
// 
//     void executeVoid(T system, PrevalanceContext context);
// 
// }
// 
// Path: airomem-core/src/main/java/pl/setblack/airomem/core/WriteChecker.java
// public class WriteChecker {
// 
//     private static final ThreadLocal<PrevalanceContext> CONTEXT_STORE = new ThreadLocal<>();
// 
//     public static void setContext(final PrevalanceContext ctx) {
//         assert !hasPrevalanceContext() : "Context was initialized before";
//         CONTEXT_STORE.set(ctx);
//     }
// 
//     public static PrevalanceContext getContext() {
//         return CONTEXT_STORE.get();
//     }
// 
//     public static void clearContext() {
//         assert hasPrevalanceContext() : "Context has to be initialized first";
//         CONTEXT_STORE.remove();
//     }
// 
//     public static boolean hasPrevalanceContext() {
//         return CONTEXT_STORE.get() != null;
//     }
// 
//     public static void enterSafe() {
//         assert hasPrevalanceContext() : "Context has to be initialized first";
//         assert !getContext().safe : "context cannot be safe before";
//         CONTEXT_STORE.set(getContext().safe());
//     }
// 
//     public static void leaveSafe() {
//         assert hasPrevalanceContext() : "Context has to be initialized first";
//         assert getContext().safe : "context must be safe before";
// 
//         CONTEXT_STORE.set(getContext().unsafe());
//     }
// 
// }


import org.junit.Test;
import pl.setblack.airomem.core.PrevalanceContext;
import pl.setblack.airomem.core.StorableObject;
import pl.setblack.airomem.core.VoidContextCommand;
import pl.setblack.airomem.core.WriteChecker;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneOffset;
import java.util.Date;
import static org.junit.Assert.*;
    @Test
    public void testWriteCheckerContextClearedWhenExceptionOccured() throws Exception {
        final LocalDateTime time = LocalDateTime.of(1977, Month.MAY, 20, 1, 1);
        final Date date = Date.from(time.toInstant(ZoneOffset.UTC));
        final RuntimeException exception = new RuntimeException("fail");
        final VoidContextCommand<StorableObject> myCmd = (x, ctx) -> {
            assertTrue(WriteChecker.hasPrevalanceContext());
            assertEquals(ctx, WriteChecker.getContext());
            throw exception;
        };
        InternalTransaction instance = new InternalTransaction(myCmd);
        final RoyalFoodTester<StorableObject> testSystem = RoyalFoodTester.of(StorableObject.createTestObject());
        try {
            instance.executeAndQuery(testSystem, date);
        } catch (Exception re) {
            assertEquals(exception, re);
        }
        assertNull(WriteChecker.getContext());
    }

    /**
     * Test of createContext method, of class InternalTransaction.
     */
    @Test
    public void testCreateContext() {
        LocalDateTime time = LocalDateTime.of(1977, Month.MAY, 20, 1, 1);
        final Date date = Date.from(time.toInstant(ZoneOffset.UTC));
        VoidContextCommand<StorableObject> myCmd = (x, ctx) -> x.internalMap.put("date", ctx.time.toString());
        InternalTransaction instance = new InternalTransaction(myCmd);

// Path: airomem-core/src/main/java/pl/setblack/airomem/core/PrevalanceContext.java
// public class PrevalanceContext {
// 
//     /**
//      * Use this time object whenewer actual time was needed in command.
//      */
//     public final Instant time;
// 
//     public final boolean safe;
// 
//     public PrevalanceContext(final Date date) {
//         this(date.toInstant(), false);
//     }
// 
//     private PrevalanceContext(final Instant time, final boolean safe) {
//         this.time = time;
//         this.safe = safe;
//     }
// 
//     public PrevalanceContext safe() {
//         return new PrevalanceContext(this.time, true);
//     }
// 
//     public PrevalanceContext unsafe() {
//         return new PrevalanceContext(this.time, false);
//     }
// }
// 
// Path: airomem-core/src/test/java/pl/setblack/airomem/core/StorableObject.java
// public class StorableObject implements Serializable {
// 
//     public final HashMap<String, String> internalMap;
// 
//     private transient Map<String, String> immutable;
// 
//     public StorableObject(HashMap<String, String> intenralMap) {
//         this.internalMap = intenralMap;
//         this.immutable = Collections.unmodifiableMap(intenralMap);
//     }
// 
// 
//     public synchronized Map<String, String> getImmutable() {
//         if (this.immutable == null) {
//             this.immutable = Collections.unmodifiableMap(internalMap);
//         }
//         return immutable;
//     }
// 
//     public static HashMap createTestHashMap() {
//         final HashMap<String, String> result = new HashMap<>();
//         for (int i = 0; i < 10; i++) {
//             result.put("key:" + i, "val:" + i);
//         }
//         return result;
//     }
// 
//     public static StorableObject createTestObject() {
//         return new StorableObject(createTestHashMap());
//     }
// }
// 
// Path: airomem-core/src/main/java/pl/setblack/airomem/core/VoidContextCommand.java
// public interface VoidContextCommand<T> extends ContextCommand<T, Void> {
// 
//     @Override
//     default Void execute(T system, PrevalanceContext context) {
//         this.executeVoid(system, context);
//         return null;
//     }
// 
//     void executeVoid(T system, PrevalanceContext context);
// 
// }
// 
// Path: airomem-core/src/main/java/pl/setblack/airomem/core/WriteChecker.java
// public class WriteChecker {
// 
//     private static final ThreadLocal<PrevalanceContext> CONTEXT_STORE = new ThreadLocal<>();
// 
//     public static void setContext(final PrevalanceContext ctx) {
//         assert !hasPrevalanceContext() : "Context was initialized before";
//         CONTEXT_STORE.set(ctx);
//     }
// 
//     public static PrevalanceContext getContext() {
//         return CONTEXT_STORE.get();
//     }
// 
//     public static void clearContext() {
//         assert hasPrevalanceContext() : "Context has to be initialized first";
//         CONTEXT_STORE.remove();
//     }
// 
//     public static boolean hasPrevalanceContext() {
//         return CONTEXT_STORE.get() != null;
//     }
// 
//     public static void enterSafe() {
//         assert hasPrevalanceContext() : "Context has to be initialized first";
//         assert !getContext().safe : "context cannot be safe before";
//         CONTEXT_STORE.set(getContext().safe());
//     }
// 
//     public static void leaveSafe() {
//         assert hasPrevalanceContext() : "Context has to be initialized first";
//         assert getContext().safe : "context must be safe before";
// 
//         CONTEXT_STORE.set(getContext().unsafe());
//     }
// 
// }


// Path: airomem-core/src/test/java/pl/setblack/airomem/core/impl/InternalTransactionTest.java
import org.junit.Test;
import pl.setblack.airomem.core.PrevalanceContext;
import pl.setblack.airomem.core.StorableObject;
import pl.setblack.airomem.core.VoidContextCommand;
import pl.setblack.airomem.core.WriteChecker;
import java.time.LocalDateTime;
import java.time.Month;
import java.time.ZoneOffset;
import java.util.Date;
import static org.junit.Assert.*;

    @Test
    public void testWriteCheckerContextClearedWhenExceptionOccured() throws Exception {
        final LocalDateTime time = LocalDateTime.of(1977, Month.MAY, 20, 1, 1);
        final Date date = Date.from(time.toInstant(ZoneOffset.UTC));
        final RuntimeException exception = new RuntimeException("fail");
        final VoidContextCommand<StorableObject> myCmd = (x, ctx) -> {
            assertTrue(WriteChecker.hasPrevalanceContext());
            assertEquals(ctx, WriteChecker.getContext());
            throw exception;
        };
        InternalTransaction instance = new InternalTransaction(myCmd);
        final RoyalFoodTester<StorableObject> testSystem = RoyalFoodTester.of(StorableObject.createTestObject());
        try {
            instance.executeAndQuery(testSystem, date);
        } catch (Exception re) {
            assertEquals(exception, re);
        }
        assertNull(WriteChecker.getContext());
    }

    /**
     * Test of createContext method, of class InternalTransaction.
     */
    @Test
    public void testCreateContext() {
        LocalDateTime time = LocalDateTime.of(1977, Month.MAY, 20, 1, 1);
        final Date date = Date.from(time.toInstant(ZoneOffset.UTC));
        VoidContextCommand<StorableObject> myCmd = (x, ctx) -> x.internalMap.put("date", ctx.time.toString());
        InternalTransaction instance = new InternalTransaction(myCmd);

        PrevalanceContext result = instance.createContext(date);