blob: 970e6a0dcc76867b89ade29f4aa96892a5c16173 [file] [log] [blame]
Sean Condon0e89bda2017-03-21 14:23:19 +00001/*
2 * Copyright 2017-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 */
16package org.onosproject.incubator.net.l2monitoring.cfm.identifier;
17
18import static org.junit.Assert.assertEquals;
19import static org.junit.Assert.assertFalse;
20import static org.junit.Assert.assertTrue;
21import static org.junit.Assert.fail;
22
23import org.junit.Before;
24import org.junit.Test;
25import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException;
26
27public class MaIdShortTest {
28
29 MaIdShort maId1;
30 MaIdShort maId2;
31 MaIdShort maId3;
32 MaIdShort maId4;
33 MaIdShort maId5;
34
35 @Before
36 public void setUp() throws Exception, CfmConfigException {
37 maId1 = MaIdCharStr.asMaId("ma-1-1");
38 maId2 = MaIdPrimaryVid.asMaId((short) 1234);
39 maId3 = MaId2Octet.asMaId(33333);
40 maId4 = MaIdRfc2685VpnId.asMaIdHex("aa:BB:cc:DD:ee:ff:11");
41 maId5 = MaIdIccY1731.asMaId("ABC", "DEFGHIJK");
42 }
43
44 @Test
45 public void testMaName() {
46 assertEquals("ma-1-1", maId1.maName());
47 assertEquals("1234", maId2.maName());
48 assertEquals("33333", maId3.maName());
49 assertEquals("aa:BB:cc:DD:ee:ff:11".toLowerCase(), maId4.maName());
50 assertEquals("ABCDEFGHIJK", maId5.maName());
51 }
52
53 @Test
54 public void testGetNameLength() {
55 assertEquals(6, maId1.getNameLength());
56 assertEquals(2, maId2.getNameLength());
57 assertEquals(2, maId3.getNameLength());
58 assertEquals(7, maId4.getNameLength());
59 assertEquals(11, maId5.getNameLength());
60 }
61
62 @Test
63 public void testMaNameWrong() {
64 try {
65 MaIdCharStr.asMaId(null);
66 fail("Expected exception");
67 } catch (IllegalArgumentException e) {
68 assertTrue(e.getMessage().contains("MA Name must follow pattern"));
69 }
70
71 try {
72 MaIdCharStr.asMaId("");
73 fail("Expected exception");
74 } catch (IllegalArgumentException e) {
75 assertTrue(e.getMessage().contains("MA Name must follow pattern"));
76 }
77
78 try {
79 MaIdCharStr.asMaId("This is a name with spaces - not allowed");
80 fail("Expected exception");
81 } catch (IllegalArgumentException e) {
82 assertTrue(e.getMessage().contains("MA Name must follow pattern"));
83 }
84
85 try {
86 MaIdCharStr.asMaId("This-name-is-too-long-at-forty-eight-characters-in-total");
87 fail("Expected exception");
88 } catch (IllegalArgumentException e) {
89 assertTrue(e.getMessage().contains("MA Name must follow pattern"));
90 }
91
92
93 try {
94 MaIdPrimaryVid.asMaId("abcdef");
95 fail("Expected exception");
96 } catch (IllegalArgumentException e) {
97 assertTrue(e.getMessage().contains("MA Name must be numeric"));
98 }
99
100 try {
101 MaIdPrimaryVid.asMaId("-20");
102 fail("Expected exception");
103 } catch (IllegalArgumentException e) {
104 assertTrue(e.getMessage().contains("MA Id must be between 0 and 4095"));
105 }
106
107 try {
108 MaIdPrimaryVid.asMaId("5000");
109 fail("Expected exception");
110 } catch (IllegalArgumentException e) {
111 assertTrue(e.getMessage().contains("MA Id must be between 0 and 4095"));
112 }
113
114
115 try {
116 MaId2Octet.asMaId("abcdef");
117 fail("Expected exception");
118 } catch (IllegalArgumentException e) {
119 assertTrue(e.getMessage().contains("MA Name must be numeric"));
120 }
121
122 try {
123 MaId2Octet.asMaId("-20");
124 fail("Expected exception");
125 } catch (IllegalArgumentException e) {
126 assertTrue(e.getMessage().contains("MA Id must be between 0 and 65535"));
127 }
128
129 try {
130 MaId2Octet.asMaId("70000");
131 fail("Expected exception");
132 } catch (IllegalArgumentException e) {
133 assertTrue(e.getMessage().contains("MA Id must be between 0 and 65535"));
134 }
135
136
137 try {
138 MaIdRfc2685VpnId.asMaIdHex("aa:bb:cc:dd:ee:ff"); //Need 7
139 fail("Expected exception");
140 } catch (IllegalArgumentException e) {
141 assertTrue(e.getMessage().contains("MA Name must follow pattern"));
142 }
143
144 try {
145 MaIdIccY1731.asMaId("ABCDEFG", "HIJKL"); //7 too long for ICC
146 fail("Expected exception");
147 } catch (IllegalArgumentException e) {
148 assertTrue(e.getMessage().contains("ICC part must follow pattern"));
149 }
150
151 try {
152 MaIdIccY1731.asMaId("A", "BCDEFGHIJKLMNO"); //14 too long for UMC
153 fail("Expected exception");
154 } catch (IllegalArgumentException e) {
155 assertTrue(e.getMessage().contains("UMC part must follow pattern"));
156 }
157
158 }
159
160 @Test
161 public void testEquals() {
162 //For char string
163 assertFalse(maId1.equals(null));
164 assertFalse(maId1.equals(new String("test")));
165
166 assertTrue(maId1.equals(maId1));
167 assertFalse(maId1.equals(maId2));
168
169 //For primary vid
170 assertFalse(maId2.equals(null));
171 assertFalse(maId2.equals(new String("test")));
172
173 assertTrue(maId2.equals(maId2));
174 assertFalse(maId2.equals(maId1));
175
176 //For 2 octet
177 assertFalse(maId3.equals(null));
178 assertFalse(maId3.equals(new String("test")));
179
180 assertTrue(maId3.equals(maId3));
181 assertFalse(maId3.equals(maId1));
182
183 //rfc2685vpn
184 assertFalse(maId4.equals(null));
185 assertFalse(maId4.equals(new String("test")));
186
187 assertTrue(maId4.equals(maId4));
188 assertFalse(maId4.equals(maId1));
189
190
191 //ICC-Y1731
192 assertFalse(maId5.equals(null));
193 assertFalse(maId5.equals(new String("test")));
194
195 assertTrue(maId5.equals(maId5));
196 assertFalse(maId5.equals(maId1));
197}
198
199 @Test
200 public void testHashCode() {
201 assertEquals(maId1.hashCode(), maId1.hashCode());
202 assertEquals(maId2.hashCode(), maId2.hashCode());
203 assertEquals(maId3.hashCode(), maId3.hashCode());
204 assertEquals(maId4.hashCode(), maId4.hashCode());
205 assertEquals(maId5.hashCode(), maId5.hashCode());
206 }
207}