blob: b7f6d79e4b87e832d1c218f2679e9c11aeab07cc [file] [log] [blame]
Ray Milkey8010bb42015-02-12 11:53:40 -08001/*
2 * Copyright 2015 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.onosproject.net.intent;
17
18import java.util.Collections;
19import java.util.concurrent.atomic.AtomicLong;
20
21import org.junit.After;
22import org.junit.Before;
23import org.junit.Test;
24import org.onosproject.core.IdGenerator;
25import org.onosproject.net.NetTestTools;
26import org.onosproject.store.Timestamp;
27
28import com.google.common.testing.EqualsTester;
29
30import static org.hamcrest.MatcherAssert.assertThat;
31import static org.hamcrest.Matchers.is;
32
33/**
34 * Unit tests for intent data objects.
35 */
36public class IntentDataTest {
37 private static class MockIntent extends Intent {
38 private static AtomicLong counter = new AtomicLong(0);
39
40 private final Long number;
41
42 public MockIntent(Long number) {
43 super(NetTestTools.APP_ID, Collections.emptyList());
44 this.number = number;
45 }
46
47 public Long number() {
48 return number;
49 }
50
51 public static Long nextId() {
52 return counter.getAndIncrement();
53 }
54 }
55
56 private static class MockTimestamp implements Timestamp {
57 final int value;
58
59 MockTimestamp(int value) {
60 this.value = value;
61 }
62
63 @Override
64 public int compareTo(Timestamp o) {
65 if (!(o instanceof MockTimestamp)) {
66 return -1;
67 }
68 MockTimestamp that = (MockTimestamp) o;
69 return (this.value > that.value ? -1 : (this.value == that.value ? 0 : 1));
70 }
71 }
72
73 private Timestamp timestamp1;
74 private Timestamp timestamp2;
75 private Timestamp timestamp3;
76
77 private Intent intent1;
78 private Intent intent2;
79 private Intent intent3;
80
81 private IntentData data1;
82 private IntentData data1Copy;
83 private IntentData data2;
84 private IntentData data2Copy;
85 private IntentData data3;
86 private IntentData data3Copy;
87
88 IdGenerator idGenerator;
89
90 @Before
91 public void setUpTest() {
92 idGenerator = new MockIdGenerator();
93 Intent.bindIdGenerator(idGenerator);
94
95 timestamp1 = new MockTimestamp(1);
96 timestamp2 = new MockTimestamp(2);
97 timestamp3 = new MockTimestamp(3);
98
99 intent1 = new MockIntent(1L);
100 intent2 = new MockIntent(2L);
101 intent3 = new MockIntent(3L);
102
103 data1 = new IntentData(intent1, IntentState.INSTALLED, timestamp1);
104 data1Copy = new IntentData(intent1, IntentState.INSTALLED, timestamp1);
105 data2 = new IntentData(intent2, IntentState.INSTALLED, timestamp2);
106 data2Copy = new IntentData(intent2, IntentState.INSTALLED, timestamp2);
107 data3 = new IntentData(intent3, IntentState.INSTALLED, timestamp3);
108 data3Copy = new IntentData(intent3, IntentState.INSTALLED, timestamp3);
109 }
110
111 @After
112 public void tearDownTest() {
113 Intent.unbindIdGenerator(idGenerator);
114 }
115
116 /**
117 * Checks that intent data objects are properly constructed.
118 */
119 @Test
120 public void checkConstruction() {
121 assertThat(data1.state(), is(IntentState.INSTALLED));
122 assertThat(data1.version(), is(timestamp1));
123 assertThat(data1.intent(), is(intent1));
124 }
125
126 /**
127 * Checks equals() for intent data objects.
128 */
129 @Test
130 public void checkEquals() {
131 new EqualsTester()
132 .addEqualityGroup(data1, data1Copy)
133 .addEqualityGroup(data2, data2Copy)
134 .addEqualityGroup(data3, data3Copy)
135 .testEquals();
136 }
137}