Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame^] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.incubator.net.l2monitoring.cfm.identifier; |
| 17 | |
| 18 | import static org.junit.Assert.assertEquals; |
| 19 | import static org.junit.Assert.assertFalse; |
| 20 | import static org.junit.Assert.assertNull; |
| 21 | import static org.junit.Assert.assertTrue; |
| 22 | import static org.junit.Assert.fail; |
| 23 | |
| 24 | import org.junit.Before; |
| 25 | import org.junit.Test; |
| 26 | import org.onlab.packet.MacAddress; |
| 27 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException; |
| 28 | |
| 29 | import com.google.common.net.InternetDomainName; |
| 30 | |
| 31 | public class MdIdTest { |
| 32 | |
| 33 | MdId mdId1; |
| 34 | MdId mdId2; |
| 35 | MdId mdId3; |
| 36 | MdId mdId4; |
| 37 | |
| 38 | @Before |
| 39 | public void setUp() throws Exception, CfmConfigException { |
| 40 | mdId1 = MdIdCharStr.asMdId("md-1"); |
| 41 | mdId2 = MdIdDomainName.asMdId("md.domain.tld"); |
| 42 | mdId3 = MdIdMacUint.asMdId(MacAddress.valueOf("AA:BB:cc:dd:ee:ff"), 54321); |
| 43 | mdId4 = MdIdNone.asMdId(); |
| 44 | } |
| 45 | |
| 46 | @Test |
| 47 | public void testMdNameWrong() { |
| 48 | |
| 49 | try { |
| 50 | MdIdCharStr.asMdId(""); |
| 51 | fail("Expected an exception"); |
| 52 | } catch (IllegalArgumentException e) { |
| 53 | assertTrue(e.getMessage().contains("MD Name must follow pattern")); |
| 54 | } |
| 55 | |
| 56 | try { |
| 57 | MdIdCharStr.asMdId("name with spaces and other stuff"); |
| 58 | fail("Expected an exception"); |
| 59 | } catch (IllegalArgumentException e) { |
| 60 | assertTrue(e.getMessage().contains("MD Name must follow pattern")); |
| 61 | } |
| 62 | |
| 63 | try { |
| 64 | MdIdCharStr.asMdId("NameIsTooLongItShouldNotExceedFortyFiveCharacters"); |
| 65 | fail("Expected an exception"); |
| 66 | } catch (IllegalArgumentException e) { |
| 67 | assertTrue(e.getMessage().contains("MD Name must follow pattern")); |
| 68 | } |
| 69 | |
| 70 | try { |
| 71 | MdIdCharStr.asMdId(null); |
| 72 | fail("Expected an exception"); |
| 73 | } catch (IllegalArgumentException e) { |
| 74 | assertTrue(e.getMessage().contains("MD Name must follow pattern")); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | try { |
| 79 | MdIdDomainName.asMdId((String) null); |
| 80 | fail("Expected an exception"); |
| 81 | } catch (IllegalArgumentException e) { |
| 82 | assertTrue(e.getMessage().contains("MD Name must follow internet domain")); |
| 83 | } |
| 84 | |
| 85 | try { |
| 86 | MdIdDomainName.asMdId("name with spaces"); |
| 87 | fail("Expected an exception"); |
| 88 | } catch (IllegalArgumentException e) { |
| 89 | assertTrue(e.getMessage().contains("MD Name must follow internet domain")); |
| 90 | } |
| 91 | |
| 92 | try { |
| 93 | MdIdDomainName.asMdId(InternetDomainName |
| 94 | .from("a.really.long.domain.name.which.is.more.than.45.chars.long")); |
| 95 | fail("Expected an exception"); |
| 96 | } catch (IllegalArgumentException e) { |
| 97 | assertTrue(e.getMessage().contains("MD Domain Name must be between 1 and 45 chars long")); |
| 98 | } |
| 99 | |
| 100 | try { |
| 101 | MdIdMacUint.asMdId("AA:BB:cc:dd:ee:ff:70000"); |
| 102 | fail("Expected an exception"); |
| 103 | } catch (IllegalArgumentException e) { |
| 104 | assertTrue(e.getMessage().contains("uInt must be between 0 and 65535")); |
| 105 | } |
| 106 | |
| 107 | try { |
| 108 | MdIdMacUint.asMdId("something:12345"); |
| 109 | fail("Expected an exception"); |
| 110 | } catch (IllegalArgumentException e) { |
| 111 | assertTrue(e.getMessage().contains("MD Name must follow pattern")); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | @Test |
| 116 | public void testMdName() { |
| 117 | assertEquals("md-1", mdId1.mdName()); |
| 118 | assertEquals("md.domain.tld", mdId2.mdName()); |
| 119 | assertEquals("AA:BB:cc:dd:ee:ff:54321".toUpperCase(), mdId3.mdName()); |
| 120 | assertNull(mdId4.mdName()); |
| 121 | } |
| 122 | |
| 123 | @Test |
| 124 | public void testGetNameLength() { |
| 125 | assertEquals(4, mdId1.getNameLength()); |
| 126 | assertEquals(13, mdId2.getNameLength()); |
| 127 | assertEquals(8, mdId3.getNameLength()); |
| 128 | assertEquals(0, mdId4.getNameLength()); |
| 129 | } |
| 130 | |
| 131 | @Test |
| 132 | public void testEquals() { |
| 133 | //For char string |
| 134 | assertFalse(mdId1.equals(null)); |
| 135 | assertFalse(mdId1.equals(new String("test"))); |
| 136 | |
| 137 | assertTrue(mdId1.equals(mdId1)); |
| 138 | assertFalse(mdId1.equals(mdId2)); |
| 139 | |
| 140 | //For DomainName |
| 141 | assertFalse(mdId2.equals(null)); |
| 142 | assertFalse(mdId2.equals(new String("test"))); |
| 143 | |
| 144 | assertTrue(mdId2.equals(mdId2)); |
| 145 | assertFalse(mdId2.equals(mdId1)); |
| 146 | |
| 147 | //For MacUint |
| 148 | assertFalse(mdId3.equals(null)); |
| 149 | assertFalse(mdId3.equals(new String("test"))); |
| 150 | |
| 151 | assertTrue(mdId3.equals(mdId3)); |
| 152 | assertFalse(mdId3.equals(mdId1)); |
| 153 | |
| 154 | //For None |
| 155 | assertFalse(mdId4.equals(null)); |
| 156 | assertFalse(mdId4.equals(new String("test"))); |
| 157 | |
| 158 | assertTrue(mdId4.equals(mdId4)); |
| 159 | assertFalse(mdId4.equals(mdId1)); |
| 160 | |
| 161 | } |
| 162 | |
| 163 | @Test |
| 164 | public void testHashCode() { |
| 165 | assertEquals(mdId1.hashCode(), mdId1.hashCode()); |
| 166 | assertEquals(mdId2.hashCode(), mdId2.hashCode()); |
| 167 | assertEquals(mdId3.hashCode(), mdId3.hashCode()); |
| 168 | assertEquals(mdId4.hashCode(), mdId4.hashCode()); |
| 169 | } |
| 170 | } |