Sho SHIMIZU

Add unit tests for GenericDiscreteResources

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