[ONOS-3116] port-pair-id testcase
Change-Id: Iad03f6447502d1a363fe56770445554b59b972f2
Showing
5 changed files
with
79 additions
and
11 deletions
... | @@ -16,6 +16,7 @@ | ... | @@ -16,6 +16,7 @@ |
16 | package org.onosproject.vtnrsc; | 16 | package org.onosproject.vtnrsc; |
17 | 17 | ||
18 | import static com.google.common.base.MoreObjects.toStringHelper; | 18 | import static com.google.common.base.MoreObjects.toStringHelper; |
19 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
19 | 20 | ||
20 | import java.util.UUID; | 21 | import java.util.UUID; |
21 | import java.util.Objects; | 22 | import java.util.Objects; |
... | @@ -32,27 +33,28 @@ public final class PortPairId { | ... | @@ -32,27 +33,28 @@ public final class PortPairId { |
32 | * | 33 | * |
33 | * @param id UUID id of port pair | 34 | * @param id UUID id of port pair |
34 | */ | 35 | */ |
35 | - private PortPairId(final UUID id) { | 36 | + private PortPairId(UUID id) { |
37 | + checkNotNull(id, "Port chain id can not be null"); | ||
36 | this.portPairId = id; | 38 | this.portPairId = id; |
37 | } | 39 | } |
38 | 40 | ||
39 | /** | 41 | /** |
40 | - * Constructor to create port pair id from UUID. | 42 | + * Returns newly created port pair id object. |
41 | * | 43 | * |
42 | * @param id UUID of port pair id | 44 | * @param id UUID of port pair id |
43 | * @return object of port pair id | 45 | * @return object of port pair id |
44 | */ | 46 | */ |
45 | - public static PortPairId portPairId(final UUID id) { | 47 | + public static PortPairId of(UUID id) { |
46 | return new PortPairId(id); | 48 | return new PortPairId(id); |
47 | } | 49 | } |
48 | 50 | ||
49 | /** | 51 | /** |
50 | - * Constructor to create port pair id from string. | 52 | + * Returns newly created port pair id object. |
51 | * | 53 | * |
52 | * @param id port pair id in string | 54 | * @param id port pair id in string |
53 | * @return object of port pair id | 55 | * @return object of port pair id |
54 | */ | 56 | */ |
55 | - public static PortPairId portPairId(final String id) { | 57 | + public static PortPairId of(String id) { |
56 | return new PortPairId(UUID.fromString(id)); | 58 | return new PortPairId(UUID.fromString(id)); |
57 | } | 59 | } |
58 | 60 | ||
... | @@ -84,6 +86,8 @@ public final class PortPairId { | ... | @@ -84,6 +86,8 @@ public final class PortPairId { |
84 | 86 | ||
85 | @Override | 87 | @Override |
86 | public String toString() { | 88 | public String toString() { |
87 | - return toStringHelper(this).add("portPairId", portPairId.toString()).toString(); | 89 | + return toStringHelper(this) |
90 | + .add("portPairId", portPairId) | ||
91 | + .toString(); | ||
88 | } | 92 | } |
89 | } | 93 | } | ... | ... |
1 | +/* | ||
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 | + */ | ||
16 | +package org.onosproject.vtnrsc.portpair; | ||
17 | + | ||
18 | +import static org.hamcrest.MatcherAssert.assertThat; | ||
19 | +import static org.hamcrest.Matchers.is; | ||
20 | +import static org.hamcrest.Matchers.notNullValue; | ||
21 | +import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable; | ||
22 | + | ||
23 | +import org.junit.Test; | ||
24 | +import org.onosproject.vtnrsc.PortPairId; | ||
25 | + | ||
26 | +import com.google.common.testing.EqualsTester; | ||
27 | +import java.util.UUID; | ||
28 | + | ||
29 | +/** | ||
30 | + * Unit tests for PortPairId class. | ||
31 | + */ | ||
32 | +public class PortPairIdTest { | ||
33 | + | ||
34 | + final PortPairId portPairId1 = PortPairId.of("78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae"); | ||
35 | + final PortPairId sameAsPortPairId1 = PortPairId.of("78dcd363-fc23-aeb6-f44b-56dc5e2fb3ae"); | ||
36 | + final PortPairId portPairId2 = PortPairId.of("dace4513-24fc-4fae-af4b-321c5e2eb3d1"); | ||
37 | + | ||
38 | + /** | ||
39 | + * Checks that the PortPairId class is immutable. | ||
40 | + */ | ||
41 | + @Test | ||
42 | + public void testImmutability() { | ||
43 | + assertThatClassIsImmutable(PortPairId.class); | ||
44 | + } | ||
45 | + | ||
46 | + /** | ||
47 | + * Checks the operation of equals() methods. | ||
48 | + */ | ||
49 | + @Test | ||
50 | + public void testEquals() { | ||
51 | + new EqualsTester().addEqualityGroup(portPairId1, sameAsPortPairId1).addEqualityGroup(portPairId2).testEquals(); | ||
52 | + } | ||
53 | + | ||
54 | + /** | ||
55 | + * Checks the construction of a PortPairId object. | ||
56 | + */ | ||
57 | + @Test | ||
58 | + public void testConstruction() { | ||
59 | + final String portPairIdValue = "dace4513-24fc-4fae-af4b-321c5e2eb3d1"; | ||
60 | + final PortPairId portPairId = PortPairId.of(portPairIdValue); | ||
61 | + assertThat(portPairId, is(notNullValue())); | ||
62 | + assertThat(portPairId.value(), is(UUID.fromString(portPairIdValue))); | ||
63 | + } | ||
64 | +} |
... | @@ -82,11 +82,11 @@ public class PortPairWebResource extends AbstractWebResource { | ... | @@ -82,11 +82,11 @@ public class PortPairWebResource extends AbstractWebResource { |
82 | @Produces(MediaType.APPLICATION_JSON) | 82 | @Produces(MediaType.APPLICATION_JSON) |
83 | public Response getPortPair(@PathParam("portPairId") String id) { | 83 | public Response getPortPair(@PathParam("portPairId") String id) { |
84 | 84 | ||
85 | - if (!service.exists(PortPairId.portPairId(id))) { | 85 | + if (!service.exists(PortPairId.of(id))) { |
86 | return Response.status(NOT_FOUND) | 86 | return Response.status(NOT_FOUND) |
87 | .entity(PORT_PAIR_NOT_FOUND).build(); | 87 | .entity(PORT_PAIR_NOT_FOUND).build(); |
88 | } | 88 | } |
89 | - PortPair portPair = nullIsNotFound(service.getPortPair(PortPairId.portPairId(id)), | 89 | + PortPair portPair = nullIsNotFound(service.getPortPair(PortPairId.of(id)), |
90 | PORT_PAIR_NOT_FOUND); | 90 | PORT_PAIR_NOT_FOUND); |
91 | 91 | ||
92 | ObjectNode result = new ObjectMapper().createObjectNode(); | 92 | ObjectNode result = new ObjectMapper().createObjectNode(); |
... | @@ -151,7 +151,7 @@ public class PortPairWebResource extends AbstractWebResource { | ... | @@ -151,7 +151,7 @@ public class PortPairWebResource extends AbstractWebResource { |
151 | @DELETE | 151 | @DELETE |
152 | public void deletePortPair(@PathParam("pair_id") String id) { | 152 | public void deletePortPair(@PathParam("pair_id") String id) { |
153 | 153 | ||
154 | - PortPairId portPairId = PortPairId.portPairId(id); | 154 | + PortPairId portPairId = PortPairId.of(id); |
155 | Boolean isSuccess = nullIsNotFound(service.removePortPair(portPairId), | 155 | Boolean isSuccess = nullIsNotFound(service.removePortPair(portPairId), |
156 | PORT_PAIR_NOT_FOUND); | 156 | PORT_PAIR_NOT_FOUND); |
157 | if (!isSuccess) { | 157 | if (!isSuccess) { | ... | ... |
... | @@ -54,7 +54,7 @@ public final class PortPairCodec extends JsonCodec<PortPair> { | ... | @@ -54,7 +54,7 @@ public final class PortPairCodec extends JsonCodec<PortPair> { |
54 | 54 | ||
55 | String id = nullIsIllegal(json.get(ID), | 55 | String id = nullIsIllegal(json.get(ID), |
56 | ID + MISSING_MEMBER_MESSAGE).asText(); | 56 | ID + MISSING_MEMBER_MESSAGE).asText(); |
57 | - resultBuilder.setId(PortPairId.portPairId(id)); | 57 | + resultBuilder.setId(PortPairId.of(id)); |
58 | 58 | ||
59 | String tenantId = nullIsIllegal(json.get(TENANT_ID), | 59 | String tenantId = nullIsIllegal(json.get(TENANT_ID), |
60 | TENANT_ID + MISSING_MEMBER_MESSAGE).asText(); | 60 | TENANT_ID + MISSING_MEMBER_MESSAGE).asText(); | ... | ... |
... | @@ -75,7 +75,7 @@ public final class PortPairGroupCodec extends JsonCodec<PortPairGroup> { | ... | @@ -75,7 +75,7 @@ public final class PortPairGroupCodec extends JsonCodec<PortPairGroup> { |
75 | 75 | ||
76 | List<PortPairId> list = Lists.newArrayList(); | 76 | List<PortPairId> list = Lists.newArrayList(); |
77 | ArrayNode arrayNode = (ArrayNode) json.path(PORT_PAIRS); | 77 | ArrayNode arrayNode = (ArrayNode) json.path(PORT_PAIRS); |
78 | - arrayNode.forEach(i -> list.add(PortPairId.portPairId(i.asText()))); | 78 | + arrayNode.forEach(i -> list.add(PortPairId.of(i.asText()))); |
79 | resultBuilder.setPortPairs(list); | 79 | resultBuilder.setPortPairs(list); |
80 | 80 | ||
81 | return resultBuilder.build(); | 81 | return resultBuilder.build(); | ... | ... |
-
Please register or login to post a comment