package org.leialearns.logic.session;

import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.leialearns.logic.interaction.Alphabet;
import org.leialearns.logic.interaction.InteractionContext;
import org.leialearns.logic.interaction.Symbol;
import org.leialearns.utilities.ExecutionListener;
import org.leialearns.utilities.TestUtilities;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;

import java.io.IOException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.leialearns.utilities.Display.display;
import static org.leialearns.utilities.Static.equal;
import static org.leialearns.utilities.Static.getLoggingClass;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/ApplicationContext.xml","/org/leialearns/AppTest-context.xml"})
@TestExecutionListeners(value = {DependencyInjectionTestExecutionListener.class, ExecutionListener.class})
public class SessionTest {
    private final Logger logger = LoggerFactory.getLogger(getLoggingClass(this));

    @Autowired
    private Root root;

    @BeforeClass
    public static void beforeClass() throws IOException {
        TestUtilities.beforeClass(null);
    }

    @Test
    public void testRoot() {
        InteractionContext interactionContext = root.createInteractionContext("http://leialearns.org/test");
        assertNotNull("Interaction context", interactionContext);
        assertNotNull("Actions", interactionContext.getActions());
        assertNotNull("Responses", interactionContext.getResponses());
        assertNotNull("Structure", interactionContext.getStructure());

        Alphabet.Iterable alphabetIterable = root.findAlphabets();
        assertFalse("Empty alphabetDAO.findAll()", alphabetIterable.isEmpty());
        for (Object object : alphabetIterable) {
            assertTrue("Expected alphabet: " + display(object), object instanceof Alphabet);
        }

        Alphabet actions = interactionContext.getActions();
        logger.debug("Actions: " + actions.toString());

        actions.internalize("");
        actions.internalize("left");
        Symbol right = actions.internalize("right");
        logger.debug("Symbol 'right': [" + right + "] in: [" + right.getAlphabet() + "]");

        Alphabet responses = interactionContext.getResponses();
        logger.debug("Responses: " + responses.toString());

        responses.internalize("dark");
        Symbol light = responses.internalize("light");
        logger.debug("Symbol 'light': [" + light + "] in: [" + light.getAlphabet() + "]");

        assertEquals("http://leialearns.org/test/structure", interactionContext.getStructure().getURI());
    }

    @Test
    public void testSessions() {
        Session rootSession = root.createSession("http://leialearns.org/test/sessions");
        InteractionContext interactionContext = root.createInteractionContext("http://leialearns.org/test/sessions");
        Session newSession = root.createSession(interactionContext);
        assertFalse("Sessions should be different: " + display(rootSession) + ": " + display(newSession), equal(rootSession, newSession));
        assertEquals(rootSession.getInteractionContext(), newSession.getInteractionContext());
    }

}

@@ -374,16 +374,67 @@
 lities;%0A
+import org.leialearns.utilities.TransactionHelper;%0A
 import o
@@ -1568,24 +1568,89 @@
 ss(this));%0A%0A
+    @Autowired%0A    private TransactionHelper transactionHelper;%0A%0A
     @Autowir
@@ -1834,24 +1834,146 @@
 estRoot() %7B%0A
+        transactionHelper.runInTransaction(new Runnable() %7B%0A            @Override%0A            public void run() %7B%0A        
         Inte
@@ -2069,32 +2069,40 @@
 test%22);%0A        
+       
+ 
 assertNotNull(%22I
@@ -2143,32 +2143,40 @@
 ntext);%0A        
+    
+    
 assertNotNull(%22A
@@ -2218,32 +2218,40 @@
 ons());%0A        
+ 
+       
 assertNotNull(%22R
@@ -2297,32 +2297,40 @@
 ses());%0A        
+   
+     
 assertNotNull(%22S
@@ -2377,32 +2377,40 @@
 re());%0A%0A        
+    
+    
 Alphabet.Iterabl
@@ -2448,24 +2448,32 @@
 lphabets();%0A
+        
         asse
@@ -2536,24 +2536,32 @@
 isEmpty());%0A
+        
         for 
@@ -2605,24 +2605,32 @@
             
+        
 assertTrue(%22
@@ -2710,19 +2710,31 @@
     
-%7D%0A%0A
+        %7D%0A%0A    
         
 Alph
@@ -2721,32 +2721,36 @@
  %7D%0A%0A            
+    
 Alphabet actions
@@ -2785,32 +2785,40 @@
 ions();%0A        
+      
+  
 logger.debug(%22Ac
@@ -2842,32 +2842,40 @@
 s.toString());%0A%0A
+        
         actions.
@@ -2887,24 +2887,32 @@
 nalize(%22%22);%0A
+        
         acti
@@ -2932,24 +2932,32 @@
 ze(%22left%22);%0A
+        
         Symb
@@ -2997,32 +2997,40 @@
 ight%22);%0A        
+   
+     
 logger.debug(%22Sy
@@ -3089,32 +3089,40 @@
 abet() + %22%5D%22);%0A%0A
+        
         Alphabet
@@ -3169,32 +3169,40 @@
 nses();%0A        
+       
+ 
 logger.debug(%22Re
@@ -3230,32 +3230,40 @@
 s.toString());%0A%0A
+        
         response
@@ -3289,24 +3289,32 @@
 %22);%0A        
+ 
+       
 Symbol light
@@ -3348,32 +3348,40 @@
 ight%22);%0A        
+   
+     
 logger.debug(%22Sy
@@ -3448,32 +3448,40 @@
  %22%5D%22);%0A%0A        
+     
+   
 assertEquals(%22ht
@@ -3558,24 +3558,50 @@
 .getURI());%0A
+            %7D%0A        %7D);%0A
     %7D%0A%0A    @

Fix to make SessionTest pass again. Wrapped the body of method SessionTest#testRoot() in TransactionHelper#runInTransaction(Runnable).