blob: 8b8d2761d2b907b44a293cc0385fe0c3ccbac19c [file] [log] [blame]
Jian Lib68b9a82016-02-23 10:25:54 +09001/*
2 * Copyright 2016 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 */
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));
94 }
95
96 /**
97 * Checks that the data shared by all the resource is correct for a given region.
98 *
99 * @param region region to check
100 */
101 private void checkCommonData(Region region) {
102 assertThat(region.id().toString(), is("1"));
103 assertThat(region.type().toString(), is("ROOM"));
104 assertThat(region.name(), is("foo"));
105 }
106
107 /**
108 * Reads in a region from the given resource and decodes it.
109 *
110 * @param resourceName resource to use to read the JSON for the rule
111 * @return decoded region
112 * @throws IOException if processing the resource fails
113 */
114 private Region getRegion(String resourceName) throws IOException {
115 InputStream jsonStream = RegionCodecTest.class.getResourceAsStream(resourceName);
116 JsonNode json = context.mapper().readTree(jsonStream);
117 MatcherAssert.assertThat(json, notNullValue());
118 Region region = regionCodec.decode((ObjectNode) json, context);
119 assertThat(region, notNullValue());
120 return region;
121 }
122}