Jian Li | b68b9a8 | 2016-02-23 10:25:54 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Jian Li | b68b9a8 | 2016-02-23 10:25:54 +0900 | [diff] [blame] | 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.codec.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import com.google.common.collect.ImmutableList; |
| 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import org.hamcrest.MatcherAssert; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
| 25 | import org.onosproject.cluster.NodeId; |
| 26 | import org.onosproject.codec.JsonCodec; |
| 27 | import org.onosproject.core.CoreService; |
| 28 | import org.onosproject.net.region.DefaultRegion; |
| 29 | import org.onosproject.net.region.Region; |
| 30 | import org.onosproject.net.region.RegionId; |
| 31 | |
| 32 | import java.io.IOException; |
| 33 | import java.io.InputStream; |
| 34 | import java.util.List; |
| 35 | import java.util.Set; |
| 36 | |
| 37 | import static org.easymock.EasyMock.createMock; |
| 38 | import static org.hamcrest.Matchers.is; |
| 39 | import static org.hamcrest.Matchers.notNullValue; |
| 40 | import static org.junit.Assert.assertThat; |
| 41 | import static org.onosproject.codec.impl.RegionJsonMatcher.matchesRegion; |
| 42 | |
| 43 | /** |
| 44 | * Unit tests for region codec. |
| 45 | */ |
| 46 | public class RegionCodecTest { |
| 47 | |
| 48 | MockCodecContext context; |
| 49 | JsonCodec<Region> regionCodec; |
| 50 | final CoreService mockCoreService = createMock(CoreService.class); |
| 51 | |
| 52 | @Before |
| 53 | public void setUp() { |
| 54 | context = new MockCodecContext(); |
| 55 | regionCodec = context.codec(Region.class); |
| 56 | assertThat(regionCodec, notNullValue()); |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Tests encoding of a Region object. |
| 61 | */ |
| 62 | @Test |
| 63 | public void testRegionEncode() { |
| 64 | NodeId nodeId1 = NodeId.nodeId("1"); |
| 65 | NodeId nodeId2 = NodeId.nodeId("2"); |
| 66 | NodeId nodeId3 = NodeId.nodeId("3"); |
| 67 | NodeId nodeId4 = NodeId.nodeId("4"); |
| 68 | |
| 69 | Set<NodeId> set1 = ImmutableSet.of(nodeId1); |
| 70 | Set<NodeId> set2 = ImmutableSet.of(nodeId1, nodeId2); |
| 71 | Set<NodeId> set3 = ImmutableSet.of(nodeId1, nodeId2, nodeId3); |
| 72 | Set<NodeId> set4 = ImmutableSet.of(nodeId1, nodeId2, nodeId3, nodeId4); |
| 73 | List<Set<NodeId>> masters = ImmutableList.of(set1, set2, set3, set4); |
| 74 | |
| 75 | RegionId regionId = RegionId.regionId("1"); |
| 76 | String name = "foo"; |
| 77 | Region.Type type = Region.Type.ROOM; |
| 78 | |
| 79 | Region region = new DefaultRegion(regionId, name, type, masters); |
| 80 | |
| 81 | ObjectNode regionJson = regionCodec.encode(region, context); |
| 82 | assertThat(regionJson, matchesRegion(region)); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Tests decoding of a json object. |
| 87 | */ |
| 88 | @Test |
| 89 | public void testRegionDecode() throws IOException { |
| 90 | Region region = getRegion("Region.json"); |
| 91 | checkCommonData(region); |
| 92 | |
| 93 | assertThat(region.masters().size(), is(2)); |
Jian Li | 79359df | 2016-02-24 17:29:54 +0900 | [diff] [blame] | 94 | |
| 95 | NodeId nodeId1 = NodeId.nodeId("1"); |
| 96 | NodeId nodeId2 = NodeId.nodeId("2"); |
| 97 | Set<NodeId> nodeIds1 = region.masters().get(0); |
| 98 | Set<NodeId> nodeIds2 = region.masters().get(1); |
| 99 | assertThat(nodeIds1.containsAll(ImmutableSet.of(nodeId1)), is(true)); |
| 100 | assertThat(nodeIds2.containsAll(ImmutableSet.of(nodeId1, nodeId2)), is(true)); |
Jian Li | b68b9a8 | 2016-02-23 10:25:54 +0900 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Checks that the data shared by all the resource is correct for a given region. |
| 105 | * |
| 106 | * @param region region to check |
| 107 | */ |
| 108 | private void checkCommonData(Region region) { |
| 109 | assertThat(region.id().toString(), is("1")); |
| 110 | assertThat(region.type().toString(), is("ROOM")); |
| 111 | assertThat(region.name(), is("foo")); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Reads in a region from the given resource and decodes it. |
| 116 | * |
| 117 | * @param resourceName resource to use to read the JSON for the rule |
| 118 | * @return decoded region |
| 119 | * @throws IOException if processing the resource fails |
| 120 | */ |
| 121 | private Region getRegion(String resourceName) throws IOException { |
| 122 | InputStream jsonStream = RegionCodecTest.class.getResourceAsStream(resourceName); |
| 123 | JsonNode json = context.mapper().readTree(jsonStream); |
| 124 | MatcherAssert.assertThat(json, notNullValue()); |
| 125 | Region region = regionCodec.decode((ObjectNode) json, context); |
| 126 | assertThat(region, notNullValue()); |
| 127 | return region; |
| 128 | } |
| 129 | } |