Sho SHIMIZU | 5d4b1c2 | 2014-11-10 10:07:36 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 Open Networking Laboratory |
| 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; |
| 26 | import org.onosproject.net.provider.ProviderId; |
Brian O'Connor | 6de2e20 | 2015-05-21 14:30:41 -0700 | [diff] [blame] | 27 | import org.onosproject.net.resource.link.LinkResourceService; |
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; |
| 54 | private LinkResourceService linkResourceService; |
| 55 | |
| 56 | @Before |
| 57 | public void setUp() { |
| 58 | linkResourceService = createMock(LinkResourceService.class); |
| 59 | |
| 60 | DefaultAnnotations annotations = DefaultAnnotations.builder().set(KEY, String.valueOf(VALUE)).build(); |
| 61 | |
| 62 | link = new DefaultLink(PID, cp(DID1, PID1), cp(DID2, PID2), DIRECT, annotations); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Tests the specified annotated value is less than the threshold. |
| 67 | */ |
| 68 | @Test |
| 69 | public void testLessThanThreshold() { |
| 70 | double value = 120; |
| 71 | sut = new AnnotationConstraint(KEY, value); |
| 72 | |
| 73 | assertThat(sut.isValid(link, linkResourceService), is(true)); |
| 74 | assertThat(sut.cost(link, linkResourceService), is(closeTo(VALUE, 1.0e-6))); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Tests the specified annotated value is more than the threshold. |
| 79 | */ |
| 80 | @Test |
| 81 | public void testMoreThanThreshold() { |
| 82 | double value = 80; |
| 83 | sut = new AnnotationConstraint(KEY, value); |
| 84 | |
| 85 | assertThat(sut.isValid(link, linkResourceService), is(false)); |
| 86 | assertThat(sut.cost(link, linkResourceService), is(lessThan(0.0))); |
| 87 | } |
| 88 | |
| 89 | @Test |
| 90 | public void testEquality() { |
| 91 | new EqualsTester() |
| 92 | .addEqualityGroup(new AnnotationConstraint(KEY, 100), new AnnotationConstraint(KEY, 100)) |
| 93 | .addEqualityGroup(new AnnotationConstraint(KEY, 120)) |
| 94 | .addEqualityGroup(new AnnotationConstraint("latency", 100)) |
| 95 | .testEquals(); |
| 96 | } |
| 97 | } |