CORD Subscriber GUI -- More bundle wrangling.

Change-Id: I2fafdb281712d7747399d61611c3d4bb663a39b5
diff --git a/apps/demo/cord-gui/src/test/org/onosproject/cord/gui/CoreModelCacheTest.java b/apps/demo/cord-gui/src/test/org/onosproject/cord/gui/CoreModelCacheTest.java
new file mode 100644
index 0000000..bccd099
--- /dev/null
+++ b/apps/demo/cord-gui/src/test/org/onosproject/cord/gui/CoreModelCacheTest.java
@@ -0,0 +1,151 @@
+/*
+ * Copyright 2015 Open Networking Laboratory
+ *
+ * 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 org.onosproject.cord.gui;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.junit.Before;
+import org.junit.Test;
+import org.onosproject.cord.gui.model.BundleFactory;
+import org.onosproject.cord.gui.model.SubscriberUser;
+
+import java.io.IOException;
+import java.util.List;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Unit tests for {@link CordModelCache}.
+ */
+public class CoreModelCacheTest {
+
+    private CordModelCache cache;
+
+    @Before
+    public void setUp() {
+        cache = new CordModelCache();
+    }
+
+    @Test
+    public void basic() {
+        assertEquals("wrong bundle", BundleFactory.BASIC_BUNDLE,
+                     cache.getCurrentBundle().descriptor());
+    }
+
+    @Test
+    public void basicBundleJson() {
+        String json = BundleFactory.toJson(cache.getCurrentBundle());
+        assertTrue("bad basic json", sameJson(BASIC_BUNDLE_JSON, json));
+    }
+
+    @Test
+    public void chooseFamilyBundle() {
+        cache.setCurrentBundle("family");
+        assertEquals("wrong bundle", BundleFactory.FAMILY_BUNDLE,
+                     cache.getCurrentBundle().descriptor());
+    }
+
+    @Test
+    public void familyBundleJson() {
+        cache.setCurrentBundle("family");
+        String json = BundleFactory.toJson(cache.getCurrentBundle());
+        System.out.println(json);
+        assertTrue("bad family json", sameJson(FAMILY_BUNDLE_JSON, json));
+    }
+
+    @Test
+    public void checkUsers() {
+        List<SubscriberUser> users = cache.getUsers();
+        assertEquals("wrong # users", 4, users.size());
+    }
+
+    // =============
+
+    private boolean sameJson(String s1, String s2) {
+        try {
+            JsonNode tree1 = MAPPER.readTree(s1);
+            JsonNode tree2 = MAPPER.readTree(s2);
+            return tree1.equals(tree2);
+        } catch (IOException e) {
+            System.out.println("Exception: " + e);
+        }
+        return false;
+    }
+
+    private static final ObjectMapper MAPPER = new ObjectMapper();
+
+    private static final String BASIC_BUNDLE_JSON = "{\n" +
+            "  \"bundle\": {\n" +
+            "    \"id\": \"basic\",\n" +
+            "    \"name\": \"Basic Bundle\",\n" +
+            "    \"functions\": [\n" +
+            "      {\n" +
+            "        \"id\": \"internet\",\n" +
+            "        \"name\": \"Internet\",\n" +
+            "        \"desc\": \"Basic internet connectivity.\",\n" +
+            "        \"params\": {}\n" +
+            "      },\n" +
+            "      {\n" +
+            "        \"id\": \"firewall\",\n" +
+            "        \"name\": \"Firewall\",\n" +
+            "        \"desc\": \"Normal firewall protection.\",\n" +
+            "        \"params\": {}\n" +
+            "      }\n" +
+            "    ]\n" +
+            "  },\n" +
+            "  \"bundles\": [\n" +
+            "    { \"id\": \"basic\", \"name\": \"Basic Bundle\" },\n" +
+            "    { \"id\": \"family\", \"name\": \"Family Bundle\" }\n" +
+            "  ]\n" +
+            "}\n";
+
+    private static final String FAMILY_BUNDLE_JSON = "{\n" +
+            "  \"bundle\": {\n" +
+            "    \"id\": \"family\",\n" +
+            "    \"name\": \"Family Bundle\",\n" +
+            "    \"functions\": [\n" +
+            "      {\n" +
+            "        \"id\": \"internet\",\n" +
+            "        \"name\": \"Internet\",\n" +
+            "        \"desc\": \"Basic internet connectivity.\",\n" +
+            "        \"params\": {}\n" +
+            "      },\n" +
+            "      {\n" +
+            "        \"id\": \"firewall\",\n" +
+            "        \"name\": \"Firewall\",\n" +
+            "        \"desc\": \"Normal firewall protection.\",\n" +
+            "        \"params\": {}\n" +
+            "      },\n" +
+            "      {\n" +
+            "        \"id\": \"url_filter\",\n" +
+            "        \"name\": \"Parental Control\",\n" +
+            "        \"desc\": \"Variable levels of URL filtering.\",\n" +
+            "        \"params\": {\n" +
+            "          \"level\": \"PG\",\n" +
+            "          \"levels\": [ \"PG\", \"PG-13\", \"R\" ]\n" +
+            "        }\n" +
+            "      }\n" +
+            "    ]\n" +
+            "  },\n" +
+            "  \"bundles\": [\n" +
+            "    { \"id\": \"basic\", \"name\": \"Basic Bundle\" },\n" +
+            "    { \"id\": \"family\", \"name\": \"Family Bundle\" }\n" +
+            "  ]\n" +
+            "}\n";
+}
diff --git a/apps/demo/cord-gui/src/test/org/onosproject/cord/gui/model/BundleFactoryTest.java b/apps/demo/cord-gui/src/test/org/onosproject/cord/gui/model/BundleFactoryTest.java
index 999ee97..a4d662e 100644
--- a/apps/demo/cord-gui/src/test/org/onosproject/cord/gui/model/BundleFactoryTest.java
+++ b/apps/demo/cord-gui/src/test/org/onosproject/cord/gui/model/BundleFactoryTest.java
@@ -22,7 +22,7 @@
 import java.util.Set;
 
 import static org.junit.Assert.*;
