unit test and fix for mode switch

Change-Id: I6585adb2857a0ed16ceae605a7f763318c946136
diff --git a/app/src/main/java/org/opencord/sadis/impl/SubscriberAndDeviceInformationAdapter.java b/app/src/main/java/org/opencord/sadis/impl/SubscriberAndDeviceInformationAdapter.java
index fbd77aa..5085ae8 100644
--- a/app/src/main/java/org/opencord/sadis/impl/SubscriberAndDeviceInformationAdapter.java
+++ b/app/src/main/java/org/opencord/sadis/impl/SubscriberAndDeviceInformationAdapter.java
@@ -92,7 +92,7 @@
         long cacheEntryTtl = cfg.getCacheTtl().getSeconds();
 
         // Rebuild cache if needed
-        if ((url != null && url != this.url) || maximumCacheSeize != this.maxiumCacheSize ||
+        if (isurlChanged(url) || maximumCacheSeize != this.maxiumCacheSize ||
                 cacheEntryTtl != this.cacheEntryTtl) {
             this.maxiumCacheSize = maximumCacheSeize;
             this.cacheEntryTtl = cacheEntryTtl;
@@ -111,6 +111,13 @@
         }
     }
 
+    private boolean isurlChanged(String url) {
+        if (url == null && this.url == null) {
+         return false;
+        }
+        return !((url == this.url) || (url != null && url.equals(this.url)));
+    }
+
     /*
      * (non-Javadoc)
      *
diff --git a/app/src/test/java/org/opencord/sadis/impl/SadisManagerTest.java b/app/src/test/java/org/opencord/sadis/impl/SadisManagerTest.java
index 3101f40..d0a2a0e 100644
--- a/app/src/test/java/org/opencord/sadis/impl/SadisManagerTest.java
+++ b/app/src/test/java/org/opencord/sadis/impl/SadisManagerTest.java
@@ -17,6 +17,8 @@
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
 
 import java.io.InputStream;
 import java.time.Duration;
@@ -37,6 +39,8 @@
 import org.onosproject.net.config.ConfigApplyDelegate;
 import org.onosproject.net.config.NetworkConfigRegistryAdapter;
 import org.onosproject.codec.impl.CodecManager;
+import org.onosproject.net.config.NetworkConfigEvent;
+import org.onosproject.net.config.NetworkConfigListener;
 
 import org.opencord.sadis.SubscriberAndDeviceInformation;
 
@@ -49,26 +53,39 @@
 public class SadisManagerTest {
 
     private SadisManager sadis;
+    private ObjectMapper mapper;
+    private ApplicationId subject;
+    private ConfigApplyDelegate delegate;
+    private SadisConfig config;
+    private NetworkConfigEvent event;
+    private static NetworkConfigListener configListener;
+
+     SubscriberAndDeviceInformationBuilder entry1 = SubscriberAndDeviceInformationBuilder.build("1", (short) 2,
+                  (short) 2, "1/1/2", (short) 125, (short) 3, "aa:bb:cc:dd:ee:ff", "XXX-NASID", "10.10.10.10",
+                  "circuit123", "remote123");
+     SubscriberAndDeviceInformationBuilder entry2 = SubscriberAndDeviceInformationBuilder.build("2", (short) 4,
+                  (short) 4, "1/1/2", (short) 129, (short) 4, "aa:bb:cc:dd:ee:ff", "YYY-NASID", "1.1.1.1",
+                  "circuit234", "remote234");
+     SubscriberAndDeviceInformationBuilder entry3 = SubscriberAndDeviceInformationBuilder.build("3", (short) 7,
+                  (short) 8, "1/1/2", (short) 130, (short) 7, "ff:aa:dd:cc:bb:ee", "MNO-NASID", "30.30.30.30",
+                  "circuit567", "remote567");
+     SubscriberAndDeviceInformationBuilder entry4 = SubscriberAndDeviceInformationBuilder.build("4", (short) 2,
+                  (short) 1, "1/1/2", (short) 132, (short) 1, "ff:cc:dd:aa:ee:bb", "PQR-NASID", "15.15.15.15",
+                  "circuit678", "remote678");
 
     @Before
     public void setUp() throws Exception {
-        this.sadis = new SadisManager();
-        this.sadis.coreService = new MockCoreService();
-
-        final InputStream jsonStream = SadisManagerTest.class.getResourceAsStream("/config.json");
-
-        final ObjectMapper mapper = new ObjectMapper();
-        final JsonNode testConfig = mapper.readTree(jsonStream);
-        final ConfigApplyDelegate delegate = new MockConfigDelegate();
-
-        final SadisConfig config = new SadisConfig();
-        final ApplicationId subject = this.sadis.coreService.registerApplication("org.opencord.sadis");
-
-        config.init(subject, "sadis-test", testConfig, mapper, delegate);
-
-        this.sadis.cfgService = new MockNetworkConfigRegistry(config);
-        this.sadis.codecService = new CodecManager();
-        this.sadis.activate();
+        sadis = new SadisManager();
+        sadis.coreService = new MockCoreService();
+        delegate = new MockConfigDelegate();
+        mapper = new ObjectMapper();
+        config = new SadisConfig();
+        subject = sadis.coreService.registerApplication("org.opencord.sadis");
+        config.init(subject, "sadis-local-mode-test", node("/LocalConfig.json"), mapper, delegate);
+        sadis.cfgService = new MockNetworkConfigRegistry(config);
+        event = new NetworkConfigEvent(NetworkConfigEvent.Type.CONFIG_ADDED, subject, config.getClass());
+        sadis.codecService = new CodecManager();
+        sadis.activate();
     }
 
     @After
@@ -96,6 +113,88 @@
 
     }
 
+    @Test
+    public void testLocalMode() throws Exception {
+        SubscriberAndDeviceInformation entry = sadis.get("3");
+        assertNull(entry);
+
+        entry = sadis.get("1");
+        assertNotNull(entry);
+        assertTrue(entry1.checkEquals(entry));
+
+        entry = sadis.get("2");
+        assertNotNull(entry);
+        assertTrue(entry2.checkEquals(entry));
+
+        sadis.invalidateId("1");
+        entry = sadis.getfromCache("1");
+        assertNull(entry);
+        entry = sadis.get("1");
+        assertNotNull(entry);
+        assertTrue(entry1.checkEquals(entry));
+
+        sadis.invalidateAll();
+        entry = sadis.getfromCache("2");
+        assertNull(entry);
+        entry = sadis.get("2");
+        assertNotNull(entry);
+        assertTrue(entry2.checkEquals(entry));
+    }
+
+    @Test
+    public void testRemoteMode() throws Exception {
+        config.init(subject, "sadis-remote-mode-test", node("/RemoteConfig.json"), mapper, delegate);
+        configListener.event(event);
+
+        SubscriberAndDeviceInformation entry = sadis.get("3");
+        assertNotNull(entry);
+        assertTrue(entry3.checkEquals(entry));
+
+        entry = sadis.get("4");
+        assertNotNull(entry);
+        assertTrue(entry4.checkEquals(entry));
+
+        sadis.invalidateId("3");
+        entry = sadis.getfromCache("3");
+        assertNull(entry);
+        entry = sadis.get("3");
+        assertNotNull(entry);
+        assertTrue(entry3.checkEquals(entry));
+
+        sadis.invalidateAll();
+        entry = sadis.getfromCache("4");
+        assertNull(entry);
+        entry = sadis.get("4");
+        assertNotNull(entry);
+        assertTrue(entry4.checkEquals(entry));
+
+        entry = sadis.get("8");
+        assertNull(entry);
+    }
+
+    @Test
+    public void testModeSwitch() throws Exception {
+        config.init(subject, "sadis-remote-mode-test", node("/RemoteConfig.json"), mapper, delegate);
+        configListener.event(event);
+        SubscriberAndDeviceInformation entry = sadis.get("3");
+        assertNotNull(entry);
+        entry = sadis.get("1");
+        assertNull(entry);
+
+        config.init(subject, "sadis-local-mode-test", node("/LocalConfig.json"), mapper, delegate);
+        configListener.event(event);
+        entry = sadis.get("1");
+        assertNotNull(entry);
+        entry = sadis.get("3");
+        assertNull(entry);
+    }
+
+    private JsonNode node(String jsonFile) throws Exception {
+        final InputStream jsonStream = SadisManagerTest.class.getResourceAsStream(jsonFile);
+        final JsonNode testConfig = mapper.readTree(jsonStream);
+        return testConfig;
+    }
+
     // Mocks live here
 
     private static final class SubscriberAndDeviceInformationBuilder extends SubscriberAndDeviceInformation {
@@ -309,5 +408,10 @@
         public <S, C extends Config<S>> C getConfig(final S subject, final Class<C> configClass) {
             return (C) this.config;
         }
+
+        @Override
+        public void addListener(NetworkConfigListener listener) {
+            configListener = listener;
+        }
     }
 }
diff --git a/app/src/test/resources/3 b/app/src/test/resources/3
new file mode 100644
index 0000000..a25567f
--- /dev/null
+++ b/app/src/test/resources/3
@@ -0,0 +1,13 @@
+{
+      "id": "3",
+      "cTag": 7,
+      "sTag": 8,
+      "nasPortId": "1/1/2",
+      "port": 130,
+      "slot": 7,
+      "hardwareIdentifier": "ff:aa:dd:cc:bb:ee",
+      "ipAddress":"30.30.30.30",
+      "nasId":"MNO-NASID",
+      "circuitId":"circuit567",
+      "remoteId":"remote567"
+}
diff --git a/app/src/test/resources/4 b/app/src/test/resources/4
new file mode 100644
index 0000000..2a8ee5c
--- /dev/null
+++ b/app/src/test/resources/4
@@ -0,0 +1,13 @@
+{
+      "id": "4",
+      "cTag": 2,
+      "sTag": 1,
+      "nasPortId": "1/1/2",
+      "port": 132,
+      "slot": 1,
+      "hardwareIdentifier": "ff:cc:dd:aa:ee:bb",
+      "ipAddress":"15.15.15.15",
+      "nasId":"PQR-NASID",
+      "circuitId":"circuit678",
+      "remoteId":"remote678"
+}
diff --git a/app/src/test/resources/config.json b/app/src/test/resources/LocalConfig.json
similarity index 90%
rename from app/src/test/resources/config.json
rename to app/src/test/resources/LocalConfig.json
index c6b8f37..b855239 100644
--- a/app/src/test/resources/config.json
+++ b/app/src/test/resources/LocalConfig.json
@@ -1,7 +1,6 @@
 {
 	"integration":
 	{
-		"url": "http://localhost:8090",
 		"cache":
 		{
 			"enabled": true,
@@ -37,7 +36,7 @@
 			"ipAddress":"1.1.1.1",
 			"nasId":"YYY-NASID",
 			"circuitId":"circuit234",
-                        "remoteId":"remote234"
+			"remoteId":"remote234"
 		},
 
 		{
diff --git a/app/src/test/resources/RemoteConfig.json b/app/src/test/resources/RemoteConfig.json
new file mode 100644
index 0000000..50715ff
--- /dev/null
+++ b/app/src/test/resources/RemoteConfig.json
@@ -0,0 +1,10 @@
+{
+  "integration": {
+    "url": "file:src/test/resources",
+    "cache": {
+      "enabled": true,
+      "maxsize": 50,
+      "ttl": "PT1m"
+    }
+  }
+}