UI-Lion: Migrate BundleStitcher and LionConfig to web.gui module.

Change-Id: Id744e8a3a33621d69379b2286d1cd29770f16e57
diff --git a/web/gui/src/test/java/org/onosproject/ui/impl/lion/BundleStitcherTest.java b/web/gui/src/test/java/org/onosproject/ui/impl/lion/BundleStitcherTest.java
new file mode 100644
index 0000000..e5ae6a6
--- /dev/null
+++ b/web/gui/src/test/java/org/onosproject/ui/impl/lion/BundleStitcherTest.java
@@ -0,0 +1,148 @@
+/*
+ * Copyright 2017-present 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.ui.impl.lion;
+
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Test;
+import org.onosproject.ui.AbstractUiTest;
+import org.onosproject.ui.lion.LionBundle;
+
+import java.util.List;
+import java.util.Locale;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Unit tests for {@link BundleStitcher}.
+ */
+public class BundleStitcherTest extends AbstractUiTest {
+
+    private static final String LION_BASE = "/org/onosproject/ui/lion";
+
+    private static final String[] LION_TAGS = {"CardGame1"};
+
+
+    private static final String[] CARD_GAME_1_KEYS = {
+            "of", "flush", "full_house", "pair", "three_oak",
+            "ace", "king", "queen", "jack", "ten",
+            "spades", "clubs",
+    };
+
+    private static final String[] CARD_GAME_1_ENGLISH = {
+            "of", "Flush", "Full House", "Pair", "Three of a Kind",
+            "Ace", "King", "Queen", "Jack", "Ten",
+            "Spades", "Clubs",
+    };
+
+    // TODO: Andrea to Localize to Italian
+    private static final String[] CARD_GAME_1_ITALIAN = {
+            "of", "Flush", "Full House", "Pair", "Three of a Kind",
+            "Ace", "King", "Queen", "Jack", "Ten",
+            "Spades", "Clubs",
+    };
+
+    private static Locale systemLocale;
+
+    private LionBundle lion;
+
+    @BeforeClass
+    public static void classSetup() {
+        systemLocale = Locale.getDefault();
+    }
+
+    @AfterClass
+    public static void classTeardown() {
+        Locale.setDefault(systemLocale);
+    }
+
+    @Before
+    public void testSetup() {
+        // reset to a known default locale before starting each test
+        Locale.setDefault(Locale.US);
+    }
+
+
+    private BundleStitcher testStitcher() {
+        return new BundleStitcher(LION_BASE);
+    }
+
+    private void verifyItems(LionBundle lion, String[] values) {
+        final int max = values.length;
+        for (int i = 0; i < max; i++) {
+            String key = CARD_GAME_1_KEYS[i];
+            String expValue = values[i];
+            String actValue = lion.getValue(key);
+            assertEquals("wrong mapping", expValue, actValue);
+        }
+    }
+
+    // -- Testing generateLionBundles(...)
+
+    @Test
+    public void generateBundles() {
+        title("generateBundles");
+        List<LionBundle> bundles =
+                BundleStitcher.generateBundles(LION_BASE, LION_TAGS);
+        print(bundles);
+        assertEquals("missing the bundle", 1, bundles.size());
+
+        LionBundle b = bundles.get(0);
+        assertEquals("wrong id", "CardGame1", b.id());
+        assertEquals("unexpected item count", 12, b.size());
+        assertEquals("missing 3oak", "Three of a Kind", b.getValue("three_oak"));
+        assertEquals("missing queen", "Queen", b.getValue("queen"));
+        assertEquals("missing clubs", "Clubs", b.getValue("clubs"));
+    }
+
+    @Test
+    public void cardGame1English() {
+        title("cardGame1English");
+        // use default locale (en_US)
+
+        lion = testStitcher().stitch("CardGame1");
+        print(lion);
+        assertEquals("wrong key", "CardGame1", lion.id());
+        assertEquals("bad key count", 12, lion.size());
+        verifyItems(lion, CARD_GAME_1_ENGLISH);
+    }
+
+    /*
+     * TODO: Andrea to localize
+     * Under: ${ONOS_ROOT}/web/gui/src/test/resources/
+     *
+     * Bundles to Localize:
+     *    org/onosproject/ui/lion/app/Cards.properties
+     *    org/onosproject/ui/lion/core/stuff/Rank.properties
+     *    org/onosproject/ui/lion/core/stuff/Suit.properties
+     */
+    @Ignore("Andrea to localize bundles to Italian")
+    @Test
+    public void cardGame1Italian() {
+        title("cardGame1Italian");
+        Locale.setDefault(Locale.ITALIAN);
+
+        lion = testStitcher().stitch("CardGame1");
+        print(lion);
+        assertEquals("wrong key", "CardGame1", lion.id());
+        assertEquals("bad key count", 12, lion.size());
+        verifyItems(lion, CARD_GAME_1_ITALIAN);
+    }
+}
diff --git a/web/gui/src/test/java/org/onosproject/ui/impl/lion/LionConfigTest.java b/web/gui/src/test/java/org/onosproject/ui/impl/lion/LionConfigTest.java
new file mode 100644
index 0000000..5e4f1dc
--- /dev/null
+++ b/web/gui/src/test/java/org/onosproject/ui/impl/lion/LionConfigTest.java
@@ -0,0 +1,177 @@
+/*
+ * Copyright 2017-present 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.ui.impl.lion;
+
+import org.junit.Test;
+import org.onosproject.ui.AbstractUiTest;
+
+import java.util.regex.Matcher;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Unit tests for {@link LionConfig}.
+ */
+public class LionConfigTest extends AbstractUiTest {
+
+    private static final String ROOT = "/org/onosproject/ui/lion/";
+    private static final String CMD_ROOT = ROOT + "_cmd/";
+    private static final String CONFIG_ROOT = ROOT + "_config/";
+
+    private static final String SUFFIX = ".lioncfg";
+
+    private static final String CARD_GAME_1 = CONFIG_ROOT + "CardGame1" + SUFFIX;
+
+    private LionConfig cfg;
+    private LionConfig.CmdFrom from;
+
+
+    private String cmdPath(String name) {
+        return CMD_ROOT + name + SUFFIX;
+    }
+
+    private String configPath(String name) {
+        return CONFIG_ROOT + name + SUFFIX;
+    }
+
+    private LionConfig cfg() {
+        return new LionConfig();
+    }
+
+    private void verifyStats(String expId, int expAliases, int expFroms) {
+        assertEquals("wrong bundle ID", expId, cfg.id());
+        assertEquals("wrong alias count", expAliases, cfg.aliasCount());
+        assertEquals("wrong from count", expFroms, cfg.fromCount());
+    }
+
+    private void verifyKeys(String res, String... keys) {
+        int nkeys = keys.length;
+        for (String k: keys) {
+            assertEquals("key not found: " + k, true, cfg.fromContains(res, k));
+        }
+        assertEquals("wrong key count", nkeys, cfg.fromKeyCount(res));
+    }
+
+    @Test
+    public void importMatch() {
+        title("importMatch");
+        String fromParams = "cs.rank import *";
+        Matcher m = LionConfig.RE_IMPORT.matcher(fromParams);
+        assertEquals("no match", true, m.matches());
+        assertEquals("bad group 1", "cs.rank", m.group(1));
+        assertEquals("bad group 2", "*", m.group(2));
+    }
+
+    @Test
+    public void basic() {
+        title("basic");
+        cfg = cfg().load(CARD_GAME_1);
+        print(cfg);
+        verifyStats("CardGame1", 1, 3);
+    }
+
+    @Test
+    public void cmd01GoodBundle() {
+        title("cmd01GoodBundle");
+        cfg = cfg().load(cmdPath("01-bundle"));
+        verifyStats("foo.bar", 0, 0);
+        assertEquals("wrong ID", "foo.bar", cfg.id());
+    }
+
+    @Test
+    public void cmd02GoodAlias() {
+        title("cmd02GoodAlias");
+        cfg = cfg().load(cmdPath("02-alias"));
+        verifyStats(null, 1, 0);
+        assertEquals("alias/subst not found", "xyzzy.wizard", cfg.alias("xy"));
+    }
+
+    @Test
+    public void cmd03GoodFrom() {
+        title("cmd03GoodFrom");
+        cfg = cfg().load(cmdPath("03-from"));
+        verifyStats(null, 0, 1);
+        assertEquals("from/keys bad count", 0, cfg.fromKeyCount("non.exist"));
+        assertEquals("from/keys bad count", 1, cfg.fromKeyCount("foo.bar"));
+        assertEquals("from/keys not found", true,
+                     cfg.fromContains("foo.bar", "fizzbuzz"));
+
+        from = cfg.entries().iterator().next();
+        assertFalse("expected no star", from.starred());
+    }
+
+    @Test
+    public void cmd04GoodFromFour() {
+        title("cmd04GoodFromFour");
+        cfg = cfg().load(cmdPath("04-from-four"));
+        assertEquals("from/keys bad count", 4, cfg.fromKeyCount("hooray"));
+        verifyKeys("hooray", "ford", "arthur", "joe", "henry");
+    }
+
+    @Test
+    public void cmd05FromExpand() {
+        title("cmd05FromExpand");
+        cfg = cfg().load(cmdPath("05-from-expand"));
+        assertEquals("no expand 1", 0, cfg.fromKeyCount("xy.spell"));
+        assertEquals("no expand 2", 0, cfg.fromKeyCount("xy.learn"));
+        verifyKeys("xyzzy.wizard.spell", "zonk", "zip", "zuffer");
+        verifyKeys("xyzzy.wizard.learn", "zap", "zigzag");
+    }
+
+    @Test
+    public void cmd06FromStar() {
+        title("cmd06FromStar");
+        cfg = cfg().load(cmdPath("06-from-star"));
+        print(cfg);
+        assertEquals("bad from count", 1, cfg.fromCount());
+
+        from = cfg.entries().iterator().next();
+        assertTrue("expected a star", from.starred());
+    }
+
+    @Test
+    public void cmd07StarIsSpecial() {
+        title("cmd06StarIsSpecial");
+        cfg = cfg().load(cmdPath("07-star-is-special"));
+        print(cfg);
+        assertEquals("no error detected", 3, cfg.errorCount());
+
+        int iBad = 0;
+        for (String line: cfg.errorLines()) {
+            print(line);
+            iBad++;
+            String prefix = "from star.bad" + iBad + " import ";
+            assertTrue("unexpected bad line", line.startsWith(prefix));
+        }
+    }
+
+    @Test
+    public void cardGameConfig() {
+        title("cardGameConfig");
+        cfg = cfg().load(configPath("CardGame1"));
+        assertEquals("wrong id", "CardGame1", cfg.id());
+        assertEquals("wrong alias count", 1, cfg.aliasCount());
+        assertEquals("wrong from count", 3, cfg.fromCount());
+
+        verifyKeys("app.Cards", "*");
+        verifyKeys("core.stuff.Rank", "ten", "jack", "queen", "king", "ace");
+        verifyKeys("core.stuff.Suit", "spades", "clubs");
+    }
+}