package org.amc.game.chessserver;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.*;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestBuilders.*;
import static org.mockito.Mockito.*;
import static org.junit.Assert.*;

import org.junit.*;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.test.context.support.WithUserDetails;
import org.springframework.security.web.session.HttpSessionCreatedEvent;
import org.springframework.security.web.session.HttpSessionDestroyedEvent;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.http.HttpSession;

/**
 * Not working correctly. WebListener HttpSessionEventPublisher
 * Not registry so User list can't checked
 * @author Adrian Mclaughlin
 *
 */

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(locations={"/SpringTestConfig.xml", "/UserLogging.xml", "/GameServerSecurity.xml", "/GameServerWebSockets.xml"})
@WithUserDetails("adrian")
public class OnlinePlayerListControllerIntegrationTest {

    @Autowired
    private WebApplicationContext wac;
    
    private MockMvc mockMvc;
 
    @Before
    public void setUp() throws Exception {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(wac)
                        .apply(springSecurity())
                        .build();
    }

    @After
    public void tearDown() throws Exception {
    }

    
    @Ignore
    @Test
    public void test() throws Exception{
        MvcResult result = this.mockMvc.perform(get("/onlinePlayerList")).andExpect(status().isOk())
                        .andExpect(request().asyncStarted()).andReturn();
        
        MvcResult result2 = this.mockMvc.perform(asyncDispatch(result)).andExpect(status().isOk())
                        .andReturn();
        System.out.println("--------------->" + result2.getAsyncResult(500000));
        
    }
    
    @Test
    public void UserLogsInOutTest() throws Exception {
        this.mockMvc.perform(formLogin("/login").user("nobby").password("cr2032ux")).andExpect(status().is3xxRedirection());
        this.mockMvc.perform(logout());
        
        HttpSession session = mock(HttpSession.class);
        when(session.getId()).thenReturn("1");
        
        wac.publishEvent(new HttpSessionCreatedEvent(session));
        wac.publishEvent(new HttpSessionDestroyedEvent(session));
        
        MockPlayerListMessager messager = (MockPlayerListMessager)wac.getBean("mockPlayerListMessager");
        ArgumentCaptor<String> destinationCaptor = ArgumentCaptor.forClass(String.class);
        ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass(String.class);
        verify(messager.getSimpMessagingTemplate(),times(2)).convertAndSend(destinationCaptor.capture(),messageCaptor.capture());
        assertEquals(destinationCaptor.getValue(), OnlinePlayerListMessager.MESSAGE_DESTINATION);
    }
}

@@ -28,16 +28,103 @@
 erver;%0A%0A
+import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;%0A
 import s
@@ -930,67 +930,8 @@
 nt;%0A
-import org.springframework.test.annotation.DirtiesContext;%0A
 impo
@@ -2203,19 +2203,8 @@
    %0A
-    @Ignore
 %0A   
@@ -2324,16 +2324,56 @@
 rList%22))
+%0A                        .andDo(print())
 .andExpe
@@ -2466,24 +2466,16 @@
 turn();%0A
-        
 %0A       
@@ -2479,217 +2479,178 @@
     
-MvcResult result2 = this.mockMvc.perform(asyncDispatch(result)).andExpect(status().isOk())%0A                        .andReturn();%0A        System.out.println(%22---------------%3E%22 + result2.getAsyncResult(500000));
+String userListString = String.valueOf(result.getAsyncResult(5000));%0A        assertNotNull(userListString);%0A        assertTrue(userListString.contains(%22nobby%22));%0A        
 %0A   

fix test case now operational