blob: 97e4a59183c077133489c868e17e778824013651 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.device;
tom3065d122014-09-03 21:56:43 -070017
18import org.junit.Test;
alshabib7911a052014-10-16 17:49:37 -070019import org.onlab.packet.ChassisId;
Pavel Likin9d49f542015-12-13 15:04:55 +030020import org.onosproject.net.DefaultAnnotations;
Ray Milkeya6b21122016-10-24 17:18:35 -070021import org.onosproject.net.Device;
tom3065d122014-09-03 21:56:43 -070022
23import java.net.URI;
24
Ray Milkeya6b21122016-10-24 17:18:35 -070025import com.google.common.testing.EqualsTester;
26
tom3065d122014-09-03 21:56:43 -070027import static org.junit.Assert.assertEquals;
28import static org.junit.Assert.assertTrue;
Ray Milkeya6b21122016-10-24 17:18:35 -070029import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass;
30import static org.onosproject.net.Device.Type.ROADM;
31import static org.onosproject.net.Device.Type.ROUTER;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import static org.onosproject.net.Device.Type.SWITCH;
tom3065d122014-09-03 21:56:43 -070033
34/**
35 * Test of the default device description.
36 */
37public class DefaultDeviceDescriptionTest {
38
Ray Milkeya6b21122016-10-24 17:18:35 -070039
tom3065d122014-09-03 21:56:43 -070040 private static final URI DURI = URI.create("of:foo");
41 private static final String MFR = "whitebox";
42 private static final String HW = "1.1.x";
43 private static final String SW = "3.9.1";
44 private static final String SN = "43311-12345";
alshabib7911a052014-10-16 17:49:37 -070045 private static final ChassisId CID = new ChassisId();
Pavel Likin9d49f542015-12-13 15:04:55 +030046 private static final DefaultAnnotations DA =
47 DefaultAnnotations.builder().set("Key", "Value").build();
tom3065d122014-09-03 21:56:43 -070048
Ray Milkeya6b21122016-10-24 17:18:35 -070049 private static final URI DURI2 = URI.create("of:foo2");
50 private static final String MFR2 = "whitebox2";
51 private static final String HW2 = "1.1.x2";
52 private static final String SW2 = "3.9.12";
53 private static final String SN2 = "43311-123452";
54 private static final ChassisId CID2 = new ChassisId(2);
55 private static final DefaultAnnotations DA2 =
56 DefaultAnnotations.builder().set("2ndKey", "2ndValue").build();
57
58 private void checkValues(DeviceDescription device,
59 URI uri,
60 Device.Type type,
61 String manufacturer,
62 String hw,
63 String sw,
64 String serial,
65 String containsUri,
66 long chassisId,
67 String annotationsString,
68 boolean defaultAvailable) {
69 assertEquals("incorrect uri", uri, device.deviceUri());
70 assertEquals("incorrect type", type, device.type());
71 assertEquals("incorrect manufacturer", device.manufacturer(), manufacturer);
72 assertEquals("incorrect hw", device.hwVersion(), hw);
73 assertEquals("incorrect sw", device.swVersion(), sw);
74 assertEquals("incorrect serial", device.serialNumber(), serial);
75 assertTrue("incorrect toString", device.toString().contains(containsUri));
76 assertTrue("Incorrect chassis",
77 device.chassisId() == null ? chassisId == 0 :
78 chassisId == device.chassisId().value());
79 assertTrue("incorrect annotations", device.toString().contains(annotationsString));
80 assertEquals("incorrect default available", defaultAvailable, device.isDefaultAvailable());
81 }
82
tom3065d122014-09-03 21:56:43 -070083 @Test
84 public void basics() {
85 DeviceDescription device =
Pavel Likin9d49f542015-12-13 15:04:55 +030086 new DefaultDeviceDescription(DURI, SWITCH, MFR, HW, SW, SN, CID, DA);
Ray Milkeya6b21122016-10-24 17:18:35 -070087 checkValues(device, DURI, SWITCH, MFR, HW, SW, SN, "uri=of:foo",
88 CID.value(), "Key=Value", true);
89 }
90
91 /**
92 * Tests equals, hashCode, and toString.
93 */
94 @Test
95 public void testEquals() {
96 DeviceDescription device1 =
97 new DefaultDeviceDescription(DURI, SWITCH, MFR, HW, SW, SN, CID, DA);
98 DeviceDescription sameAsDevice1 =
99 new DefaultDeviceDescription(DURI, SWITCH, MFR, HW, SW, SN, CID, DA);
100 DeviceDescription device2 =
101 new DefaultDeviceDescription(DURI2, SWITCH, MFR, HW, SW, SN, CID, DA);
102 DeviceDescription device3 =
103 new DefaultDeviceDescription(DURI, ROUTER, MFR2, HW, SW, SN, CID, DA);
104 DeviceDescription device4 =
105 new DefaultDeviceDescription(DURI, SWITCH, MFR, HW2, SW, SN, CID, DA);
106 DeviceDescription device5 =
107 new DefaultDeviceDescription(DURI, SWITCH, MFR, HW, SW2, SN, CID, DA);
108 DeviceDescription device6 =
109 new DefaultDeviceDescription(DURI, SWITCH, MFR, HW, SW, SN2, CID, DA);
110 DeviceDescription device7 =
111 new DefaultDeviceDescription(DURI, SWITCH, MFR, HW, SW, SN, CID2, DA);
112 DeviceDescription device8 =
113 new DefaultDeviceDescription(DURI, SWITCH, MFR, HW, SW, SN, CID, DA2);
114
115 new EqualsTester()
116 .addEqualityGroup(device1, sameAsDevice1)
117 .addEqualityGroup(device2)
118 .addEqualityGroup(device3)
119 .addEqualityGroup(device4)
120 .addEqualityGroup(device5)
121 .addEqualityGroup(device6)
122 .addEqualityGroup(device7)
123 .addEqualityGroup(device8)
124 .testEquals();
125 }
126
127 /**
128 * Tests base + annotations constructor.
129 */
130 @Test
131 public void testConstructorWithBaseAndAnnotations() {
132 DeviceDescription base =
133 new DefaultDeviceDescription(DURI, SWITCH, MFR, HW, SW, SN, CID, DA);
134 DeviceDescription device = new DefaultDeviceDescription(base, DA2);
135
136 checkValues(device, DURI, SWITCH, MFR, HW, SW, SN, "uri=of:foo",
137 CID.value(), "2ndKey=2ndValue", true);
138 }
139
140 /**
141 * Tests base + type + annotations constructor.
142 */
143 @Test
144 public void testConstructorWithBaseAndType() {
145 DeviceDescription base =
146 new DefaultDeviceDescription(DURI, ROADM, MFR, HW, SW, SN, CID, DA);
147 DeviceDescription device = new DefaultDeviceDescription(base, ROADM, DA);
148
149 checkValues(device, DURI, ROADM, MFR, HW, SW, SN, "uri=of:foo",
150 CID.value(), "Key=Value", true);
151 }
152
153 /**
154 * Tests base + annotations + isDefaultAvailable constructor.
155 */
156 @Test
157 public void testConstructorWithBaseAndIsDefault() {
158 DeviceDescription base =
159 new DefaultDeviceDescription(DURI, SWITCH, MFR, HW, SW, SN, CID, DA);
160 DeviceDescription device = new DefaultDeviceDescription(base, false, DA2);
161
162 checkValues(device, DURI, SWITCH, MFR, HW, SW, SN, "uri=of:foo",
163 CID.value(), "2ndKey=2ndValue", false);
164 }
165
166 /**
167 * Tests empty constructor.
168 */
169 @Test
170 public void testBareConstructor() {
171 DeviceDescription device = new DefaultDeviceDescription();
172
173 checkValues(device, null, null, null, null, null, null, "uri=null",
174 CID.value(), "", true);
175
176 assertEquals("incorrect uri", null, device.deviceUri());
177 assertEquals("incorrect type", null, device.type());
178 assertEquals("incorrect manufacturer", null, device.manufacturer());
179 assertEquals("incorrect hw", null, device.hwVersion());
180 assertEquals("incorrect sw", null, device.swVersion());
181 assertEquals("incorrect serial", null, device.serialNumber());
182 assertEquals("Incorrect chassis", null, device.chassisId());
183 assertEquals("incorrect annotations", null, device.annotations());
184 assertTrue("incorrect default available", device.isDefaultAvailable());
185 }
186
187 /**
188 * Tests immutability.
189 */
190 @Test
191 public void testImmutable() {
192 assertThatClassIsImmutableBaseClass(DefaultDeviceDescription.class);
tom3065d122014-09-03 21:56:43 -0700193 }
194
195}