blob: b246939994d780552531fd064a6667881a6c818d [file] [log] [blame]
Jian Lie1c1c8d2016-05-09 16:24:40 -07001/*
2 * Copyright 2016-present 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 org.hamcrest.Description;
21import org.hamcrest.TypeSafeDiagnosingMatcher;
22import org.junit.Before;
23import org.junit.Test;
24import org.onosproject.codec.JsonCodec;
25import org.onosproject.core.ApplicationId;
26import org.onosproject.core.DefaultApplicationId;
27
28import java.io.IOException;
29import java.io.InputStream;
30
31import static org.hamcrest.MatcherAssert.assertThat;
32import static org.hamcrest.Matchers.is;
33import static org.hamcrest.Matchers.notNullValue;
34
35/**
36 * Unit tests for ApplicationId codec.
37 */
38public final class ApplicationIdCodecTest {
39
40 MockCodecContext context;
41 JsonCodec<ApplicationId> applicationIdCodec;
42
43 /**
44 * Sets up for each test. Creates a context and fetches the applicationId
45 * codec.
46 */
47 @Before
48 public void setUp() {
49 context = new MockCodecContext();
50 applicationIdCodec = context.codec(ApplicationId.class);
51 assertThat(applicationIdCodec, notNullValue());
52 }
53
54 /**
55 * Tests encoding of an application id object.
56 */
57 @Test
58 public void testApplicationIdEncode() {
59
60 int id = 1;
61 String name = "org.onosproject.foo";
62 ApplicationId appId = new DefaultApplicationId(id, name);
63
64 ObjectNode applicationIdJson = applicationIdCodec.encode(appId, context);
65 assertThat(applicationIdJson, ApplicationIdJsonMatcher.matchesApplicationId(appId));
66 }
67
68 /**
69 * Tests decoding of an application id object.
70 */
71 @Test
72 public void testApplicationIdDecode() throws IOException {
73 ApplicationId appId = getApplicationId("ApplicationId.json");
74
75 assertThat((int) appId.id(), is(1));
76 assertThat(appId.name(), is("org.onosproject.foo"));
77 }
78
79 private static final class ApplicationIdJsonMatcher extends TypeSafeDiagnosingMatcher<JsonNode> {
80
81 private final ApplicationId applicationId;
82
83 private ApplicationIdJsonMatcher(ApplicationId applicationId) {
84 this.applicationId = applicationId;
85 }
86
87 @Override
88 protected boolean matchesSafely(JsonNode jsonNode, Description description) {
89
90 // check application role
91 int jsonAppId = jsonNode.get("id").asInt();
92 int appId = applicationId.id();
93 if (jsonAppId != appId) {
94 description.appendText("application ID was " + jsonAppId);
95 return false;
96 }
97
98 String jsonAppName = jsonNode.get("name").asText();
99 String appName = applicationId.name();
100
101 if (!jsonAppName.equals(appName)) {
102 description.appendText("application name was " + jsonAppName);
103 return false;
104 }
105
106 return true;
107 }
108
109 @Override
110 public void describeTo(Description description) {
111 description.appendText(applicationId.toString());
112 }
113
114 static ApplicationIdJsonMatcher matchesApplicationId(ApplicationId applicationId) {
115 return new ApplicationIdJsonMatcher(applicationId);
116 }
117 }
118
119 /**
120 * Reads in a application id from the given resource and decodes it.
121 *
122 * @param resourceName resource to use to read the JSON for the rule
123 * @return decoded application id
124 * @throws IOException if processing the resource fails
125 */
126 private ApplicationId getApplicationId(String resourceName) throws IOException {
127 InputStream jsonStream = ApplicationIdCodecTest.class.getResourceAsStream(resourceName);
128 JsonNode json = context.mapper().readTree(jsonStream);
129 assertThat(json, notNullValue());
130 ApplicationId applicationId = applicationIdCodec.decode((ObjectNode) json, context);
131 assertThat(applicationId, notNullValue());
132 return applicationId;
133 }
134}