/**
 * Licensed to the Rhiot under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The licenses this file to You 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 io.rhiot.component.kura.router;

import com.google.common.collect.ImmutableMap;
import io.rhiot.component.kura.test.TestKuraServer;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.google.common.truth.Truth.assertThat;
import static org.eclipse.kura.core.util.IOUtil.readResource;

public class RhiotKuraRouterTest {

    TestKuraServer kuraServer = new TestKuraServer();

    @Test
    public void shouldHandleNoRoute() throws IOException {
        // Given
        MyRouter router = kuraServer.start(MyRouter.class);

        // When
        router.updated(new HashMap<String, Object>());

        // Then
        assertThat(router.getContext().getRouteDefinitions()).hasSize(0);
    }

    @Test
    public void shouldLoadScrRoutes() throws IOException {
        // Given
        Map<String, Object> scrProperties = ImmutableMap.<String, Object>of(RhiotKuraRouter.XML_ROUTE_PROPERTY, readResource("routes.xml"));
        MyRouter router = kuraServer.start(MyRouter.class);

        // When
        router.updated(scrProperties);

        // Then
        assertThat(router.getContext().getRouteDefinitions()).hasSize(1);
    }

    @Test
    public void shouldReloadScrRoutes() throws IOException {
        // Given
        Map<String, Object> scrProperties = ImmutableMap.<String, Object>of(RhiotKuraRouter.XML_ROUTE_PROPERTY, readResource("routes.xml"));
        MyRouter router = kuraServer.start(MyRouter.class);

        // When
        router.updated(scrProperties);
        router.updated(scrProperties);

        // Then
        assertThat(router.getContext().getRouteDefinitions()).hasSize(1);
    }

    @Test
    public void shouldAddMoreScrRoutes() throws IOException {
        // Given
        Map<String, Object> scrProperties = ImmutableMap.<String, Object>of(RhiotKuraRouter.XML_ROUTE_PROPERTY, readResource("routes.xml"));
        Map<String, Object> newScrProperties = ImmutableMap.<String, Object>of(RhiotKuraRouter.XML_ROUTE_PROPERTY, readResource("routes-new.xml"));
        MyRouter router = kuraServer.start(MyRouter.class);

        // When
        router.updated(scrProperties);
        router.updated(newScrProperties);

        // Then
        assertThat(router.getContext().getRouteDefinitions()).hasSize(2);
    }

    // Fixture classes

    public static class MyRouter extends RhiotKuraRouter {
    }

}


@@ -814,16 +814,23 @@
 %0Aimport 
+static 
 com.goog
@@ -843,30 +843,95 @@
 mon.
-collect.ImmutableMap
+truth.Truth.assertThat;%0Aimport static org.eclipse.kura.core.util.IOUtil.readResource
 ;%0A
+%0A
 impo
@@ -981,31 +981,8 @@
 ver;
-%0Aimport org.junit.Test;
 %0A%0Aim
@@ -1067,117 +1067,70 @@
 ort 
-static com.google.common.truth.Truth.assertTha
+org.junit.Tes
 t;%0A
+%0A
 import 
-static org.eclipse.kura.core.util.IOUtil.readResource
+com.google.common.collect.ImmutableMap
 ;%0A%0Ap
@@ -1686,32 +1686,33 @@
 %3CString, Object%3E
+ 
 of(RhiotKuraRout
@@ -1699,38 +1699,41 @@
 ct%3E of(RhiotKura
-Router
+Constants
 .XML_ROUTE_PROPE
@@ -1728,32 +1728,48 @@
 _ROUTE_PROPERTY,
+%0A               
  readResource(%22r
@@ -2149,32 +2149,33 @@
 %3CString, Object%3E
+ 
 of(RhiotKuraRout
@@ -2162,38 +2162,41 @@
 ct%3E of(RhiotKura
-Router
+Constants
 .XML_ROUTE_PROPE
@@ -2191,32 +2191,48 @@
 _ROUTE_PROPERTY,
+%0A               
  readResource(%22r
@@ -2652,32 +2652,33 @@
 %3CString, Object%3E
+ 
 of(RhiotKuraRout
@@ -2665,38 +2665,41 @@
 ct%3E of(RhiotKura
-Router
+Constants
 .XML_ROUTE_PROPE
@@ -2694,32 +2694,48 @@
 _ROUTE_PROPERTY,
+%0A               
  readResource(%22r
@@ -2824,16 +2824,17 @@
  Object%3E
+ 
 of(Rhiot
@@ -2837,22 +2837,25 @@
 hiotKura
-Router
+Constants
 .XML_ROU
@@ -2866,16 +2866,32 @@
 ROPERTY,
+%0A               
  readRes
@@ -3262,11 +3262,10 @@
 %0A    %7D%0A%0A
+
 %7D%0A
-%0A

update test