/*******************************************************************************
 * Copyright (C) 2016 Push Technology Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/

package com.pushtechnology.adapters.rest.adapter;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.MockitoAnnotations.initMocks;

import java.util.concurrent.ScheduledExecutorService;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;

import com.pushtechnology.adapters.rest.model.latest.Model;

/**
 * Unit tests for {@link RESTAdapter}.
 *
 * @author Push Technology Limited
 */
public final class RESTAdapterTest {

    @Mock
    private ScheduledExecutorService executor;
    @Mock
    private Runnable shutdownHandler;
    @Mock
    private ServiceListener listener;

    @Before
    public void setUp() {
        initMocks(this);
    }

    @After
    public void postConditions() {
        verifyNoMoreInteractions(executor, shutdownHandler, listener);
    }

    @Test
    public void selfStopping() {
        final RESTAdapter adapter = new RESTAdapter(executor, shutdownHandler, listener);

        adapter.reconfigure(Model.builder().active(false).build());

        verify(shutdownHandler).run();
    }

    @Test
    public void startStopInactive() {
        final RESTAdapter adapter = new RESTAdapter(executor, shutdownHandler, listener);

        adapter.reconfigure(Model.builder().active(true).build());

        adapter.close();
    }

    @Test
    public void startRestartStopInactive() {
        final RESTAdapter adapter = new RESTAdapter(executor, shutdownHandler, listener);

        adapter.reconfigure(Model.builder().active(true).build());

        adapter.close();

        adapter.reconfigure(Model.builder().active(true).build());

        adapter.close();
    }
}

@@ -802,16 +802,114 @@
 apter;%0A%0A
+import static java.util.Collections.emptyList;%0Aimport static java.util.Collections.singletonList;%0A
 import s
@@ -942,16 +942,16 @@
 verify;%0A
-
 import s
@@ -1059,16 +1059,46 @@
 Mocks;%0A%0A
+import java.util.Collections;%0A
 import j
@@ -1242,16 +1242,16 @@
 .Mock;%0A%0A
-
 import c
@@ -1301,16 +1301,84 @@
 t.Model;
+%0Aimport com.pushtechnology.adapters.rest.model.latest.ServiceConfig;
 %0A%0A/**%0A *
@@ -2630,32 +2630,702 @@
 rue).build());%0A%0A
+        adapter.close();%0A    %7D%0A%0A    @Test%0A    public void startAddInactiveServiceStop() %7B%0A        final RESTAdapter adapter = new RESTAdapter(executor, shutdownHandler, listener);%0A%0A        adapter.reconfigure(Model.builder().active(true).build());%0A%0A        adapter.reconfigure(Model%0A            .builder()%0A            .active(true)%0A            .services(singletonList(ServiceConfig%0A                .builder()%0A                .name(%22service-0%22)%0A                .host(%22localhost%22)%0A                .secure(false)%0A                .endpoints(emptyList())%0A                .topicRoot(%22root%22)%0A                .pollPeriod(5000)%0A                .build()))%0A            .build());%0A%0A
         adapter.

Add a test that reconfigures the RESTAdapter with an inactive service.