blob: 70ef35967fa90b3433d38df1522e2b8c6c365de6 [file] [log] [blame]
Simon Hunt41b943e2015-05-21 13:52:01 -07001/*
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
18package org.onosproject.cord.gui;
19
20import com.fasterxml.jackson.databind.JsonNode;
21import com.fasterxml.jackson.databind.ObjectMapper;
Simon Huntee6a7372015-05-28 14:04:24 -070022import com.fasterxml.jackson.databind.node.ObjectNode;
Simon Hunt41b943e2015-05-21 13:52:01 -070023import org.junit.Before;
Simon Hunt7d02c082015-05-29 12:17:09 -070024import org.junit.Ignore;
Simon Hunt41b943e2015-05-21 13:52:01 -070025import org.junit.Test;
26import org.onosproject.cord.gui.model.BundleFactory;
27import org.onosproject.cord.gui.model.SubscriberUser;
28
29import java.io.IOException;
30import java.util.List;
31
32import static org.junit.Assert.assertEquals;
33import static org.junit.Assert.assertTrue;
34
35/**
36 * Unit tests for {@link CordModelCache}.
37 */
Simon Hunt7d02c082015-05-29 12:17:09 -070038@Ignore("How to test against a live XOS system??")
Simon Hunt41b943e2015-05-21 13:52:01 -070039public 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 Huntee6a7372015-05-28 14:04:24 -070056 ObjectNode node = BundleFactory.toObjectNode(cache.getCurrentBundle());
57 String json = node.toString();
Simon Hunt6c2555b2015-05-21 18:17:56 -070058 System.out.println(json);
Simon Hunt41b943e2015-05-21 13:52:01 -070059 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 Huntee6a7372015-05-28 14:04:24 -070072 ObjectNode node = BundleFactory.toObjectNode(cache.getCurrentBundle());
73 String json = node.toString();
Simon Hunt41b943e2015-05-21 13:52:01 -070074 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 Hunt6c2555b2015-05-21 18:17:56 -070084 @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 Hunt87b157c2015-05-22 12:09:59 -070099 @Test
100 public void setNewLevel() {
101 cache.setCurrentBundle("family");
102 JsonNode node = fromString(cache.jsonUsers());
Simon Huntab5232e2015-05-26 16:59:46 -0700103 assertEquals("wrong level", "G", getMomsLevel(node));
Simon Hunt87b157c2015-05-22 12:09:59 -0700104
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 Hunt41b943e2015-05-21 13:52:01 -0700118 // =============
119
Simon Hunt87b157c2015-05-22 12:09:59 -0700120 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 Hunt41b943e2015-05-21 13:52:01 -0700129 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 Hunt6c2555b2015-05-21 18:17:56 -0700143 " \"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 Hunt41b943e2015-05-21 13:52:01 -0700173 " ]\n" +
Simon Hunt41b943e2015-05-21 13:52:01 -0700174 "}\n";
175
176 private static final String FAMILY_BUNDLE_JSON = "{\n" +
Simon Hunt6c2555b2015-05-21 18:17:56 -0700177 " \"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 Huntab5232e2015-05-26 16:59:46 -0700199 " \"level\": \"G\",\n" +
Simon Hunt6c2555b2015-05-21 18:17:56 -0700200 " \"levels\": [\n" +
Simon Huntab5232e2015-05-26 16:59:46 -0700201 " \"OFF\",\n" +
202 " \"G\",\n" +
Simon Hunt6c2555b2015-05-21 18:17:56 -0700203 " \"PG\",\n" +
204 " \"PG_13\",\n" +
Simon Huntab5232e2015-05-26 16:59:46 -0700205 " \"R\",\n" +
206 " \"NONE\"\n" +
Simon Hunt6c2555b2015-05-21 18:17:56 -0700207 " ]\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 Huntab5232e2015-05-26 16:59:46 -0700263 " \"level\": \"G\"\n" +
Simon Hunt41b943e2015-05-21 13:52:01 -0700264 " }\n" +
265 " }\n" +
Simon Hunt6c2555b2015-05-21 18:17:56 -0700266 " },\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 Huntab5232e2015-05-26 16:59:46 -0700273 " \"level\": \"G\"\n" +
Simon Hunt6c2555b2015-05-21 18:17:56 -0700274 " }\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 Huntab5232e2015-05-26 16:59:46 -0700283 " \"level\": \"G\"\n" +
Simon Hunt6c2555b2015-05-21 18:17:56 -0700284 " }\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 Huntab5232e2015-05-26 16:59:46 -0700293 " \"level\": \"G\"\n" +
Simon Hunt6c2555b2015-05-21 18:17:56 -0700294 " }\n" +
295 " }\n" +
296 " }\n" +
Simon Hunt41b943e2015-05-21 13:52:01 -0700297 " ]\n" +
298 "}\n";
299}