blob: 358c4e101d665747a69049b9078c10ced8cb09b4 [file] [log] [blame]
Simon Huntf59d36b2016-10-04 19:05:53 -07001/*
2 * Copyright 2016-present 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
17package org.onosproject.net.config.basics;
18
19import com.fasterxml.jackson.databind.JsonNode;
Simon Hunt4f3a4072016-10-17 17:52:11 -070020import com.fasterxml.jackson.databind.node.ObjectNode;
Simon Huntf59d36b2016-10-04 19:05:53 -070021import com.google.common.collect.ImmutableSet;
22import org.junit.Before;
23import org.junit.Test;
24import org.onosproject.net.DeviceId;
Simon Hunt4f3a4072016-10-17 17:52:11 -070025import org.onosproject.net.config.InvalidFieldException;
Simon Huntf59d36b2016-10-04 19:05:53 -070026import org.onosproject.net.region.Region;
27
28import java.util.List;
29import java.util.Set;
30
31import static org.junit.Assert.assertEquals;
Simon Hunt4f3a4072016-10-17 17:52:11 -070032import static org.junit.Assert.assertNotNull;
33import static org.junit.Assert.assertNull;
Simon Huntf59d36b2016-10-04 19:05:53 -070034import static org.junit.Assert.assertTrue;
35import static org.onosproject.net.region.RegionId.regionId;
36
37/**
38 * Test class for {@link BasicRegionConfig}.
39 */
40public class BasicRegionConfigTest extends AbstractConfigTest {
41
42 private static final String REGION_JSON = "configs.regions.1.json";
Simon Hunt4f3a4072016-10-17 17:52:11 -070043
44 private static final String NAME = "name";
45 private static final String TYPE = "type";
46 private static final String DEVICES = "devices";
47
Simon Huntf59d36b2016-10-04 19:05:53 -070048 private static final String R1 = "r1";
49 private static final String R2 = "r2";
50 private static final String R3 = "r3";
51
Simon Hunt4f3a4072016-10-17 17:52:11 -070052 private static final String EUROPE = "Europe";
53 private static final String PARIS = "Paris";
54 private static final String AUSTRALIA = "Australia";
55
Simon Huntf59d36b2016-10-04 19:05:53 -070056 private static final Set<DeviceId> R1_DEVS =
57 ImmutableSet.of(dstr("01"), dstr("02"), dstr("03"));
58 private static final Set<DeviceId> R2_DEVS =
59 ImmutableSet.of(dstr("04"), dstr("05"), dstr("06"));
60 private static final Set<DeviceId> R3_DEVS =
61 ImmutableSet.of(dstr("07"), dstr("08"), dstr("09"));
Simon Hunt4f3a4072016-10-17 17:52:11 -070062 private static final Set<DeviceId> ALT_DEVICES =
63 ImmutableSet.of(dstr("0a"), dstr("0b"), dstr("0c"));
Simon Huntf59d36b2016-10-04 19:05:53 -070064
65 private JsonNode data;
Simon Hunt4f3a4072016-10-17 17:52:11 -070066 private BasicRegionConfig cfg;
Simon Huntf59d36b2016-10-04 19:05:53 -070067
68 @Before
69 public void setUp() {
70 data = getTestJson(REGION_JSON);
71 }
72
73 private JsonNode getR(String key) {
74 return data.get("regions").get(key).get("basic");
75 }
76
Simon Hunt4f3a4072016-10-17 17:52:11 -070077 // loads a region config from the test resource file
78 private void loadRegion(String rid) {
79 JsonNode node = getR(rid);
80 print(JSON_LOADED, node);
Simon Huntf59d36b2016-10-04 19:05:53 -070081
Simon Hunt4f3a4072016-10-17 17:52:11 -070082 cfg = new BasicRegionConfig();
83 cfg.init(regionId(rid), rid, node, mapper, delegate);
84 }
Simon Huntf59d36b2016-10-04 19:05:53 -070085
Simon Hunt4f3a4072016-10-17 17:52:11 -070086 private void checkRegion(String expN, Region.Type expT, Set<DeviceId> expD) {
87 print(CHECKING_S, cfg);
88 assertEquals("wrong name", expN, cfg.name());
89 assertEquals("wrong type", expT, cfg.type());
Simon Huntf59d36b2016-10-04 19:05:53 -070090
Simon Hunt4f3a4072016-10-17 17:52:11 -070091 List<DeviceId> devs = cfg.devices();
92 if (expD == null) {
93 assertNull("unexp device list", devs);
94 } else {
95 assertNotNull(devs);
96 assertEquals("wr.size", expD.size(), devs.size());
97 for (DeviceId d : expD) {
98 assertTrue("missing dev: " + d, devs.contains(d));
99 }
Simon Huntf59d36b2016-10-04 19:05:53 -0700100 }
101 }
102
103 @Test
104 public void region1Config() {
Simon Hunt4f3a4072016-10-17 17:52:11 -0700105 loadRegion(R1);
106 checkRegion(EUROPE, Region.Type.CONTINENT, R1_DEVS);
Simon Huntf59d36b2016-10-04 19:05:53 -0700107 }
108
109 @Test
110 public void region2Config() {
Simon Hunt4f3a4072016-10-17 17:52:11 -0700111 loadRegion(R2);
112 checkRegion(PARIS, Region.Type.METRO, R2_DEVS);
Simon Huntf59d36b2016-10-04 19:05:53 -0700113 }
114
115 @Test
116 public void region3Config() {
Simon Hunt4f3a4072016-10-17 17:52:11 -0700117 loadRegion(R3);
Simon Hunt53612212016-12-04 17:19:52 -0800118 checkRegion(R3, null, R3_DEVS);
Simon Huntf59d36b2016-10-04 19:05:53 -0700119 }
120
Simon Hunt4f3a4072016-10-17 17:52:11 -0700121 @Test
122 public void modifyName() {
123 loadRegion(R1);
124 cfg.name(AUSTRALIA);
125 checkRegion(AUSTRALIA, Region.Type.CONTINENT, R1_DEVS);
126 }
127
128 @Test
129 public void clearName() {
130 loadRegion(R1);
Simon Hunt53612212016-12-04 17:19:52 -0800131 checkRegion(EUROPE, Region.Type.CONTINENT, R1_DEVS);
Simon Hunt4f3a4072016-10-17 17:52:11 -0700132 cfg.name(null);
Simon Hunt53612212016-12-04 17:19:52 -0800133 // if the friendly name is cleared, name() returns the identifier
134 checkRegion(R1, Region.Type.CONTINENT, R1_DEVS);
Simon Hunt4f3a4072016-10-17 17:52:11 -0700135 }
136
137 @Test
138 public void modifyType() {
139 loadRegion(R2);
140 cfg.type(Region.Type.CAMPUS);
141 checkRegion(PARIS, Region.Type.CAMPUS, R2_DEVS);
142 }
143
144 @Test
145 public void clearType() {
146 loadRegion(R2);
147 cfg.type(null);
148 checkRegion(PARIS, null, R2_DEVS);
149 }
150
151 @Test
152 public void modifyDevices() {
153 loadRegion(R3);
154 cfg.devices(ALT_DEVICES);
Simon Hunt53612212016-12-04 17:19:52 -0800155 checkRegion(R3, null, ALT_DEVICES);
Simon Hunt4f3a4072016-10-17 17:52:11 -0700156 }
157
158 @Test
159 public void clearDevices() {
160 loadRegion(R3);
161 cfg.devices(null);
Simon Hunt53612212016-12-04 17:19:52 -0800162 checkRegion(R3, null, null);
Simon Hunt4f3a4072016-10-17 17:52:11 -0700163 }
164
165
166 @Test
167 public void sampleValidConfig() {
168 ObjectNode node = new TmpJson()
169 .props(NAME, TYPE)
170 .arrays(DEVICES)
171 .node();
172 cfg = new BasicRegionConfig();
173 cfg.init(regionId(R1), BASIC, node, mapper, delegate);
174
175 assertTrue("not valid: " + cfg, cfg.isValid());
176 }
177
178 @Test(expected = InvalidFieldException.class)
179 public void sampleInvalidConfig() {
180 ObjectNode node = new TmpJson()
181 .props(NAME, TYPE, "foo")
182 .arrays(DEVICES)
183 .node();
184 cfg = new BasicRegionConfig();
185 cfg.init(regionId(R1), BASIC, node, mapper, delegate);
186
187 cfg.isValid();
188 }
Simon Huntf59d36b2016-10-04 19:05:53 -0700189}