package no.ntnu.okse.protocol.amqp091;

import no.ntnu.okse.core.messaging.Message;
import no.ntnu.okse.core.subscription.SubscriptionService;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import static org.mockito.Mockito.*;
import static org.testng.AssertJUnit.*;

@Test
public class AMQP091ProtocolServerTest {
    private AMQP091ProtocolServer protocolServer;
    @Mock
    private AMQP091Service amqpService;
    @Mock
    private SubscriptionService subscriptionService;

    @BeforeMethod
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        protocolServer = spy(new AMQP091ProtocolServer("localhost", 32121));
        protocolServer.setAmqpService(amqpService);
        protocolServer.setSubscriptionService(subscriptionService);
    }

    @AfterMethod
    public void tearDown() {
        protocolServer = null;
    }

    public void run() {
        protocolServer.run();
        verify(amqpService).start();
    }

    public void boot() {
        protocolServer.setAmqpService(null);
        doNothing().when(protocolServer).run();
        assertFalse(protocolServer.isRunning());
        protocolServer.boot();
        assertTrue(protocolServer.isRunning());
    }

    public void run_stop() {
        protocolServer.run();
        verify(amqpService).start();
        protocolServer.stopServer();
        verify(amqpService).stop();
    }

    public void stop_notStarted() {
        protocolServer.setAmqpService(null);
        protocolServer.stopServer();
        verify(amqpService, never()).stop();
    }

    public void getProtocolServerType() {
        assertEquals(protocolServer.getProtocolServerType(), "amqp091");
    }

    public void sendMessage_noSubscribers() {
        Message message = new Message("Message body", "topic", null, protocolServer.getProtocolServerType());
        protocolServer.sendMessage(message);
        verify(protocolServer, never()).incrementTotalMessagesSent();
    }

    public void sendMessage_Subscribers() {
        Message message = new Message("Message body", "topic", null, protocolServer.getProtocolServerType());
        protocolServer.sendMessage(message);
        verify(protocolServer, never()).incrementTotalMessagesSent();
    }
}

@@ -77,16 +77,66 @@
 essage;%0A
+import no.ntnu.okse.core.subscription.Subscriber;%0A
 import n
@@ -374,16 +374,94 @@
 .Test;%0A%0A
+import java.util.ArrayList;%0Aimport java.util.HashSet;%0Aimport java.util.List;%0A%0A
 import s
@@ -1460,32 +1460,321 @@
 ning());%0A    %7D%0A%0A
+    public void boot_twice() %7B%0A        protocolServer.setAmqpService(null);%0A        doNothing().when(protocolServer).run();%0A        protocolServer.boot();%0A        assertTrue(protocolServer.isRunning());%0A        protocolServer.boot();%0A        assertTrue(protocolServer.isRunning());%0A    %7D%0A%0A
     public void 
@@ -2649,32 +2649,396 @@
 lServerType());%0A
+        int numberOfTopicSubscribers = 10;%0A        HashSet%3CSubscriber%3E subscribers = new HashSet%3C%3E();%0A        for(int i = 0; i %3C numberOfTopicSubscribers; i++) %7B%0A            subscribers.add(createSubscriber(%22topic%22));%0A            subscribers.add(createSubscriber(%22notTopic%22));%0A        %7D%0A        doReturn(subscribers).when(subscriptionService).getAllSubscribers();%0A
         protocol
@@ -3089,38 +3089,62 @@
 protocolServer, 
-never(
+times(numberOfTopicSubscribers
 )).incrementTota
@@ -3165,11 +3165,171 @@
 );%0A    %7D
+%0A%0A    private Subscriber createSubscriber(String topic) %7B%0A        return new Subscriber(%22localhost%22, 1234, topic, protocolServer.getProtocolServerType());%0A    %7D
 %0A%7D%0A

Add some more tests for missing branches