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