blob: 5d7ec37d663c1fcd23f2f6f9b293bc12a9d3bb59 [file] [log] [blame]
Sho SHIMIZU5d4b1c22014-11-10 10:07:36 -08001/*
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 */
16package org.onlab.onos.net.intent.constraint;
17
18import com.google.common.testing.EqualsTester;
19import org.junit.Before;
20import org.junit.Test;
21import org.onlab.onos.net.DefaultAnnotations;
22import org.onlab.onos.net.DefaultLink;
23import org.onlab.onos.net.DeviceId;
24import org.onlab.onos.net.Link;
25import org.onlab.onos.net.PortNumber;
26import org.onlab.onos.net.provider.ProviderId;
27import org.onlab.onos.net.resource.LinkResourceService;
28
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;
34import static org.onlab.onos.net.DefaultLinkTest.cp;
35import static org.onlab.onos.net.DeviceId.deviceId;
36import static org.onlab.onos.net.Link.Type.DIRECT;
37import static org.onlab.onos.net.PortNumber.portNumber;
38
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;
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}