Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -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 | package org.onosproject.net.config.basics; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onosproject.net.DeviceId; |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 25 | import org.onosproject.net.config.InvalidFieldException; |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 26 | import org.onosproject.net.region.Region; |
| 27 | |
| 28 | import java.util.List; |
| 29 | import java.util.Set; |
| 30 | |
| 31 | import static org.junit.Assert.assertEquals; |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 32 | import static org.junit.Assert.assertNotNull; |
| 33 | import static org.junit.Assert.assertNull; |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 34 | import static org.junit.Assert.assertTrue; |
| 35 | import static org.onosproject.net.region.RegionId.regionId; |
| 36 | |
| 37 | /** |
| 38 | * Test class for {@link BasicRegionConfig}. |
| 39 | */ |
| 40 | public class BasicRegionConfigTest extends AbstractConfigTest { |
| 41 | |
| 42 | private static final String REGION_JSON = "configs.regions.1.json"; |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 43 | |
| 44 | private static final String NAME = "name"; |
| 45 | private static final String TYPE = "type"; |
| 46 | private static final String DEVICES = "devices"; |
| 47 | |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 48 | private static final String R1 = "r1"; |
| 49 | private static final String R2 = "r2"; |
| 50 | private static final String R3 = "r3"; |
| 51 | |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 52 | private static final String EUROPE = "Europe"; |
| 53 | private static final String PARIS = "Paris"; |
| 54 | private static final String AUSTRALIA = "Australia"; |
| 55 | |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 56 | 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 Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 62 | private static final Set<DeviceId> ALT_DEVICES = |
| 63 | ImmutableSet.of(dstr("0a"), dstr("0b"), dstr("0c")); |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 64 | |
| 65 | private JsonNode data; |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 66 | private BasicRegionConfig cfg; |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 67 | |
| 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 Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 77 | // 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 Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 81 | |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 82 | cfg = new BasicRegionConfig(); |
| 83 | cfg.init(regionId(rid), rid, node, mapper, delegate); |
| 84 | } |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 85 | |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 86 | 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 Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 90 | |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 91 | 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 Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | @Test |
| 104 | public void region1Config() { |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 105 | loadRegion(R1); |
| 106 | checkRegion(EUROPE, Region.Type.CONTINENT, R1_DEVS); |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | @Test |
| 110 | public void region2Config() { |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 111 | loadRegion(R2); |
| 112 | checkRegion(PARIS, Region.Type.METRO, R2_DEVS); |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | @Test |
| 116 | public void region3Config() { |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 117 | loadRegion(R3); |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 118 | checkRegion(R3, null, R3_DEVS); |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 121 | @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 Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 131 | checkRegion(EUROPE, Region.Type.CONTINENT, R1_DEVS); |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 132 | cfg.name(null); |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 133 | // if the friendly name is cleared, name() returns the identifier |
| 134 | checkRegion(R1, Region.Type.CONTINENT, R1_DEVS); |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 135 | } |
| 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 Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 155 | checkRegion(R3, null, ALT_DEVICES); |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | @Test |
| 159 | public void clearDevices() { |
| 160 | loadRegion(R3); |
| 161 | cfg.devices(null); |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 162 | checkRegion(R3, null, null); |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 163 | } |
| 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 Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 189 | } |