Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 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 | |
| 18 | package org.onosproject.cord.gui; |
| 19 | |
| 20 | import com.fasterxml.jackson.databind.JsonNode; |
| 21 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onosproject.cord.gui.model.BundleFactory; |
| 25 | import org.onosproject.cord.gui.model.SubscriberUser; |
| 26 | |
| 27 | import java.io.IOException; |
| 28 | import java.util.List; |
| 29 | |
| 30 | import static org.junit.Assert.assertEquals; |
| 31 | import static org.junit.Assert.assertTrue; |
| 32 | |
| 33 | /** |
| 34 | * Unit tests for {@link CordModelCache}. |
| 35 | */ |
| 36 | public class CoreModelCacheTest { |
| 37 | |
| 38 | private CordModelCache cache; |
| 39 | |
| 40 | @Before |
| 41 | public void setUp() { |
| 42 | cache = new CordModelCache(); |
| 43 | } |
| 44 | |
| 45 | @Test |
| 46 | public void basic() { |
| 47 | assertEquals("wrong bundle", BundleFactory.BASIC_BUNDLE, |
| 48 | cache.getCurrentBundle().descriptor()); |
| 49 | } |
| 50 | |
| 51 | @Test |
| 52 | public void basicBundleJson() { |
| 53 | String json = BundleFactory.toJson(cache.getCurrentBundle()); |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 54 | System.out.println(json); |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 55 | assertTrue("bad basic json", sameJson(BASIC_BUNDLE_JSON, json)); |
| 56 | } |
| 57 | |
| 58 | @Test |
| 59 | public void chooseFamilyBundle() { |
| 60 | cache.setCurrentBundle("family"); |
| 61 | assertEquals("wrong bundle", BundleFactory.FAMILY_BUNDLE, |
| 62 | cache.getCurrentBundle().descriptor()); |
| 63 | } |
| 64 | |
| 65 | @Test |
| 66 | public void familyBundleJson() { |
| 67 | cache.setCurrentBundle("family"); |
| 68 | String json = BundleFactory.toJson(cache.getCurrentBundle()); |
| 69 | System.out.println(json); |
| 70 | assertTrue("bad family json", sameJson(FAMILY_BUNDLE_JSON, json)); |
| 71 | } |
| 72 | |
| 73 | @Test |
| 74 | public void checkUsers() { |
| 75 | List<SubscriberUser> users = cache.getUsers(); |
| 76 | assertEquals("wrong # users", 4, users.size()); |
| 77 | } |
| 78 | |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 79 | @Test |
| 80 | public void usersBasicJson() { |
| 81 | String json = cache.jsonUsers(); |
| 82 | System.out.println(json); |
| 83 | assertTrue("bad users basic json", sameJson(USERS_BASIC, json)); |
| 84 | } |
| 85 | |
| 86 | @Test |
| 87 | public void usersFamilyJson() { |
| 88 | cache.setCurrentBundle("family"); |
| 89 | String json = cache.jsonUsers(); |
| 90 | System.out.println(json); |
| 91 | assertTrue("bad users family json", sameJson(USERS_FAMILY, json)); |
| 92 | } |
| 93 | |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 94 | @Test |
| 95 | public void setNewLevel() { |
| 96 | cache.setCurrentBundle("family"); |
| 97 | JsonNode node = fromString(cache.jsonUsers()); |
| 98 | assertEquals("wrong level", "PG", getMomsLevel(node)); |
| 99 | |
| 100 | cache.applyPerUserParam("1", "url_filter", "level", "R"); |
| 101 | |
| 102 | node = fromString(cache.jsonUsers()); |
| 103 | assertEquals("wrong level", "R", getMomsLevel(node)); |
| 104 | } |
| 105 | |
| 106 | private String getMomsLevel(JsonNode node) { |
| 107 | JsonNode mom = node.get("users").elements().next(); |
| 108 | assertEquals("wrong ID", 1, mom.get("id").asInt()); |
| 109 | return mom.get("profile").get("url_filter").get("level").asText(); |
| 110 | } |
| 111 | |
| 112 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 113 | // ============= |
| 114 | |
Simon Hunt | 87b157c | 2015-05-22 12:09:59 -0700 | [diff] [blame] | 115 | private JsonNode fromString(String s) { |
| 116 | try { |
| 117 | return MAPPER.readTree(s); |
| 118 | } catch (IOException e) { |
| 119 | System.out.println("Exception: " + e); |
| 120 | } |
| 121 | return null; |
| 122 | } |
| 123 | |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 124 | private boolean sameJson(String s1, String s2) { |
| 125 | try { |
| 126 | JsonNode tree1 = MAPPER.readTree(s1); |
| 127 | JsonNode tree2 = MAPPER.readTree(s2); |
| 128 | return tree1.equals(tree2); |
| 129 | } catch (IOException e) { |
| 130 | System.out.println("Exception: " + e); |
| 131 | } |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | private static final ObjectMapper MAPPER = new ObjectMapper(); |
| 136 | |
| 137 | private static final String BASIC_BUNDLE_JSON = "{\n" + |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 138 | " \"bundle\": {\n" + |
| 139 | " \"id\": \"basic\",\n" + |
| 140 | " \"name\": \"Basic Bundle\",\n" + |
| 141 | " \"desc\": \"Provides basic internet and firewall functions.\",\n" + |
| 142 | " \"functions\": [\n" + |
| 143 | " {\n" + |
| 144 | " \"id\": \"internet\",\n" + |
| 145 | " \"name\": \"Internet\",\n" + |
| 146 | " \"desc\": \"Basic internet connectivity.\",\n" + |
| 147 | " \"params\": {}\n" + |
| 148 | " },\n" + |
| 149 | " {\n" + |
| 150 | " \"id\": \"firewall\",\n" + |
| 151 | " \"name\": \"Firewall\",\n" + |
| 152 | " \"desc\": \"Normal firewall protection.\",\n" + |
| 153 | " \"params\": {}\n" + |
| 154 | " }\n" + |
| 155 | " ]\n" + |
| 156 | " },\n" + |
| 157 | " \"bundles\": [\n" + |
| 158 | " {\n" + |
| 159 | " \"id\": \"basic\",\n" + |
| 160 | " \"name\": \"Basic Bundle\",\n" + |
| 161 | " \"desc\": \"Provides basic internet and firewall functions.\"\n" + |
| 162 | " },\n" + |
| 163 | " {\n" + |
| 164 | " \"id\": \"family\",\n" + |
| 165 | " \"name\": \"Family Bundle\",\n" + |
| 166 | " \"desc\": \"Provides internet, firewall and parental control functions.\"\n" + |
| 167 | " }\n" + |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 168 | " ]\n" + |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 169 | "}\n"; |
| 170 | |
| 171 | private static final String FAMILY_BUNDLE_JSON = "{\n" + |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 172 | " \"bundle\": {\n" + |
| 173 | " \"id\": \"family\",\n" + |
| 174 | " \"name\": \"Family Bundle\",\n" + |
| 175 | " \"desc\": \"Provides internet, firewall and parental control functions.\",\n" + |
| 176 | " \"functions\": [\n" + |
| 177 | " {\n" + |
| 178 | " \"id\": \"internet\",\n" + |
| 179 | " \"name\": \"Internet\",\n" + |
| 180 | " \"desc\": \"Basic internet connectivity.\",\n" + |
| 181 | " \"params\": {}\n" + |
| 182 | " },\n" + |
| 183 | " {\n" + |
| 184 | " \"id\": \"firewall\",\n" + |
| 185 | " \"name\": \"Firewall\",\n" + |
| 186 | " \"desc\": \"Normal firewall protection.\",\n" + |
| 187 | " \"params\": {}\n" + |
| 188 | " },\n" + |
| 189 | " {\n" + |
| 190 | " \"id\": \"url_filter\",\n" + |
| 191 | " \"name\": \"Parental Control\",\n" + |
| 192 | " \"desc\": \"Variable levels of URL filtering.\",\n" + |
| 193 | " \"params\": {\n" + |
| 194 | " \"level\": \"PG\",\n" + |
| 195 | " \"levels\": [\n" + |
| 196 | " \"PG\",\n" + |
| 197 | " \"PG_13\",\n" + |
| 198 | " \"R\"\n" + |
| 199 | " ]\n" + |
| 200 | " }\n" + |
| 201 | " }\n" + |
| 202 | " ]\n" + |
| 203 | " },\n" + |
| 204 | " \"bundles\": [\n" + |
| 205 | " {\n" + |
| 206 | " \"id\": \"basic\",\n" + |
| 207 | " \"name\": \"Basic Bundle\",\n" + |
| 208 | " \"desc\": \"Provides basic internet and firewall functions.\"\n" + |
| 209 | " },\n" + |
| 210 | " {\n" + |
| 211 | " \"id\": \"family\",\n" + |
| 212 | " \"name\": \"Family Bundle\",\n" + |
| 213 | " \"desc\": \"Provides internet, firewall and parental control functions.\"\n" + |
| 214 | " }\n" + |
| 215 | " ]\n" + |
| 216 | "}\n"; |
| 217 | |
| 218 | private static final String USERS_BASIC = "{\n" + |
| 219 | " \"users\": [\n" + |
| 220 | " {\n" + |
| 221 | " \"id\": 1,\n" + |
| 222 | " \"name\": \"Mom's MacBook\",\n" + |
| 223 | " \"mac\": \"010203040506\",\n" + |
| 224 | " \"profile\": { }\n" + |
| 225 | " },\n" + |
| 226 | " {\n" + |
| 227 | " \"id\": 2,\n" + |
| 228 | " \"name\": \"Dad's iPad\",\n" + |
| 229 | " \"mac\": \"010203040507\",\n" + |
| 230 | " \"profile\": { }\n" + |
| 231 | " },\n" + |
| 232 | " {\n" + |
| 233 | " \"id\": 3,\n" + |
| 234 | " \"name\": \"Dick's laptop\",\n" + |
| 235 | " \"mac\": \"010203040508\",\n" + |
| 236 | " \"profile\": { }\n" + |
| 237 | " },\n" + |
| 238 | " {\n" + |
| 239 | " \"id\": 4,\n" + |
| 240 | " \"name\": \"Jane's laptop\",\n" + |
| 241 | " \"mac\": \"010203040509\",\n" + |
| 242 | " \"profile\": { }\n" + |
| 243 | " }\n" + |
| 244 | " ]\n" + |
| 245 | "}\n"; |
| 246 | |
| 247 | private static final String USERS_FAMILY = "{\n" + |
| 248 | " \"users\": [\n" + |
| 249 | " {\n" + |
| 250 | " \"id\": 1,\n" + |
| 251 | " \"name\": \"Mom's MacBook\",\n" + |
| 252 | " \"mac\": \"010203040506\",\n" + |
| 253 | " \"profile\": {\n" + |
| 254 | " \"url_filter\": {\n" + |
| 255 | " \"level\": \"PG\"\n" + |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 256 | " }\n" + |
| 257 | " }\n" + |
Simon Hunt | 6c2555b | 2015-05-21 18:17:56 -0700 | [diff] [blame] | 258 | " },\n" + |
| 259 | " {\n" + |
| 260 | " \"id\": 2,\n" + |
| 261 | " \"name\": \"Dad's iPad\",\n" + |
| 262 | " \"mac\": \"010203040507\",\n" + |
| 263 | " \"profile\": {\n" + |
| 264 | " \"url_filter\": {\n" + |
| 265 | " \"level\": \"PG\"\n" + |
| 266 | " }\n" + |
| 267 | " }\n" + |
| 268 | " },\n" + |
| 269 | " {\n" + |
| 270 | " \"id\": 3,\n" + |
| 271 | " \"name\": \"Dick's laptop\",\n" + |
| 272 | " \"mac\": \"010203040508\",\n" + |
| 273 | " \"profile\": {\n" + |
| 274 | " \"url_filter\": {\n" + |
| 275 | " \"level\": \"PG\"\n" + |
| 276 | " }\n" + |
| 277 | " }\n" + |
| 278 | " },\n" + |
| 279 | " {\n" + |
| 280 | " \"id\": 4,\n" + |
| 281 | " \"name\": \"Jane's laptop\",\n" + |
| 282 | " \"mac\": \"010203040509\",\n" + |
| 283 | " \"profile\": {\n" + |
| 284 | " \"url_filter\": {\n" + |
| 285 | " \"level\": \"PG\"\n" + |
| 286 | " }\n" + |
| 287 | " }\n" + |
| 288 | " }\n" + |
Simon Hunt | 41b943e | 2015-05-21 13:52:01 -0700 | [diff] [blame] | 289 | " ]\n" + |
| 290 | "}\n"; |
| 291 | } |