blob: 05d95e1be0567fe111d448f178aaeb510391e47c [file] [log] [blame]
Jian Lib68b9a82016-02-23 10:25:54 +09001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Jian Lib68b9a82016-02-23 10:25:54 +09003 *
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 */
16package org.onosproject.codec.impl;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import com.google.common.collect.ImmutableList;
21import com.google.common.collect.ImmutableSet;
22import org.hamcrest.MatcherAssert;
23import org.junit.Before;
24import org.junit.Test;
25import org.onosproject.cluster.NodeId;
26import org.onosproject.codec.JsonCodec;
27import org.onosproject.core.CoreService;
28import org.onosproject.net.region.DefaultRegion;
29import org.onosproject.net.region.Region;
30import org.onosproject.net.region.RegionId;
31
32import java.io.IOException;
33import java.io.InputStream;
34import java.util.List;
35import java.util.Set;
36
37import static org.easymock.EasyMock.createMock;
38import static org.hamcrest.Matchers.is;
39import static org.hamcrest.Matchers.notNullValue;
40import static org.junit.Assert.assertThat;
41import static org.onosproject.codec.impl.RegionJsonMatcher.matchesRegion;
42
43/**
44 * Unit tests for region codec.
45 */
46public 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 Li79359df2016-02-24 17:29:54 +090094
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 Lib68b9a82016-02-23 10:25:54 +0900101 }
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}