Pier Luigi | 09220c2 | 2017-09-14 22:00:30 +0200 | [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 | |
| 17 | package org.onosproject.net.behaviour.trafficcontrol; |
| 18 | |
| 19 | import com.google.common.base.Strings; |
| 20 | import org.junit.Rule; |
| 21 | import org.junit.Test; |
| 22 | import org.junit.rules.ExpectedException; |
| 23 | import org.onlab.junit.ImmutableClassChecker; |
| 24 | |
| 25 | import java.net.URI; |
| 26 | |
| 27 | import static org.hamcrest.Matchers.is; |
| 28 | import static org.hamcrest.Matchers.notNullValue; |
| 29 | import static org.junit.Assert.assertEquals; |
| 30 | import static org.junit.Assert.assertNotEquals; |
| 31 | import static org.junit.Assert.assertThat; |
| 32 | |
| 33 | /** |
| 34 | * Test class for PolicerId. |
| 35 | */ |
| 36 | public class PolicerIdTest { |
| 37 | |
| 38 | // Test scheme |
| 39 | private static final String FOO_SCHEME = "foo"; |
| 40 | // OpenFlow scheme |
| 41 | private static final String OF_SCHEME = "of"; |
| 42 | // Some test ids |
| 43 | private static final Short ONE = 1; |
| 44 | private static final Short TWO = 15; |
| 45 | private static final String A = "A"; |
| 46 | private static final String LA = "a"; |
| 47 | |
| 48 | /** |
| 49 | * Test policer id creation from URI. |
| 50 | */ |
| 51 | @Test |
| 52 | public void testfromUriCreation() { |
| 53 | // Create URI representing the id |
| 54 | URI uriOne = URI.create(OF_SCHEME + ":" + Integer.toHexString(ONE)); |
| 55 | // Create policer id |
| 56 | PolicerId one = PolicerId.policerId(uriOne); |
| 57 | // Verify proper creation |
| 58 | assertThat(one, notNullValue()); |
| 59 | assertThat(one.id(), is(uriOne.toString().toLowerCase())); |
| 60 | assertThat(one.uri(), is(uriOne)); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Test policer id creation from string. |
| 65 | */ |
| 66 | @Test |
| 67 | public void testfromStringCreation() { |
| 68 | // Create String representing the id |
| 69 | String stringTwo = OF_SCHEME + ":" + Integer.toHexString(TWO); |
| 70 | // Create policer id |
| 71 | PolicerId two = PolicerId.policerId(stringTwo); |
| 72 | // Verify proper creation |
| 73 | assertThat(two, notNullValue()); |
| 74 | assertThat(two.id(), is(stringTwo)); |
| 75 | assertThat(two.uri(), is(URI.create(stringTwo))); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Exception expected to raise when creating a wrong policer id. |
| 80 | */ |
| 81 | @Rule |
| 82 | public ExpectedException exceptionWrongId = ExpectedException.none(); |
| 83 | |
| 84 | /** |
| 85 | * Test wrong creation of a policer id. |
| 86 | */ |
| 87 | @Test |
| 88 | public void testWrongCreation() { |
| 89 | // Build not allowed string |
| 90 | String wrongString = Strings.repeat("x", 1025); |
| 91 | // Define expected exception |
| 92 | exceptionWrongId.expect(IllegalArgumentException.class); |
| 93 | // Create policer id |
| 94 | PolicerId.policerId(wrongString); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Test equality between policer ids. |
| 99 | */ |
| 100 | @Test |
| 101 | public void testEquality() { |
| 102 | // Create URI representing the id one |
| 103 | URI uriOne = URI.create(OF_SCHEME + ":" + Integer.toHexString(ONE)); |
| 104 | // Create String representing the id one |
| 105 | String stringOne = OF_SCHEME + ":" + Integer.toHexString(ONE); |
| 106 | // Create String representing the id two |
| 107 | String stringTwo = OF_SCHEME + ":" + Integer.toHexString(TWO); |
| 108 | // Create String representing the id A |
| 109 | String stringA = FOO_SCHEME + ":" + A; |
| 110 | // Create String representing the id LA |
| 111 | String stringLA = FOO_SCHEME + ":" + LA; |
| 112 | // Create policer id one |
| 113 | PolicerId one = PolicerId.policerId(uriOne); |
| 114 | // Create policer id one |
| 115 | PolicerId copyOfOne = PolicerId.policerId(stringOne); |
| 116 | // Verify equality |
| 117 | assertEquals(one, copyOfOne); |
| 118 | // Create a different policer id |
| 119 | PolicerId two = PolicerId.policerId(stringTwo); |
| 120 | // Verify not equals |
| 121 | assertNotEquals(two, one); |
| 122 | assertNotEquals(two, copyOfOne); |
| 123 | // Create policer id A |
| 124 | PolicerId a = PolicerId.policerId(A); |
| 125 | // Create policer id LA |
| 126 | PolicerId la = PolicerId.policerId(LA); |
| 127 | // Verify not equals |
| 128 | assertNotEquals(a, la); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Tests immutability of PolicerId. |
| 133 | */ |
| 134 | @Test |
| 135 | public void testImmutability() { |
| 136 | ImmutableClassChecker.assertThatClassIsImmutable(PolicerId.class); |
| 137 | } |
| 138 | |
| 139 | } |