UI-Lion:
- BundleStitcherTest now working!
- Expand aliases in from lines.
- Added unit tests for parsing of from lines.

Change-Id: I65d343f1283fd60f46879431c37299c6ecd5a36e
diff --git a/core/api/src/test/java/org/onosproject/ui/lion/LionBundleTest.java b/core/api/src/test/java/org/onosproject/ui/lion/LionBundleTest.java
index 7100088..f4f27ee 100644
--- a/core/api/src/test/java/org/onosproject/ui/lion/LionBundleTest.java
+++ b/core/api/src/test/java/org/onosproject/ui/lion/LionBundleTest.java
@@ -18,17 +18,36 @@
 package org.onosproject.ui.lion;
 
 import org.junit.Test;
+import org.onosproject.ui.AbstractUiTest;
+
+import static org.junit.Assert.assertEquals;
 
 /**
  * Unit tests for {@link LionBundle}.
  */
-public class LionBundleTest {
+public class LionBundleTest extends AbstractUiTest {
+
+    private static final String ID = "foo";
+    private static final String KEY_A = "ka";
+    private static final String KEY_B = "kb";
+    private static final String VAL_A = "Alpha";
+    private static final String VAL_B = "Beta";
 
     private LionBundle bundle;
 
     @Test
     public void basic() {
+        title("basic");
 
-//        bundle = new LionBundle();
+        bundle = new LionBundle.Builder(ID)
+                .addItem(KEY_A, VAL_A)
+                .addItem(KEY_B, VAL_B)
+                .build();
+        print(bundle);
+        assertEquals("wrong id", ID, bundle.id());
+        assertEquals("wrong item count", 2, bundle.size());
+
+        assertEquals("wrong A lookup", VAL_A, bundle.getValue(KEY_A));
+        assertEquals("wrong B lookup", VAL_B, bundle.getValue(KEY_B));
     }
 }
diff --git a/core/api/src/test/java/org/onosproject/ui/lion/stitch/BundleStitcherTest.java b/core/api/src/test/java/org/onosproject/ui/lion/stitch/BundleStitcherTest.java
new file mode 100644
index 0000000..a362deb
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/ui/lion/stitch/BundleStitcherTest.java
@@ -0,0 +1,94 @@
+/*
+ * 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.lion.stitch;
+
+import org.junit.Test;
+import org.onosproject.ui.AbstractUiTest;
+import org.onosproject.ui.lion.LionBundle;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Unit tests for {@link BundleStitcher}.
+ */
+public class BundleStitcherTest extends AbstractUiTest {
+
+    private static final String TEST_RESOURCE_BASE =
+            "/org/onosproject/ui/lion/stitchtests";
+
+    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",
+    };
+
+
+    private LionBundle lion;
+
+    private BundleStitcher testStitcher() {
+        return new BundleStitcher(TEST_RESOURCE_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);
+        }
+    }
+
+    @Test
+    public void cardGame1English() {
+        title("cardGame1English");
+        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);
+    }
+}
diff --git a/core/api/src/test/java/org/onosproject/ui/lion/stitch/LionConfigTest.java b/core/api/src/test/java/org/onosproject/ui/lion/stitch/LionConfigTest.java
new file mode 100644
index 0000000..ec636b1
--- /dev/null
+++ b/core/api/src/test/java/org/onosproject/ui/lion/stitch/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.lion.stitch;
+
+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/stitchtests/";
+    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");
+    }
+}
diff --git a/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/_cmd/01-bundle.lioncfg b/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/_cmd/01-bundle.lioncfg
new file mode 100644
index 0000000..127de70
--- /dev/null
+++ b/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/_cmd/01-bundle.lioncfg
@@ -0,0 +1,2 @@
+# 01. test the bundle command
+bundle foo.bar
\ No newline at end of file
diff --git a/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/_cmd/02-alias.lioncfg b/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/_cmd/02-alias.lioncfg
new file mode 100644
index 0000000..1bdba86
--- /dev/null
+++ b/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/_cmd/02-alias.lioncfg
@@ -0,0 +1,2 @@
+# 02. good alias
+alias xy xyzzy.wizard
diff --git a/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/_cmd/03-from.lioncfg b/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/_cmd/03-from.lioncfg
new file mode 100644
index 0000000..d856ebd
--- /dev/null
+++ b/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/_cmd/03-from.lioncfg
@@ -0,0 +1,2 @@
+# 03. good from
+from foo.bar import fizzbuzz
diff --git a/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/_cmd/04-from-four.lioncfg b/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/_cmd/04-from-four.lioncfg
new file mode 100644
index 0000000..ec13545
--- /dev/null
+++ b/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/_cmd/04-from-four.lioncfg
@@ -0,0 +1,2 @@
+# 04. good from with 4 keys
+from hooray import henry, joe, arthur, ford
diff --git a/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/_cmd/05-from-expand.lioncfg b/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/_cmd/05-from-expand.lioncfg
new file mode 100644
index 0000000..920d701
--- /dev/null
+++ b/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/_cmd/05-from-expand.lioncfg
@@ -0,0 +1,4 @@
+# 05. from expanding alias
+alias xy xyzzy.wizard
+from xy.spell import zonk, zip, zuffer
+from xy.learn import zap, zigzag
diff --git a/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/_cmd/06-from-star.lioncfg b/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/_cmd/06-from-star.lioncfg
new file mode 100644
index 0000000..3019c2f
--- /dev/null
+++ b/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/_cmd/06-from-star.lioncfg
@@ -0,0 +1,2 @@
+# 06. from star
+from star.singular import *
diff --git a/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/_cmd/07-star-is-special.lioncfg b/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/_cmd/07-star-is-special.lioncfg
new file mode 100644
index 0000000..9a72010
--- /dev/null
+++ b/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/_cmd/07-star-is-special.lioncfg
@@ -0,0 +1,6 @@
+# 07. star is special
+from abc import foo, bar
+from star.bad1 import *, foo
+from star.good import *
+from star.bad2 import foo, *, bar
+from star.bad3 import xanth, *
diff --git a/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/_config/CardGame1.lioncfg b/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/_config/CardGame1.lioncfg
new file mode 100644
index 0000000..00135cc
--- /dev/null
+++ b/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/_config/CardGame1.lioncfg
@@ -0,0 +1,10 @@
+# test configuration
+
+bundle CardGame1
+
+alias cs core.stuff
+
+from app.Cards import *
+
+from cs.Rank import ace, king, queen, jack, ten
+from cs.Suit import spades, clubs
diff --git a/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/app/Cards.properties b/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/app/Cards.properties
new file mode 100644
index 0000000..23f8766
--- /dev/null
+++ b/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/app/Cards.properties
@@ -0,0 +1,23 @@
+#
+# 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.
+#
+#
+# --- more properties for a card game
+of=of
+flush=Flush
+full_house=Full House
+pair=Pair
+three_oak=Three of a Kind
+
diff --git a/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/core/stuff/Rank.properties b/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/core/stuff/Rank.properties
new file mode 100644
index 0000000..a6b35f0
--- /dev/null
+++ b/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/core/stuff/Rank.properties
@@ -0,0 +1,30 @@
+#
+# 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.
+#
+#
+# --- Card Ranks
+two=Two
+three=Three
+four=Four
+five=Five
+six=Six
+seven=Seven
+eight=Eight
+nine=Nine
+ten=Ten
+jack=Jack
+queen=Queen
+king=King
+ace=Ace
diff --git a/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/core/stuff/Suit.properties b/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/core/stuff/Suit.properties
new file mode 100644
index 0000000..dab0e22
--- /dev/null
+++ b/core/api/src/test/resources/org/onosproject/ui/lion/stitchtests/core/stuff/Suit.properties
@@ -0,0 +1,21 @@
+#
+# 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.
+#
+#
+# --- Card Suits
+clubs=Clubs
+hearts=Hearts
+spades=Spades
+diamonds=Diamonds