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; |
| 48 | |
| 49 | private static final VlanId INGRESS_VLAN_1 = VlanId.NONE; |
| 50 | private static final VlanId EGRESS_VLAN_1 = VlanId.NONE; |
| 51 | private static final VlanId INGRESS_VLAN_2 = VlanId.vlanId((short) 100); |
| 52 | private static final VlanId EGRESS_VLAN_2 = VlanId.vlanId((short) 100); |
| 53 | |
| 54 | /** |
| 55 | * Initialize test related variables. |
| 56 | * |
| 57 | * @throws Exception |
| 58 | */ |
| 59 | @Before |
| 60 | public void setUp() throws Exception { |
| 61 | InputStream jsonStream = McastConfigTest.class |
| 62 | .getResourceAsStream("/mcast-config.json"); |
| 63 | InputStream invalidJsonStream = McastConfigTest.class |
| 64 | .getResourceAsStream("/mcast-config-invalid.json"); |
| 65 | |
| 66 | ApplicationId subject = APP_ID; |
| 67 | String key = CoreService.CORE_APP_NAME; |
| 68 | ObjectMapper mapper = new ObjectMapper(); |
| 69 | JsonNode jsonNode = mapper.readTree(jsonStream); |
| 70 | JsonNode invalidJsonNode = mapper.readTree(invalidJsonStream); |
| 71 | ConfigApplyDelegate delegate = new MockDelegate(); |
| 72 | |
| 73 | config = new McastConfig(); |
| 74 | config.init(subject, key, jsonNode, mapper, delegate); |
| 75 | invalidConfig = new McastConfig(); |
| 76 | invalidConfig.init(subject, key, invalidJsonNode, mapper, delegate); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Tests config validity. |
| 81 | * |
| 82 | * @throws Exception |
| 83 | */ |
| 84 | @Test |
| 85 | public void isValid() throws Exception { |
| 86 | assertTrue(config.isValid()); |
| 87 | assertFalse(invalidConfig.isValid()); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Tests ingress VLAN getter. |
| 92 | * |
| 93 | * @throws Exception |
| 94 | */ |
| 95 | @Test |
| 96 | public void ingressVlan() throws Exception { |
| 97 | VlanId ingressVlan = config.ingressVlan(); |
| 98 | assertNotNull("ingressVlan should not be null", ingressVlan); |
| 99 | assertThat(ingressVlan, is(INGRESS_VLAN_1)); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Tests ingress VLAN setter. |
| 104 | * |
| 105 | * @throws Exception |
| 106 | */ |
| 107 | @Test |
| 108 | public void setIngressVlan() throws Exception { |
| 109 | config.setIngressVlan(INGRESS_VLAN_2); |
| 110 | |
| 111 | VlanId ingressVlan = config.ingressVlan(); |
| 112 | assertNotNull("ingressVlan should not be null", ingressVlan); |
| 113 | assertThat(ingressVlan, is(INGRESS_VLAN_2)); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Tests egress VLAN getter. |
| 118 | * |
| 119 | * @throws Exception |
| 120 | */ |
| 121 | @Test |
| 122 | public void egressVlan() throws Exception { |
| 123 | VlanId egressVlan = config.egressVlan(); |
| 124 | assertNotNull("egressVlan should not be null", egressVlan); |
| 125 | assertThat(egressVlan, is(EGRESS_VLAN_1)); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Tests egress VLAN setter. |
| 130 | * |
| 131 | * @throws Exception |
| 132 | */ |
| 133 | @Test |
| 134 | public void setEgressVlan() throws Exception { |
| 135 | config.setEgressVlan(EGRESS_VLAN_2); |
| 136 | |
| 137 | VlanId egressVlan = config.egressVlan(); |
| 138 | assertNotNull("egressVlan should not be null", egressVlan); |
| 139 | assertThat(egressVlan, is(EGRESS_VLAN_2)); |
| 140 | } |
| 141 | |
| 142 | private class MockDelegate implements ConfigApplyDelegate { |
| 143 | @Override |
| 144 | public void onApply(Config config) { |
| 145 | } |
| 146 | } |
Ray Milkey | 6c1f0f0 | 2017-08-15 11:02:29 -0700 | [diff] [blame] | 147 | } |