-import static org.onosproject.cord.gui.model.BundleFactory.availableBundles;
+import static org.onosproject.cord.gui.model.BundleFactory.*;
 import static org.onosproject.cord.gui.model.XosFunctionDescriptor.*;
 
 /**
@@ -33,11 +33,13 @@
     @Test
     public void bundleCount() {
         assertEquals("wrong count", 2, availableBundles().size());
+        assertTrue("missing basic", availableBundles().contains(BASIC_BUNDLE));
+        assertTrue("missing family", availableBundles().contains(FAMILY_BUNDLE));
     }
 
     @Test
     public void basicBundle() {
-        BundleDescriptor bundle = availableBundles().get(0);
+        BundleDescriptor bundle = BundleFactory.BASIC_BUNDLE;
         assertEquals("wrong id", "basic", bundle.id());
         assertEquals("wrong id", "Basic Bundle", bundle.displayName());
         Set<XosFunctionDescriptor> funcs = bundle.functions();
@@ -48,7 +50,7 @@
 
     @Test
     public void familyBundle() {
-        BundleDescriptor bundle = availableBundles().get(1);
+        BundleDescriptor bundle = BundleFactory.FAMILY_BUNDLE;
         assertEquals("wrong id", "family", bundle.id());
         assertEquals("wrong id", "Family Bundle", bundle.displayName());
         Set<XosFunctionDescriptor> funcs = bundle.functions();
@@ -57,5 +59,19 @@
         assertTrue("missing url-f", funcs.contains(URL_FILTER));
     }
 
+    @Test
+    public void bundleFromIdBasic() {
+        assertEquals("wrong bundle", BASIC_BUNDLE, bundleFromId("basic"));
+    }
+
+    @Test
+    public void bundleFromIdFamily() {
+        assertEquals("wrong bundle", FAMILY_BUNDLE, bundleFromId("family"));
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void bundleFromIdUnknown() {
+        bundleFromId("unknown");
+    }
 }
 
diff --git a/apps/demo/cord-gui/src/test/org/onosproject/cord/gui/model/XosFunctionDescriptorTest.java b/apps/demo/cord-gui/src/test/org/onosproject/cord/gui/model/XosFunctionDescriptorTest.java
index c08d019..fd8cb8b 100644
--- a/apps/demo/cord-gui/src/test/org/onosproject/cord/gui/model/XosFunctionDescriptorTest.java
+++ b/apps/demo/cord-gui/src/test/org/onosproject/cord/gui/model/XosFunctionDescriptorTest.java
@@ -49,7 +49,7 @@
 
     @Test
     public void urlFiltering() {
-        assertEquals("wrong id", "url_filtering", URL_FILTER.id());
+        assertEquals("wrong id", "url_filter", URL_FILTER.id());
         assertEquals("wrong display", "Parental Control", URL_FILTER.displayName());
         assertTrue("wrong desc", URL_FILTER.description().startsWith("Variable"));
     }