Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.device; |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 17 | |
| 18 | import org.junit.Test; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 19 | import org.onlab.packet.ChassisId; |
Pavel Likin | 9d49f54 | 2015-12-13 15:04:55 +0300 | [diff] [blame] | 20 | import org.onosproject.net.DefaultAnnotations; |
Ray Milkey | a6b2112 | 2016-10-24 17:18:35 -0700 | [diff] [blame] | 21 | import org.onosproject.net.Device; |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 22 | |
| 23 | import java.net.URI; |
| 24 | |
Ray Milkey | a6b2112 | 2016-10-24 17:18:35 -0700 | [diff] [blame] | 25 | import com.google.common.testing.EqualsTester; |
| 26 | |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 27 | import static org.junit.Assert.assertEquals; |
| 28 | import static org.junit.Assert.assertTrue; |
Ray Milkey | a6b2112 | 2016-10-24 17:18:35 -0700 | [diff] [blame] | 29 | import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutableBaseClass; |
| 30 | import static org.onosproject.net.Device.Type.ROADM; |
| 31 | import static org.onosproject.net.Device.Type.ROUTER; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 32 | import static org.onosproject.net.Device.Type.SWITCH; |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Test of the default device description. |
| 36 | */ |
| 37 | public class DefaultDeviceDescriptionTest { |
| 38 | |
Ray Milkey | a6b2112 | 2016-10-24 17:18:35 -0700 | [diff] [blame] | 39 | |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 40 | 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"; |
alshabib | 7911a05 | 2014-10-16 17:49:37 -0700 | [diff] [blame] | 45 | private static final ChassisId CID = new ChassisId(); |
Pavel Likin | 9d49f54 | 2015-12-13 15:04:55 +0300 | [diff] [blame] | 46 | private static final DefaultAnnotations DA = |
| 47 | DefaultAnnotations.builder().set("Key", "Value").build(); |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 48 | |
Ray Milkey | a6b2112 | 2016-10-24 17:18:35 -0700 | [diff] [blame] | 49 | 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 | |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 83 | @Test |
| 84 | public void basics() { |
| 85 | DeviceDescription device = |
Pavel Likin | 9d49f54 | 2015-12-13 15:04:55 +0300 | [diff] [blame] | 86 | new DefaultDeviceDescription(DURI, SWITCH, MFR, HW, SW, SN, CID, DA); |
Ray Milkey | a6b2112 | 2016-10-24 17:18:35 -0700 | [diff] [blame] | 87 | 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); |
tom | 3065d12 | 2014-09-03 21:56:43 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | } |