Sho SHIMIZU
Committed by Jonathan Hart

Fix unit tests and bugs of EncodableDiscreteResources

Change-Id: Iea3b39dbdcfabaf23fa7e7ef6cb8a98bfe432081
...@@ -66,6 +66,17 @@ final class EncodableDiscreteResources implements DiscreteResources { ...@@ -66,6 +66,17 @@ final class EncodableDiscreteResources implements DiscreteResources {
66 return new EncodableDiscreteResources(parent, values); 66 return new EncodableDiscreteResources(parent, values);
67 } 67 }
68 68
69 + private static DiscreteResources of(DiscreteResource parent, Map<Class<?>, EncodedDiscreteResources> map) {
70 + if (isEmpty(map)) {
71 + return DiscreteResources.empty();
72 + }
73 + return new EncodableDiscreteResources(parent, map);
74 + }
75 +
76 + private static boolean isEmpty(Map<Class<?>, EncodedDiscreteResources> map) {
77 + return map.values().stream().allMatch(EncodedDiscreteResources::isEmpty);
78 + }
79 +
69 EncodableDiscreteResources(DiscreteResource parent, Map<Class<?>, EncodedDiscreteResources> map) { 80 EncodableDiscreteResources(DiscreteResource parent, Map<Class<?>, EncodedDiscreteResources> map) {
70 this.parent = parent; 81 this.parent = parent;
71 this.map = map; 82 this.map = map;
...@@ -73,6 +84,9 @@ final class EncodableDiscreteResources implements DiscreteResources { ...@@ -73,6 +84,9 @@ final class EncodableDiscreteResources implements DiscreteResources {
73 84
74 @Override 85 @Override
75 public Optional<DiscreteResource> lookup(DiscreteResourceId id) { 86 public Optional<DiscreteResource> lookup(DiscreteResourceId id) {
87 + if (!id.parent().filter(parent.id()::equals).isPresent()) {
88 + return Optional.empty();
89 + }
76 DiscreteResource resource = Resources.discrete(id).resource(); 90 DiscreteResource resource = Resources.discrete(id).resource();
77 Class<?> cls = getClass(resource); 91 Class<?> cls = getClass(resource);
78 return Optional.ofNullable(map.get(cls)) 92 return Optional.ofNullable(map.get(cls))
...@@ -92,7 +106,7 @@ final class EncodableDiscreteResources implements DiscreteResources { ...@@ -92,7 +106,7 @@ final class EncodableDiscreteResources implements DiscreteResources {
92 Map.Entry::getValue, 106 Map.Entry::getValue,
93 EncodedDiscreteResources::difference, 107 EncodedDiscreteResources::difference,
94 LinkedHashMap::new)); 108 LinkedHashMap::new));
95 - return new EncodableDiscreteResources(parent, newMap); 109 + return of(parent, newMap);
96 } else if (other instanceof EmptyDiscreteResources) { 110 } else if (other instanceof EmptyDiscreteResources) {
97 return this; 111 return this;
98 } 112 }
...@@ -102,8 +116,7 @@ final class EncodableDiscreteResources implements DiscreteResources { ...@@ -102,8 +116,7 @@ final class EncodableDiscreteResources implements DiscreteResources {
102 116
103 @Override 117 @Override
104 public boolean isEmpty() { 118 public boolean isEmpty() {
105 - return map.values().stream() 119 + return isEmpty(map);
106 - .allMatch(x -> x.isEmpty());
107 } 120 }
108 121
109 @Override 122 @Override
...@@ -125,7 +138,7 @@ final class EncodableDiscreteResources implements DiscreteResources { ...@@ -125,7 +138,7 @@ final class EncodableDiscreteResources implements DiscreteResources {
125 EncodedDiscreteResources::add, 138 EncodedDiscreteResources::add,
126 LinkedHashMap::new 139 LinkedHashMap::new
127 )); 140 ));
128 - return new EncodableDiscreteResources(parent, newMap); 141 + return of(parent, newMap);
129 } else if (other instanceof EmptyDiscreteResources) { 142 } else if (other instanceof EmptyDiscreteResources) {
130 return this; 143 return this;
131 } 144 }
......
...@@ -62,9 +62,9 @@ public class EncodableDiscreteResourcesTest { ...@@ -62,9 +62,9 @@ public class EncodableDiscreteResourcesTest {
62 @Test 62 @Test
63 public void testIfResourceIsNotFound() { 63 public void testIfResourceIsNotFound() {
64 DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource(); 64 DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
65 - DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("b"), PortNumber.portNumber(1)).resource(); 65 + DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(2)).resource();
66 66
67 - DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1)); 67 + DiscreteResources sut = EncodableDiscreteResources.of(ImmutableSet.of(res1));
68 68
69 assertThat(sut.lookup(res2.id()), is(Optional.empty())); 69 assertThat(sut.lookup(res2.id()), is(Optional.empty()));
70 } 70 }
...@@ -72,39 +72,39 @@ public class EncodableDiscreteResourcesTest { ...@@ -72,39 +72,39 @@ public class EncodableDiscreteResourcesTest {
72 @Test 72 @Test
73 public void testIfDifferenceIsEmpty() { 73 public void testIfDifferenceIsEmpty() {
74 DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource(); 74 DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
75 - DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("b"), PortNumber.portNumber(1)).resource(); 75 + DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(2)).resource();
76 76
77 - DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1, res2)); 77 + DiscreteResources sut = EncodableDiscreteResources.of(ImmutableSet.of(res1, res2));
78 78
79 - DiscreteResources other = GenericDiscreteResources.of(ImmutableSet.of(res1, res2)); 79 + DiscreteResources other = EncodableDiscreteResources.of(ImmutableSet.of(res1, res2));
80 80
81 - DiscreteResources expected = GenericDiscreteResources.of(ImmutableSet.of()); 81 + DiscreteResources expected = DiscreteResources.empty();
82 assertThat(sut.difference(other), is(expected)); 82 assertThat(sut.difference(other), is(expected));
83 } 83 }
84 84
85 @Test 85 @Test
86 public void testIfDifferenceIsNotEmpty() { 86 public void testIfDifferenceIsNotEmpty() {
87 DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource(); 87 DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
88 - DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("b"), PortNumber.portNumber(1)).resource(); 88 + DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(2)).resource();
89 89
90 - DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1, res2)); 90 + DiscreteResources sut = EncodableDiscreteResources.of(ImmutableSet.of(res1, res2));
91 91
92 - DiscreteResources other = GenericDiscreteResources.of(ImmutableSet.of(res1)); 92 + DiscreteResources other = EncodableDiscreteResources.of(ImmutableSet.of(res1));
93 93
94 - DiscreteResources expected = GenericDiscreteResources.of(ImmutableSet.of(res2)); 94 + DiscreteResources expected = EncodableDiscreteResources.of(ImmutableSet.of(res2));
95 assertThat(sut.difference(other), is(expected)); 95 assertThat(sut.difference(other), is(expected));
96 } 96 }
97 97
98 @Test 98 @Test
99 public void testIfDifferenceIsNotChanged() { 99 public void testIfDifferenceIsNotChanged() {
100 DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource(); 100 DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
101 - DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("b"), PortNumber.portNumber(1)).resource(); 101 + DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(2)).resource();
102 102
103 - DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1)); 103 + DiscreteResources sut = EncodableDiscreteResources.of(ImmutableSet.of(res1));
104 104
105 - DiscreteResources other = GenericDiscreteResources.of(ImmutableSet.of(res2)); 105 + DiscreteResources other = EncodableDiscreteResources.of(ImmutableSet.of(res2));
106 106
107 - DiscreteResources expected = GenericDiscreteResources.of(ImmutableSet.of(res1)); 107 + DiscreteResources expected = EncodableDiscreteResources.of(ImmutableSet.of(res1));
108 assertThat(sut.difference(other), is(expected)); 108 assertThat(sut.difference(other), is(expected));
109 } 109 }
110 110
...@@ -112,17 +112,17 @@ public class EncodableDiscreteResourcesTest { ...@@ -112,17 +112,17 @@ public class EncodableDiscreteResourcesTest {
112 public void testDifferenceFromEmpty() { 112 public void testDifferenceFromEmpty() {
113 DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource(); 113 DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
114 114
115 - DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1)); 115 + DiscreteResources sut = EncodableDiscreteResources.of(ImmutableSet.of(res1));
116 116
117 - DiscreteResources other = GenericDiscreteResources.of(ImmutableSet.of()); 117 + DiscreteResources other = DiscreteResources.empty();
118 118
119 - DiscreteResources expected = GenericDiscreteResources.of(ImmutableSet.of(res1)); 119 + DiscreteResources expected = EncodableDiscreteResources.of(ImmutableSet.of(res1));
120 assertThat(sut.difference(other), is(expected)); 120 assertThat(sut.difference(other), is(expected));
121 } 121 }
122 122
123 @Test 123 @Test
124 public void testEmpty() { 124 public void testEmpty() {
125 - DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of()); 125 + DiscreteResources sut = EncodableDiscreteResources.of(ImmutableSet.of());
126 126
127 assertThat(sut.isEmpty(), is(true)); 127 assertThat(sut.isEmpty(), is(true));
128 } 128 }
...@@ -131,7 +131,7 @@ public class EncodableDiscreteResourcesTest { ...@@ -131,7 +131,7 @@ public class EncodableDiscreteResourcesTest {
131 public void testNotEmpty() { 131 public void testNotEmpty() {
132 DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource(); 132 DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
133 133
134 - DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1)); 134 + DiscreteResources sut = EncodableDiscreteResources.of(ImmutableSet.of(res1));
135 135
136 assertThat(sut.isEmpty(), is(false)); 136 assertThat(sut.isEmpty(), is(false));
137 } 137 }
...@@ -140,7 +140,7 @@ public class EncodableDiscreteResourcesTest { ...@@ -140,7 +140,7 @@ public class EncodableDiscreteResourcesTest {
140 public void testIfResourceIsContained() { 140 public void testIfResourceIsContained() {
141 DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource(); 141 DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
142 142
143 - DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1)); 143 + DiscreteResources sut = EncodableDiscreteResources.of(ImmutableSet.of(res1));
144 144
145 assertThat(sut.containsAny(ImmutableSet.of(res1)), is(true)); 145 assertThat(sut.containsAny(ImmutableSet.of(res1)), is(true));
146 } 146 }
...@@ -148,9 +148,9 @@ public class EncodableDiscreteResourcesTest { ...@@ -148,9 +148,9 @@ public class EncodableDiscreteResourcesTest {
148 @Test 148 @Test
149 public void testIfResourceIsNotContained() { 149 public void testIfResourceIsNotContained() {
150 DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource(); 150 DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
151 - DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("b"), PortNumber.portNumber(1)).resource(); 151 + DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(2)).resource();
152 152
153 - DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1)); 153 + DiscreteResources sut = EncodableDiscreteResources.of(ImmutableSet.of(res1));
154 154
155 assertThat(sut.containsAny(ImmutableSet.of(res2)), is(false)); 155 assertThat(sut.containsAny(ImmutableSet.of(res2)), is(false));
156 } 156 }
...@@ -159,7 +159,7 @@ public class EncodableDiscreteResourcesTest { ...@@ -159,7 +159,7 @@ public class EncodableDiscreteResourcesTest {
159 public void testContainsWithEmpty() { 159 public void testContainsWithEmpty() {
160 DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource(); 160 DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
161 161
162 - DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1)); 162 + DiscreteResources sut = EncodableDiscreteResources.of(ImmutableSet.of(res1));
163 163
164 assertThat(sut.containsAny(ImmutableSet.of()), is(false)); 164 assertThat(sut.containsAny(ImmutableSet.of()), is(false));
165 } 165 }
...@@ -167,22 +167,22 @@ public class EncodableDiscreteResourcesTest { ...@@ -167,22 +167,22 @@ public class EncodableDiscreteResourcesTest {
167 @Test 167 @Test
168 public void testAdd() { 168 public void testAdd() {
169 DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource(); 169 DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
170 - DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("b"), PortNumber.portNumber(1)).resource(); 170 + DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(2)).resource();
171 171
172 - DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1)); 172 + DiscreteResources sut = EncodableDiscreteResources.of(ImmutableSet.of(res1));
173 173
174 - DiscreteResources other = GenericDiscreteResources.of(ImmutableSet.of(res2)); 174 + DiscreteResources other = EncodableDiscreteResources.of(ImmutableSet.of(res2));
175 175
176 - DiscreteResources expected = GenericDiscreteResources.of(ImmutableSet.of(res1, res2)); 176 + DiscreteResources expected = EncodableDiscreteResources.of(ImmutableSet.of(res1, res2));
177 assertThat(sut.add(other), is(expected)); 177 assertThat(sut.add(other), is(expected));
178 } 178 }
179 179
180 @Test 180 @Test
181 public void testValues() { 181 public void testValues() {
182 DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource(); 182 DiscreteResource res1 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(1)).resource();
183 - DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("b"), PortNumber.portNumber(1)).resource(); 183 + DiscreteResource res2 = Resources.discrete(DeviceId.deviceId("a"), PortNumber.portNumber(2)).resource();
184 184
185 - DiscreteResources sut = GenericDiscreteResources.of(ImmutableSet.of(res1, res2)); 185 + DiscreteResources sut = EncodableDiscreteResources.of(ImmutableSet.of(res1, res2));
186 186
187 assertThat(sut.values(), is(ImmutableSet.of(res1, res2))); 187 assertThat(sut.values(), is(ImmutableSet.of(res1, res2)));
188 } 188 }
......