package io.amaze.bench.orchestrator.registry;

import com.google.common.testing.NullPointerTester;
import io.amaze.bench.client.runtime.agent.AgentRegistrationMessage;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.Set;

import static com.google.common.truth.Truth.assertThat;
import static io.amaze.bench.client.runtime.agent.AgentTest.DUMMY_AGENT;
import static org.mockito.Mockito.*;

/**
 * Created on 4/2/16.
 *
 * @author Florent Weber (florent.weber@gmail.com)
 */
public final class AgentRegistryTest {

    private static final AgentRegistrationMessage REG_MSG = AgentRegistrationMessage.create(DUMMY_AGENT);

    private AgentRegistry registry;
    private AgentRegistryListener orchestratorListener;
    private AgentRegistryListener clientListener;

    @Before
    public void before() {
        registry = new AgentRegistry();
        orchestratorListener = registry.getListenerForOrchestrator();

        clientListener = mock(AgentRegistryListener.class);
        registry.addListener(clientListener);
    }

    @After
    public void after() {
        registry.removeListener(clientListener);
    }

    @Test
    public void null_parameters_invalid() {
        NullPointerTester tester = new NullPointerTester();
        tester.testAllPublicConstructors(AgentRegistry.class);
        tester.testAllPublicInstanceMethods(registry);
    }

    @Test
    public void agent_registered() {
        orchestratorListener.onAgentRegistration(REG_MSG);

        RegisteredAgent agent = registry.byName(DUMMY_AGENT);
        assertThat(agent.getName()).is(DUMMY_AGENT);
        assertThat(agent.getSystemInfo()).is(REG_MSG.getSystemInfo());
        assertThat(agent.getCreationTime()).is(REG_MSG.getCreationTime());

        verify(clientListener).onAgentRegistration(REG_MSG);
        verifyNoMoreInteractions(clientListener);
    }

    @Test
    public void agent_registered_signs_off() {
        orchestratorListener.onAgentRegistration(REG_MSG);
        orchestratorListener.onAgentSignOff(DUMMY_AGENT);

        RegisteredAgent agent = registry.byName(DUMMY_AGENT);
        assertThat(agent).isNull();

        verify(clientListener).onAgentRegistration(REG_MSG);
        verify(clientListener).onAgentSignOff(DUMMY_AGENT);
        verifyNoMoreInteractions(clientListener);
    }

    @Test
    public void unknown_agent_signs_off() {
        orchestratorListener.onAgentSignOff(DUMMY_AGENT);

        RegisteredAgent agent = registry.byName(DUMMY_AGENT);
        assertThat(agent).isNull();

        verifyNoMoreInteractions(clientListener);
    }

    @Test
    public void list_all() {
        orchestratorListener.onAgentRegistration(REG_MSG);

        Set<RegisteredAgent> agents = registry.all();

        assertThat(agents.size()).is(1);

        RegisteredAgent agent = agents.iterator().next();
        assertThat(agent.getName()).is(DUMMY_AGENT);
    }

    @Test
    public void removed_listener_is_not_notified() {
        registry.removeListener(clientListener);

        orchestratorListener.onAgentRegistration(REG_MSG);

        verifyNoMoreInteractions(clientListener);
    }

    @Test
    public void remove_listener_twice_does_not_throw() {
        registry.removeListener(clientListener);
        registry.removeListener(clientListener);
    }
}
@@ -261,64 +261,8 @@
 t;%0A%0A
-import static com.google.common.truth.Truth.assertThat;%0A
 impo
@@ -330,16 +330,141 @@
 _AGENT;%0A
+import static org.hamcrest.core.Is.is;%0Aimport static org.junit.Assert.assertNull;%0Aimport static org.junit.Assert.assertThat;%0A
 import s
@@ -1657,34 +1657,34 @@
 (agent.getName()
-).
+, 
 is(DUMMY_AGENT);
@@ -1674,32 +1674,33 @@
  is(DUMMY_AGENT)
+)
 ;%0A        assert
@@ -1725,18 +1725,18 @@
 emInfo()
-).
+, 
 is(REG_M
@@ -1754,16 +1754,17 @@
 mInfo())
+)
 ;%0A      
@@ -1799,18 +1799,18 @@
 onTime()
-).
+, 
 is(REG_M
@@ -1830,16 +1830,17 @@
 nTime())
+)
 ;%0A%0A     
@@ -2203,36 +2203,27 @@
   assert
-That
+Null
 (agent)
-.isNull()
 ;%0A%0A     
@@ -2588,28 +2588,19 @@
 sert
-That
+Null
 (agent)
-.isNull()
 ;%0A%0A 
@@ -2845,15 +2845,16 @@
 ze()
-).
+, 
 is(1)
+)
 ;%0A%0A 
@@ -2944,18 +2944,18 @@
 etName()
-).
+, 
 is(DUMMY
@@ -2957,24 +2957,25 @@
 DUMMY_AGENT)
+)
 ;%0A    %7D%0A%0A   

Remove dependency on Truth