blob: 397bc0ee4a3fed6b19d4afbe33c7504323431751 [file] [log] [blame]
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -08003 *
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'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.net.intent.constraint;
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -080017
18import com.google.common.testing.EqualsTester;
19import org.junit.Before;
20import org.junit.Test;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.net.DefaultAnnotations;
22import org.onosproject.net.DefaultLink;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.Link;
25import org.onosproject.net.PortNumber;
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080026import org.onosproject.net.intent.ResourceContext;
Brian O'Connorabafb502014-12-02 22:26:20 -080027import org.onosproject.net.provider.ProviderId;
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -080028
29import static org.easymock.EasyMock.createMock;
30import static org.hamcrest.Matchers.closeTo;
31import static org.hamcrest.Matchers.is;
32import static org.hamcrest.Matchers.lessThan;
33import static org.junit.Assert.assertThat;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import static org.onosproject.net.DefaultLinkTest.cp;
35import static org.onosproject.net.DeviceId.deviceId;
36import static org.onosproject.net.Link.Type.DIRECT;
37import static org.onosproject.net.PortNumber.portNumber;
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -080038
39/**
40 * Test for link annotated value threshold.
41 */
42public 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 SHIMIZUb1681bd2016-02-22 12:47:50 -080054 private ResourceContext resourceContext;
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -080055
56 @Before
57 public void setUp() {
Sho SHIMIZUb1681bd2016-02-22 12:47:50 -080058 resourceContext = createMock(ResourceContext.class);
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -080059
60 DefaultAnnotations annotations = DefaultAnnotations.builder().set(KEY, String.valueOf(VALUE)).build();
61
Ray Milkey2693bda2016-01-22 16:08:14 -080062 link = DefaultLink.builder()
63 .providerId(PID)
64 .src(cp(DID1, PID1))
65 .dst(cp(DID2, PID2))
66 .type(DIRECT)
67 .annotations(annotations).build();
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -080068 }
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 SHIMIZUb1681bd2016-02-22 12:47:50 -080078 assertThat(sut.isValid(link, resourceContext), is(true));
79 assertThat(sut.cost(link, resourceContext), is(closeTo(VALUE, 1.0e-6)));
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -080080 }
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 SHIMIZUb1681bd2016-02-22 12:47:50 -080090 assertThat(sut.isValid(link, resourceContext), is(false));
91 assertThat(sut.cost(link, resourceContext), is(lessThan(0.0)));
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -080092 }
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}