Sho SHIMIZU | 5d4b1c2 | 2014-11-10 10:07:36 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Sho SHIMIZU | 5d4b1c2 | 2014-11-10 10:07:36 -0800 | [diff] [blame] | 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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.net.intent.constraint; |
Sho SHIMIZU | 5d4b1c2 | 2014-11-10 10:07:36 -0800 | [diff] [blame] | 17 | |
| 18 | import com.google.common.testing.EqualsTester; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 21 | import org.onosproject.net.DefaultAnnotations; |
| 22 | import org.onosproject.net.DefaultLink; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.Link; |
| 25 | import org.onosproject.net.PortNumber; |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 26 | import org.onosproject.net.intent.ResourceContext; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 27 | import org.onosproject.net.provider.ProviderId; |
Sho SHIMIZU | 5d4b1c2 | 2014-11-10 10:07:36 -0800 | [diff] [blame] | 28 | |
| 29 | import static org.easymock.EasyMock.createMock; |
| 30 | import static org.hamcrest.Matchers.closeTo; |
| 31 | import static org.hamcrest.Matchers.is; |
| 32 | import static org.hamcrest.Matchers.lessThan; |
| 33 | import static org.junit.Assert.assertThat; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 34 | import static org.onosproject.net.DefaultLinkTest.cp; |
| 35 | import static org.onosproject.net.DeviceId.deviceId; |
| 36 | import static org.onosproject.net.Link.Type.DIRECT; |
| 37 | import static org.onosproject.net.PortNumber.portNumber; |
Sho SHIMIZU | 5d4b1c2 | 2014-11-10 10:07:36 -0800 | [diff] [blame] | 38 | |
| 39 | /** |
| 40 | * Test for link annotated value threshold. |
| 41 | */ |
| 42 | public class AnnotationConstraintTest { |
| 43 | |
| 44 | private static final ProviderId PID = new ProviderId("of", "foo"); |
| 45 | private static final DeviceId DID1 = deviceId("of:1"); |
| 46 | private static final DeviceId DID2 = deviceId("of:2"); |
| 47 | private static final PortNumber PID1 = portNumber(1); |
| 48 | private static final PortNumber PID2 = portNumber(2); |
| 49 | private static final String KEY = "distance"; |
| 50 | private static final double VALUE = 100; |
| 51 | |
| 52 | private AnnotationConstraint sut; |
| 53 | private Link link; |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 54 | private ResourceContext resourceContext; |
Sho SHIMIZU | 5d4b1c2 | 2014-11-10 10:07:36 -0800 | [diff] [blame] | 55 | |
| 56 | @Before |
| 57 | public void setUp() { |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 58 | resourceContext = createMock(ResourceContext.class); |
Sho SHIMIZU | 5d4b1c2 | 2014-11-10 10:07:36 -0800 | [diff] [blame] | 59 | |
| 60 | DefaultAnnotations annotations = DefaultAnnotations.builder().set(KEY, String.valueOf(VALUE)).build(); |
| 61 | |
Ray Milkey | 2693bda | 2016-01-22 16:08:14 -0800 | [diff] [blame] | 62 | link = DefaultLink.builder() |
| 63 | .providerId(PID) |
| 64 | .src(cp(DID1, PID1)) |
| 65 | .dst(cp(DID2, PID2)) |
| 66 | .type(DIRECT) |
| 67 | .annotations(annotations).build(); |
Sho SHIMIZU | 5d4b1c2 | 2014-11-10 10:07:36 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Tests the specified annotated value is less than the threshold. |
| 72 | */ |
| 73 | @Test |
| 74 | public void testLessThanThreshold() { |
| 75 | double value = 120; |
| 76 | sut = new AnnotationConstraint(KEY, value); |
| 77 | |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 78 | assertThat(sut.isValid(link, resourceContext), is(true)); |
| 79 | assertThat(sut.cost(link, resourceContext), is(closeTo(VALUE, 1.0e-6))); |
Sho SHIMIZU | 5d4b1c2 | 2014-11-10 10:07:36 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Tests the specified annotated value is more than the threshold. |
| 84 | */ |
| 85 | @Test |
| 86 | public void testMoreThanThreshold() { |
| 87 | double value = 80; |
| 88 | sut = new AnnotationConstraint(KEY, value); |
| 89 | |
Sho SHIMIZU | b1681bd | 2016-02-22 12:47:50 -0800 | [diff] [blame] | 90 | assertThat(sut.isValid(link, resourceContext), is(false)); |
| 91 | assertThat(sut.cost(link, resourceContext), is(lessThan(0.0))); |
Sho SHIMIZU | 5d4b1c2 | 2014-11-10 10:07:36 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Test |
| 95 | public void testEquality() { |
| 96 | new EqualsTester() |
| 97 | .addEqualityGroup(new AnnotationConstraint(KEY, 100), new AnnotationConstraint(KEY, 100)) |
| 98 | .addEqualityGroup(new AnnotationConstraint(KEY, 120)) |
| 99 | .addEqualityGroup(new AnnotationConstraint("latency", 100)) |
| 100 | .testEquals(); |
| 101 | } |
| 102 | } |