blob: 21467c5cd67ee693ab1321f2545e5aec2ecfce5b [file] [log] [blame]
Andreas Pantelopoulos19416412018-06-12 13:19:52 -07001/*
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
17package org.onosproject.net.config.basics;
18
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.ObjectMapper;
21import com.google.common.annotations.Beta;
22import org.junit.Before;
23import org.junit.Test;
24import org.onosproject.core.CoreService;
25import org.onosproject.net.ConnectPoint;
26import org.onosproject.net.DeviceId;
27import org.onosproject.net.PortNumber;
28import org.onosproject.net.config.Config;
29import org.onosproject.net.config.ConfigApplyDelegate;
30import org.onosproject.net.config.InvalidFieldException;
31
32import java.io.InputStream;
33import java.net.URI;
34
35import static org.junit.Assert.*;
36
37/**
38 * Tests for class {@link HostLearningConfig}.
39 */
40@Beta
41public 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}