Open Kilda Java Documentation
SwitchIdTest.java
Go to the documentation of this file.
1 /* Copyright 2018 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.messaging.model;
17 
18 import org.junit.Assert;
19 import org.junit.Rule;
20 import org.junit.Test;
21 import org.junit.rules.ExpectedException;
22 
23 public class SwitchIdTest {
24 
25  @Rule
26  public ExpectedException thrown = ExpectedException.none();
27 
28  @Test
30  thrown.expect(IllegalArgumentException.class);
31  thrown.expectMessage(String.format(
32  "Illegal offset value %d (expect offset > 0 and offset %% 2 == 0 and offset < hex.length)", -1));
33 
34  new SwitchId("00").colonSeparatedBytes(new char[] {'t', 'e', 's', 't'}, -1);
35  }
36 
37  @Test
39  thrown.expect(IllegalArgumentException.class);
40  thrown.expectMessage(String.format(
41  "Illegal offset value %d (expect offset > 0 and offset %% 2 == 0 and offset < hex.length)", 3));
42 
43  new SwitchId("00").colonSeparatedBytes(new char[] {'t', 'e', 's', 't'}, 3);
44  }
45 
46  @Test
48  thrown.expect(IllegalArgumentException.class);
49  thrown.expectMessage(String.format(
50  "Illegal offset value %d (expect offset > 0 and offset %% 2 == 0 and offset < hex.length)", 6));
51 
52  new SwitchId("00").colonSeparatedBytes(new char[] {'t', 'e', 's', 't'}, 6);
53  }
54 
55  @Test
57  String switchIdString = "fe:dc:ba:98:76:54:32:10";
58  SwitchId switchId = new SwitchId(switchIdString);
59 
60  char[] hexArray = String.format("%016x", switchId.toLong()).toCharArray();
61 
62  Assert.assertEquals(switchIdString, switchId.colonSeparatedBytes(hexArray, 0));
63  Assert.assertEquals(switchIdString.substring(3), switchId.colonSeparatedBytes(hexArray, 2));
64  Assert.assertEquals(switchIdString.substring(6), switchId.colonSeparatedBytes(hexArray, 4));
65  Assert.assertEquals(switchIdString.substring(9), switchId.colonSeparatedBytes(hexArray, 6));
66  Assert.assertEquals(switchIdString.substring(12), switchId.colonSeparatedBytes(hexArray, 8));
67  Assert.assertEquals(switchIdString.substring(15), switchId.colonSeparatedBytes(hexArray, 10));
68  Assert.assertEquals(switchIdString.substring(18), switchId.colonSeparatedBytes(hexArray, 12));
69  Assert.assertEquals(switchIdString.substring(21), switchId.colonSeparatedBytes(hexArray, 14));
70  }
71 }