Simon Hunt | e556e94 | 2017-06-19 15:35:44 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Simon Hunt | e556e94 | 2017-06-19 15:35:44 -0700 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | */ |
| 17 | |
Simon Hunt | 00b369a | 2017-06-20 19:46:40 -0700 | [diff] [blame] | 18 | package org.onosproject.ui.impl.lion; |
Simon Hunt | e556e94 | 2017-06-19 15:35:44 -0700 | [diff] [blame] | 19 | |
| 20 | import org.junit.Test; |
| 21 | import org.onosproject.ui.AbstractUiTest; |
| 22 | |
| 23 | import java.util.regex.Matcher; |
| 24 | |
| 25 | import static org.junit.Assert.assertEquals; |
| 26 | import static org.junit.Assert.assertFalse; |
| 27 | import static org.junit.Assert.assertTrue; |
| 28 | |
| 29 | /** |
| 30 | * Unit tests for {@link LionConfig}. |
| 31 | */ |
| 32 | public class LionConfigTest extends AbstractUiTest { |
| 33 | |
Simon Hunt | 00b369a | 2017-06-20 19:46:40 -0700 | [diff] [blame] | 34 | private static final String ROOT = "/org/onosproject/ui/lion/"; |
Simon Hunt | e556e94 | 2017-06-19 15:35:44 -0700 | [diff] [blame] | 35 | private static final String CMD_ROOT = ROOT + "_cmd/"; |
| 36 | private static final String CONFIG_ROOT = ROOT + "_config/"; |
| 37 | |
| 38 | private static final String SUFFIX = ".lioncfg"; |
| 39 | |
| 40 | private static final String CARD_GAME_1 = CONFIG_ROOT + "CardGame1" + SUFFIX; |
| 41 | |
| 42 | private LionConfig cfg; |
| 43 | private LionConfig.CmdFrom from; |
| 44 | |
| 45 | |
| 46 | private String cmdPath(String name) { |
| 47 | return CMD_ROOT + name + SUFFIX; |
| 48 | } |
| 49 | |
| 50 | private String configPath(String name) { |
| 51 | return CONFIG_ROOT + name + SUFFIX; |
| 52 | } |
| 53 | |
| 54 | private LionConfig cfg() { |
| 55 | return new LionConfig(); |
| 56 | } |
| 57 | |
| 58 | private void verifyStats(String expId, int expAliases, int expFroms) { |
| 59 | assertEquals("wrong bundle ID", expId, cfg.id()); |
| 60 | assertEquals("wrong alias count", expAliases, cfg.aliasCount()); |
| 61 | assertEquals("wrong from count", expFroms, cfg.fromCount()); |
| 62 | } |
| 63 | |
| 64 | private void verifyKeys(String res, String... keys) { |
| 65 | int nkeys = keys.length; |
| 66 | for (String k: keys) { |
| 67 | assertEquals("key not found: " + k, true, cfg.fromContains(res, k)); |
| 68 | } |
| 69 | assertEquals("wrong key count", nkeys, cfg.fromKeyCount(res)); |
| 70 | } |
| 71 | |
| 72 | @Test |
| 73 | public void importMatch() { |
| 74 | title("importMatch"); |
| 75 | String fromParams = "cs.rank import *"; |
| 76 | Matcher m = LionConfig.RE_IMPORT.matcher(fromParams); |
| 77 | assertEquals("no match", true, m.matches()); |
| 78 | assertEquals("bad group 1", "cs.rank", m.group(1)); |
| 79 | assertEquals("bad group 2", "*", m.group(2)); |
| 80 | } |
| 81 | |
| 82 | @Test |
| 83 | public void basic() { |
| 84 | title("basic"); |
| 85 | cfg = cfg().load(CARD_GAME_1); |
| 86 | print(cfg); |
| 87 | verifyStats("CardGame1", 1, 3); |
| 88 | } |
| 89 | |
| 90 | @Test |
| 91 | public void cmd01GoodBundle() { |
| 92 | title("cmd01GoodBundle"); |
| 93 | cfg = cfg().load(cmdPath("01-bundle")); |
| 94 | verifyStats("foo.bar", 0, 0); |
| 95 | assertEquals("wrong ID", "foo.bar", cfg.id()); |
| 96 | } |
| 97 | |
| 98 | @Test |
| 99 | public void cmd02GoodAlias() { |
| 100 | title("cmd02GoodAlias"); |
| 101 | cfg = cfg().load(cmdPath("02-alias")); |
| 102 | verifyStats(null, 1, 0); |
| 103 | assertEquals("alias/subst not found", "xyzzy.wizard", cfg.alias("xy")); |
| 104 | } |
| 105 | |
| 106 | @Test |
| 107 | public void cmd03GoodFrom() { |
| 108 | title("cmd03GoodFrom"); |
| 109 | cfg = cfg().load(cmdPath("03-from")); |
| 110 | verifyStats(null, 0, 1); |
| 111 | assertEquals("from/keys bad count", 0, cfg.fromKeyCount("non.exist")); |
| 112 | assertEquals("from/keys bad count", 1, cfg.fromKeyCount("foo.bar")); |
| 113 | assertEquals("from/keys not found", true, |
| 114 | cfg.fromContains("foo.bar", "fizzbuzz")); |
| 115 | |
| 116 | from = cfg.entries().iterator().next(); |
| 117 | assertFalse("expected no star", from.starred()); |
| 118 | } |
| 119 | |
| 120 | @Test |
| 121 | public void cmd04GoodFromFour() { |
| 122 | title("cmd04GoodFromFour"); |
| 123 | cfg = cfg().load(cmdPath("04-from-four")); |
| 124 | assertEquals("from/keys bad count", 4, cfg.fromKeyCount("hooray")); |
| 125 | verifyKeys("hooray", "ford", "arthur", "joe", "henry"); |
| 126 | } |
| 127 | |
| 128 | @Test |
| 129 | public void cmd05FromExpand() { |
| 130 | title("cmd05FromExpand"); |
| 131 | cfg = cfg().load(cmdPath("05-from-expand")); |
| 132 | assertEquals("no expand 1", 0, cfg.fromKeyCount("xy.spell")); |
| 133 | assertEquals("no expand 2", 0, cfg.fromKeyCount("xy.learn")); |
| 134 | verifyKeys("xyzzy.wizard.spell", "zonk", "zip", "zuffer"); |
| 135 | verifyKeys("xyzzy.wizard.learn", "zap", "zigzag"); |
| 136 | } |
| 137 | |
| 138 | @Test |
| 139 | public void cmd06FromStar() { |
| 140 | title("cmd06FromStar"); |
| 141 | cfg = cfg().load(cmdPath("06-from-star")); |
| 142 | print(cfg); |
| 143 | assertEquals("bad from count", 1, cfg.fromCount()); |
| 144 | |
| 145 | from = cfg.entries().iterator().next(); |
| 146 | assertTrue("expected a star", from.starred()); |
| 147 | } |
| 148 | |
| 149 | @Test |
| 150 | public void cmd07StarIsSpecial() { |
| 151 | title("cmd06StarIsSpecial"); |
| 152 | cfg = cfg().load(cmdPath("07-star-is-special")); |
| 153 | print(cfg); |
| 154 | assertEquals("no error detected", 3, cfg.errorCount()); |
| 155 | |
| 156 | int iBad = 0; |
| 157 | for (String line: cfg.errorLines()) { |
| 158 | print(line); |
| 159 | iBad++; |
| 160 | String prefix = "from star.bad" + iBad + " import "; |
| 161 | assertTrue("unexpected bad line", line.startsWith(prefix)); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | @Test |
| 166 | public void cardGameConfig() { |
| 167 | title("cardGameConfig"); |
| 168 | cfg = cfg().load(configPath("CardGame1")); |
| 169 | assertEquals("wrong id", "CardGame1", cfg.id()); |
| 170 | assertEquals("wrong alias count", 1, cfg.aliasCount()); |
| 171 | assertEquals("wrong from count", 3, cfg.fromCount()); |
| 172 | |
| 173 | verifyKeys("app.Cards", "*"); |
| 174 | verifyKeys("core.stuff.Rank", "ten", "jack", "queen", "king", "ace"); |
| 175 | verifyKeys("core.stuff.Suit", "spades", "clubs"); |
| 176 | } |
| 177 | } |