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 org.junit.Rule; |
| 20 | import org.junit.Test; |
| 21 | import org.junit.rules.ExpectedException; |
| 22 | import org.onlab.junit.ImmutableClassChecker; |
| 23 | import org.onosproject.net.ConnectPoint; |
| 24 | |
| 25 | import static org.hamcrest.Matchers.is; |
| 26 | import static org.hamcrest.Matchers.notNullValue; |
| 27 | import static org.junit.Assert.*; |
| 28 | |
| 29 | /** |
| 30 | * Test class for PolicingResource. |
| 31 | */ |
| 32 | public class PolicingResourceTest { |
| 33 | |
| 34 | // Connectpoints |
| 35 | private static final String SCP1 = "of:00000000000001/1"; |
| 36 | private static final String SCP2 = "of:00000000000001/2"; |
| 37 | private static final ConnectPoint CP1 = ConnectPoint.deviceConnectPoint(SCP1); |
| 38 | private static final ConnectPoint CP2 = ConnectPoint.deviceConnectPoint(SCP2); |
| 39 | // OpenFlow scheme |
| 40 | private static final String OF_SCHEME = "of"; |
| 41 | // Policer identifier |
| 42 | private static final String SID = OF_SCHEME + ":" + Integer.toHexString(1); |
| 43 | private static final PolicerId PID = PolicerId.policerId(SID); |
| 44 | |
| 45 | /** |
| 46 | * Test policing resource creation. |
| 47 | */ |
| 48 | @Test |
| 49 | public void testCreation() { |
| 50 | // Create a new policing resource |
| 51 | PolicingResource policingResource = new PolicingResource(PID, CP1); |
| 52 | // Verify proper creation |
| 53 | assertThat(policingResource, notNullValue()); |
| 54 | assertThat(policingResource.policerId(), is(PID)); |
| 55 | assertThat(policingResource.connectPoint(), is(CP1)); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Exception expected to raise when creating policing resource with null id. |
| 60 | */ |
| 61 | @Rule |
| 62 | public ExpectedException exceptionNullId = ExpectedException.none(); |
| 63 | |
| 64 | /** |
| 65 | * Test wrong creation of a policing resource. |
| 66 | */ |
| 67 | @Test |
| 68 | public void testNullIdCreation() { |
| 69 | // Define expected exception |
| 70 | exceptionNullId.expect(NullPointerException.class); |
| 71 | // Create wrong policing resource |
| 72 | new PolicingResource(null, CP1); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Test equality between policing resources. |
| 77 | */ |
| 78 | @Test |
| 79 | public void testEqualilty() { |
| 80 | // Create two identical resources |
| 81 | PolicingResource one = new PolicingResource(PID, CP1); |
| 82 | PolicingResource copyOfOne = new PolicingResource(PID, CP1); |
| 83 | // Verify equality |
| 84 | assertEquals(one, copyOfOne); |
| 85 | // Create a different resource |
| 86 | PolicingResource two = new PolicingResource(PID, CP2); |
| 87 | // Verify not equals |
| 88 | assertNotEquals(two, one); |
| 89 | assertNotEquals(two, copyOfOne); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Tests immutability of PolicingResource. |
| 94 | */ |
| 95 | @Test |
| 96 | public void testImmutability() { |
| 97 | ImmutableClassChecker.assertThatClassIsImmutable(PolicingResource.class); |
| 98 | } |
| 99 | |
| 100 | } |