/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Norwegian Defence Research Establishment / NTNU
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

package no.ntnu.okse.core;

import no.ntnu.okse.protocol.Protocol;
import no.ntnu.okse.protocol.servers.ProtocolServer;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;

import static org.testng.Assert.*;

public class CoreServiceTest {

    CoreService cs;

    @BeforeMethod
    public void setUp() throws Exception {
        cs = new CoreService();
    }

    @AfterMethod
    public void tearDown() throws Exception {
        // STUB
    }

    @Test
    public void testGetEventQueue() throws Exception {
        assertTrue(cs.getEventQueue() instanceof LinkedBlockingQueue);
    }

    @Test
    public void testGetExecutor() throws Exception {
        assertTrue(cs.getExecutor() instanceof ThreadPoolExecutor);
    }

    @Test
    public void testAddProtocolServer() throws Exception {
        ProtocolServer testProtocolServer = () -> System.out.println("ProtocolServer initialized.");

        // Test that adding a new ProtocolServer works.
        cs.addProtocolServer(testProtocolServer);
        assertEquals(cs.getAllProtocolServers().size(), 1,
                "The amount of registered ProtocolServers should be 1 at this point.");

        // Adding the same ProtocolServer should not have any effect
        cs.addProtocolServer(testProtocolServer);
        assertEquals(cs.getAllProtocolServers().size(), 1,
                "The amount of registered ProtocolServers should still be 1 at this point.");

        // Remove it again.
        cs.removeProtocolServer(testProtocolServer);
    }

    @Test
    public void testRemoveProtocolServer() throws Exception {
        ProtocolServer testProtocolServer = () -> System.out.println("ProtocolServer initialized.");

        // Test that removing a non-existant PS has no effect
        cs.removeProtocolServer(testProtocolServer);
        assertEquals(cs.getAllProtocolServers().size(), 0,
                "The amount of PS should still be 0 at this point.");

        // Test adding and removing a PS works
        cs.addProtocolServer(testProtocolServer);
        assertEquals(cs.getAllProtocolServers().size(), 1,
                "The amount of registered ProtocolServers should be 1 at this point.");
        cs.removeProtocolServer(testProtocolServer);
        assertEquals(cs.getAllProtocolServers().size(), 0,
                "The amount of registered ProtocolServers should be 0 at this point.");
    }
}
@@ -1206,47 +1206,8 @@
 e;%0A%0A
-import no.ntnu.okse.protocol.Protocol;%0A
 impo
@@ -2988,33 +2988,32 @@
 ialized.%22);%0A
-%0A
         
 // Test that
@@ -3004,61 +3004,122 @@
     
-// Test that removing a non-existant PS has no effect
+ProtocolServer testProtocolServer2 = () -%3E System.out.println(%22ProtocolServer initialized.%22);%0A%0A        // Add a PS
 %0A   
@@ -3118,38 +3118,35 @@
 a PS%0A        cs.
-remove
+add
 ProtocolServer(t
@@ -3213,33 +3213,33 @@
 rvers().size(), 
-0
+1
 ,%0A              
@@ -3259,18 +3259,42 @@
  of 
-PS
+registered ProtocolServers
  should 
 stil
@@ -3293,18 +3293,12 @@
 uld 
-still 
 be 
-0
+1
  at 
@@ -3329,22 +3329,10 @@
 // T
-est adding and
+ry
  rem
@@ -3342,21 +3342,31 @@
 ng a PS 
-works
+not in the list
 %0A       
@@ -3361,35 +3361,38 @@
 list%0A        cs.
-add
+remove
 ProtocolServer(t
@@ -3400,32 +3400,33 @@
 stProtocolServer
+2
 );%0A        asser
@@ -3506,42 +3506,18 @@
  of 
-registered ProtocolServers
+PS
  should 
 be 1
@@ -3504,32 +3504,38 @@
 nt of PS should 
+still 
 be 1 at this poi
@@ -3536,24 +3536,66 @@
 is point.%22);
+%0A%0A        // Try removing a PS in the list
 %0A        cs.
@@ -3779,19 +3779,20 @@
 s point.%22);%0A
+%0A
     %7D%0A%7D

Change the CoreService tests a bit