Andreas Pantelopoulos | 1941641 | 2018-06-12 13:19:52 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Foundation |
| 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; |
| 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.onosproject.core.CoreService; |
| 25 | import org.onosproject.net.ConnectPoint; |
| 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.net.PortNumber; |
| 28 | import org.onosproject.net.config.Config; |
| 29 | import org.onosproject.net.config.ConfigApplyDelegate; |
| 30 | import org.onosproject.net.config.InvalidFieldException; |
| 31 | |
| 32 | import java.io.InputStream; |
| 33 | import java.net.URI; |
| 34 | |
| 35 | import static org.junit.Assert.*; |
| 36 | |
| 37 | /** |
| 38 | * Tests for class {@link HostLearningConfig}. |
| 39 | */ |
| 40 | @Beta |
| 41 | public class HostLearningConfigTest { |
| 42 | private ConnectPoint cp; |
| 43 | private HostLearningConfig config; |
| 44 | private HostLearningConfig invalidConfig; |
| 45 | |
| 46 | /** |
| 47 | * Initialize test related variables. |
| 48 | * |
| 49 | * @throws Exception |
| 50 | */ |
| 51 | @Before |
| 52 | public void setUp() throws Exception { |
| 53 | InputStream jsonStream = HostLearningConfigTest.class |
| 54 | .getResourceAsStream("/host-learning-config.json"); |
| 55 | InputStream invalidJsonStream = HostLearningConfigTest.class |
| 56 | .getResourceAsStream("/host-learning-config-invalid.json"); |
| 57 | |
| 58 | cp = new ConnectPoint(DeviceId.deviceId(new URI("of:0000000000000202")), PortNumber.portNumber((long) 5)); |
| 59 | ConnectPoint subject = cp; |
| 60 | String key = CoreService.CORE_APP_NAME; |
| 61 | ObjectMapper mapper = new ObjectMapper(); |
| 62 | JsonNode jsonNode = mapper.readTree(jsonStream); |
| 63 | JsonNode invalidJsonNode = mapper.readTree(invalidJsonStream); |
| 64 | ConfigApplyDelegate delegate = new MockDelegate(); |
| 65 | |
| 66 | config = new HostLearningConfig(); |
| 67 | config.init(subject, key, jsonNode, mapper, delegate); |
| 68 | invalidConfig = new HostLearningConfig(); |
| 69 | invalidConfig.init(subject, key, invalidJsonNode, mapper, delegate); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Tests config validity. |
| 74 | * |
| 75 | */ |
| 76 | @Test(expected = InvalidFieldException.class) |
| 77 | public void isValid() { |
| 78 | assertTrue(config.isValid()); |
| 79 | assertFalse(invalidConfig.isValid()); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Tests enabled setter. |
| 84 | * |
| 85 | */ |
| 86 | @Test |
| 87 | public void testEnableLearning() { |
| 88 | config.setEnabled("false"); |
| 89 | assertTrue(!config.hostLearningEnabled()); |
| 90 | config.setEnabled("true"); |
| 91 | assertTrue(config.hostLearningEnabled()); |
| 92 | } |
| 93 | |
| 94 | private class MockDelegate implements ConfigApplyDelegate { |
| 95 | @Override |
| 96 | public void onApply(Config config) { |
| 97 | } |
| 98 | } |
| 99 | } |