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