Sho SHIMIZU

Add unit tests for EncodableDiscreteResources

Change-Id: Id95baabb9885c95a4e503c4f117dacbd2f86a374
1 +/*
2 + * Copyright 2016-present 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 + */
16 +
17 +package org.onosproject.store.resource.impl;
18 +
19 +import com.google.common.collect.ImmutableSet;
20 +import org.junit.Test;
21 +import org.onosproject.net.DeviceId;
22 +import org.onosproject.net.PortNumber;
23 +import org.onosproject.net.resource.DiscreteResource;
24 +import org.onosproject.net.resource.Resources;
25 +
26 +import java.util.Optional;
27 +
28 +import static org.hamcrest.Matchers.is;
29 +import static org.junit.Assert.assertThat;
30 +
31 +public class EncodableDiscreteResourcesTest {
32 + @Test
33 + public void testIfResourceIsFound() {
34 + DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
35 +
36 + DiscreteResources sut = EncodableDiscreteResources.of(ImmutableSet.of(res1));
37 +
38 + assertThat(sut.lookup(res1.id()), is(Optional.of(res1)));
39 + }
40 +
41 + @Test
42 + public void testIfResourceIsNotFound() {
43 + DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
44 + DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("b"), PortNumber.portNumber(1)).resource();
45 +
46 + DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1));
47 +
48 + assertThat(sut.lookup(res2.id()), is(Optional.empty()));
49 + }
50 +
51 + @Test
52 + public void testIfDifferenceIsEmpty() {
53 + DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
54 + DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("b"), PortNumber.portNumber(1)).resource();
55 +
56 + DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1, res2));
57 +
58 + DiscreteResources other = GenericDiscreteResources.of(ImmutableSet.of(res1, res2));
59 +
60 + DiscreteResources expected = GenericDiscreteResources.of(ImmutableSet.of());
61 + assertThat(sut.difference(other), is(expected));
62 + }
63 +
64 + @Test
65 + public void testIfDifferenceIsNotEmpty() {
66 + DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
67 + DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("b"), PortNumber.portNumber(1)).resource();
68 +
69 + DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1, res2));
70 +
71 + DiscreteResources other = GenericDiscreteResources.of(ImmutableSet.of(res1));
72 +
73 + DiscreteResources expected = GenericDiscreteResources.of(ImmutableSet.of(res2));
74 + assertThat(sut.difference(other), is(expected));
75 + }
76 +
77 + @Test
78 + public void testIfDifferenceIsNotChanged() {
79 + DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
80 + DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("b"), PortNumber.portNumber(1)).resource();
81 +
82 + DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1));
83 +
84 + DiscreteResources other = GenericDiscreteResources.of(ImmutableSet.of(res2));
85 +
86 + DiscreteResources expected = GenericDiscreteResources.of(ImmutableSet.of(res1));
87 + assertThat(sut.difference(other), is(expected));
88 + }
89 +
90 + @Test
91 + public void testDifferenceFromEmpty() {
92 + DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
93 +
94 + DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1));
95 +
96 + DiscreteResources other = GenericDiscreteResources.of(ImmutableSet.of());
97 +
98 + DiscreteResources expected = GenericDiscreteResources.of(ImmutableSet.of(res1));
99 + assertThat(sut.difference(other), is(expected));
100 + }
101 +
102 + @Test
103 + public void testEmpty() {
104 + DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of());
105 +
106 + assertThat(sut.isEmpty(), is(true));
107 + }
108 +
109 + @Test
110 + public void testNotEmpty() {
111 + DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
112 +
113 + DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1));
114 +
115 + assertThat(sut.isEmpty(), is(false));
116 + }
117 +
118 + @Test
119 + public void testIfResourceIsContained() {
120 + DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
121 +
122 + DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1));
123 +
124 + assertThat(sut.containsAny(ImmutableSet.of(res1)), is(true));
125 + }
126 +
127 + @Test
128 + public void testIfResourceIsNotContained() {
129 + DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
130 + DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("b"), PortNumber.portNumber(1)).resource();
131 +
132 + DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1));
133 +
134 + assertThat(sut.containsAny(ImmutableSet.of(res2)), is(false));
135 + }
136 +
137 + @Test
138 + public void testContainsWithEmpty() {
139 + DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
140 +
141 + DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1));
142 +
143 + assertThat(sut.containsAny(ImmutableSet.of()), is(false));
144 + }
145 +
146 + @Test
147 + public void testAdd() {
148 + DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
149 + DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("b"), PortNumber.portNumber(1)).resource();
150 +
151 + DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1));
152 +
153 + DiscreteResources other = GenericDiscreteResources.of(ImmutableSet.of(res2));
154 +
155 + DiscreteResources expected = GenericDiscreteResources.of(ImmutableSet.of(res1, res2));
156 + assertThat(sut.add(other), is(expected));
157 + }
158 +
159 + @Test
160 + public void testValues() {
161 + DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
162 + DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("b"), PortNumber.portNumber(1)).resource();
163 +
164 + DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1, res2));
165 +
166 + assertThat(sut.values(), is(ImmutableSet.of(res1, res2)));
167 + }
168 +}