Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -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 | |
Ray Milkey | d29bc1c | 2018-03-13 11:57:11 -0700 | [diff] [blame] | 17 | package org.onosproject.net.config.basics; |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import com.google.common.annotations.Beta; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.packet.VlanId; |
| 25 | import org.onosproject.TestApplicationId; |
| 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.CoreService; |
| 28 | import org.onosproject.net.config.Config; |
| 29 | import org.onosproject.net.config.ConfigApplyDelegate; |
| 30 | |
| 31 | import java.io.InputStream; |
| 32 | |
| 33 | import static org.hamcrest.Matchers.is; |
Ray Milkey | d29bc1c | 2018-03-13 11:57:11 -0700 | [diff] [blame] | 34 | import static org.junit.Assert.assertFalse; |
| 35 | import static org.junit.Assert.assertNotNull; |
| 36 | import static org.junit.Assert.assertThat; |
| 37 | import static org.junit.Assert.assertTrue; |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Tests for class {@link McastConfig}. |
| 41 | */ |
| 42 | @Beta |
| 43 | public class McastConfigTest { |
| 44 | private static final TestApplicationId APP_ID = |
| 45 | new TestApplicationId(CoreService.CORE_APP_NAME); |
| 46 | private McastConfig config; |
| 47 | private McastConfig invalidConfig; |
Esin Karaman | 66f8c61 | 2020-03-12 14:22:46 +0000 | [diff] [blame^] | 48 | private McastConfig configForInnerVlan; |
| 49 | private McastConfig invalidConfigForInnerVlan; |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 50 | |
| 51 | private static final VlanId INGRESS_VLAN_1 = VlanId.NONE; |
| 52 | private static final VlanId EGRESS_VLAN_1 = VlanId.NONE; |
Esin Karaman | 66f8c61 | 2020-03-12 14:22:46 +0000 | [diff] [blame^] | 53 | private static final VlanId EGRESS_INNER_VLAN_1 = VlanId.NONE; |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 54 | private static final VlanId INGRESS_VLAN_2 = VlanId.vlanId((short) 100); |
| 55 | private static final VlanId EGRESS_VLAN_2 = VlanId.vlanId((short) 100); |
Esin Karaman | 66f8c61 | 2020-03-12 14:22:46 +0000 | [diff] [blame^] | 56 | private static final VlanId EGRESS_INNER_VLAN_2 = VlanId.vlanId((short) 100); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Initialize test related variables. |
| 60 | * |
| 61 | * @throws Exception |
| 62 | */ |
| 63 | @Before |
| 64 | public void setUp() throws Exception { |
| 65 | InputStream jsonStream = McastConfigTest.class |
| 66 | .getResourceAsStream("/mcast-config.json"); |
| 67 | InputStream invalidJsonStream = McastConfigTest.class |
| 68 | .getResourceAsStream("/mcast-config-invalid.json"); |
Esin Karaman | 66f8c61 | 2020-03-12 14:22:46 +0000 | [diff] [blame^] | 69 | InputStream jsonStreamForInnerVlan = McastConfigTest.class |
| 70 | .getResourceAsStream("/mcast-config-inner.json"); |
| 71 | InputStream invalidJsonStreamForInnerVlan = McastConfigTest.class |
| 72 | .getResourceAsStream("/mcast-config-invalid-inner.json"); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 73 | |
| 74 | ApplicationId subject = APP_ID; |
| 75 | String key = CoreService.CORE_APP_NAME; |
| 76 | ObjectMapper mapper = new ObjectMapper(); |
| 77 | JsonNode jsonNode = mapper.readTree(jsonStream); |
| 78 | JsonNode invalidJsonNode = mapper.readTree(invalidJsonStream); |
Esin Karaman | 66f8c61 | 2020-03-12 14:22:46 +0000 | [diff] [blame^] | 79 | JsonNode jsonNodeForInnerVlan = mapper.readTree(jsonStreamForInnerVlan); |
| 80 | JsonNode invalidJsonNodeForInnerVlan = mapper.readTree(invalidJsonStreamForInnerVlan); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 81 | ConfigApplyDelegate delegate = new MockDelegate(); |
| 82 | |
| 83 | config = new McastConfig(); |
| 84 | config.init(subject, key, jsonNode, mapper, delegate); |
| 85 | invalidConfig = new McastConfig(); |
| 86 | invalidConfig.init(subject, key, invalidJsonNode, mapper, delegate); |
Esin Karaman | 66f8c61 | 2020-03-12 14:22:46 +0000 | [diff] [blame^] | 87 | configForInnerVlan = new McastConfig(); |
| 88 | configForInnerVlan.init(subject, key, jsonNodeForInnerVlan, mapper, delegate); |
| 89 | invalidConfigForInnerVlan = new McastConfig(); |
| 90 | invalidConfigForInnerVlan.init(subject, key, invalidJsonNodeForInnerVlan, mapper, delegate); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Tests config validity. |
| 95 | * |
| 96 | * @throws Exception |
| 97 | */ |
| 98 | @Test |
| 99 | public void isValid() throws Exception { |
| 100 | assertTrue(config.isValid()); |
| 101 | assertFalse(invalidConfig.isValid()); |
Esin Karaman | 66f8c61 | 2020-03-12 14:22:46 +0000 | [diff] [blame^] | 102 | assertTrue(configForInnerVlan.isValid()); |
| 103 | assertFalse(invalidConfigForInnerVlan.isValid()); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Tests ingress VLAN getter. |
| 108 | * |
| 109 | * @throws Exception |
| 110 | */ |
| 111 | @Test |
| 112 | public void ingressVlan() throws Exception { |
| 113 | VlanId ingressVlan = config.ingressVlan(); |
| 114 | assertNotNull("ingressVlan should not be null", ingressVlan); |
| 115 | assertThat(ingressVlan, is(INGRESS_VLAN_1)); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Tests ingress VLAN setter. |
| 120 | * |
| 121 | * @throws Exception |
| 122 | */ |
| 123 | @Test |
| 124 | public void setIngressVlan() throws Exception { |
| 125 | config.setIngressVlan(INGRESS_VLAN_2); |
| 126 | |
| 127 | VlanId ingressVlan = config.ingressVlan(); |
| 128 | assertNotNull("ingressVlan should not be null", ingressVlan); |
| 129 | assertThat(ingressVlan, is(INGRESS_VLAN_2)); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Tests egress VLAN getter. |
| 134 | * |
| 135 | * @throws Exception |
| 136 | */ |
| 137 | @Test |
| 138 | public void egressVlan() throws Exception { |
| 139 | VlanId egressVlan = config.egressVlan(); |
| 140 | assertNotNull("egressVlan should not be null", egressVlan); |
| 141 | assertThat(egressVlan, is(EGRESS_VLAN_1)); |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Tests egress VLAN setter. |
| 146 | * |
| 147 | * @throws Exception |
| 148 | */ |
| 149 | @Test |
| 150 | public void setEgressVlan() throws Exception { |
| 151 | config.setEgressVlan(EGRESS_VLAN_2); |
| 152 | |
| 153 | VlanId egressVlan = config.egressVlan(); |
| 154 | assertNotNull("egressVlan should not be null", egressVlan); |
| 155 | assertThat(egressVlan, is(EGRESS_VLAN_2)); |
| 156 | } |
| 157 | |
Esin Karaman | 66f8c61 | 2020-03-12 14:22:46 +0000 | [diff] [blame^] | 158 | /** |
| 159 | * Tests egress inner VLAN getter. |
| 160 | * |
| 161 | * @throws Exception |
| 162 | */ |
| 163 | @Test |
| 164 | public void egressInnerVlan() { |
| 165 | VlanId egressInnerVlan = config.egressInnerVlan(); |
| 166 | assertNotNull("egressInnerVlan should not be null", egressInnerVlan); |
| 167 | assertThat(egressInnerVlan, is(EGRESS_INNER_VLAN_1)); |
| 168 | } |
| 169 | |
| 170 | |
| 171 | /** |
| 172 | * Tests egress inner VLAN setter. |
| 173 | * |
| 174 | * @throws Exception |
| 175 | */ |
| 176 | @Test |
| 177 | public void setEgressInnerVlan() { |
| 178 | config.setEgressInnerVlan(EGRESS_INNER_VLAN_2); |
| 179 | |
| 180 | VlanId egressInnerVlan = config.egressInnerVlan(); |
| 181 | assertNotNull("egressInnerVlan should not be null", egressInnerVlan); |
| 182 | assertThat(egressInnerVlan, is(EGRESS_INNER_VLAN_2)); |
| 183 | } |
| 184 | |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 185 | private class MockDelegate implements ConfigApplyDelegate { |
| 186 | @Override |
| 187 | public void onApply(Config config) { |
| 188 | } |
| 189 | } |
Ray Milkey | 6c1f0f0 | 2017-08-15 11:02:29 -0700 | [diff] [blame] | 190 | } |