blob: 626aa02cb04529975c6a3800d4062a8eae6bde72 [file] [log] [blame]
Jian Li7fe7eaf2018-12-31 17:00:33 +09001/*
2 * Copyright 2018-present Open Networking Foundation
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.openstacktelemetry.codec.rest;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import com.google.common.collect.ImmutableList;
22import com.google.common.collect.Maps;
23import org.hamcrest.MatcherAssert;
24import org.junit.Before;
25import org.junit.Test;
26import org.onosproject.codec.CodecContext;
27import org.onosproject.codec.JsonCodec;
28import org.onosproject.codec.impl.CodecManager;
29import org.onosproject.core.CoreService;
30import org.onosproject.openstacktelemetry.api.DefaultTelemetryConfig;
31import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
32
33import java.io.IOException;
34import java.io.InputStream;
35import java.util.HashMap;
36import java.util.Map;
37
38import static junit.framework.TestCase.assertEquals;
39import static junit.framework.TestCase.assertTrue;
40import static org.easymock.EasyMock.createMock;
41import static org.easymock.EasyMock.expect;
42import static org.easymock.EasyMock.replay;
43import static org.hamcrest.MatcherAssert.assertThat;
44import static org.hamcrest.Matchers.notNullValue;
45import static org.onosproject.net.NetTestTools.APP_ID;
46import static org.onosproject.openstacktelemetry.codec.rest.TelemetryConfigJsonMatcher.matchesTelemetryConfig;
47
48/**
49 * Unit tests for TelemetryConfig codec.
50 */
51public class TelemetryConfigCodecTest {
52
53 MockCodecContext context;
54
55 JsonCodec<TelemetryConfig> telemetryConfigCodec;
56
57 final CoreService mockCoreService = createMock(CoreService.class);
58 private static final String REST_APP_ID = "org.onosproject.rest";
59
60 @Before
61 public void setUp() {
62 context = new MockCodecContext();
63 telemetryConfigCodec = new TelemetryConfigJsonCodec();
64
65 assertThat(telemetryConfigCodec, notNullValue());
66
67 expect(mockCoreService.registerApplication(REST_APP_ID))
68 .andReturn(APP_ID).anyTimes();
69 replay(mockCoreService);
70 context.registerService(CoreService.class, mockCoreService);
71 }
72
73 /**
74 * Tests the telemetry config encoding.
75 */
76 @Test
77 public void testTelemetryConfigEncode() {
78
79 String name = "grpc";
80 TelemetryConfig.ConfigType type = TelemetryConfig.ConfigType.GRPC;
81 String manufacturer = "grpc.io";
82 String swVersion = "1.0";
83 boolean enabled = true;
84
85 Map<String, String> properties = Maps.newConcurrentMap();
86 properties.put("key1", "value1");
87 properties.put("key2", "value2");
88
89 TelemetryConfig config = new DefaultTelemetryConfig(name, type,
90 ImmutableList.of(), manufacturer, swVersion, enabled, properties);
91
92 ObjectNode configJson = telemetryConfigCodec.encode(config, context);
93 assertThat(configJson, matchesTelemetryConfig(config));
94 }
95
96 /**
97 * Tests the telemetry config decoding.
98 */
99 @Test
100 public void testTelemetryConfigDecode() throws IOException {
101 TelemetryConfig config = getTelemetryConfig("TelemetryConfig.json");
102
103 assertEquals(config.name(), "grpc-config");
104 assertEquals(config.type().name(), "GRPC");
105 assertEquals(config.manufacturer(), "grpc.io");
106 assertEquals(config.swVersion(), "1.0");
107 assertTrue(config.enabled());
108
109 config.properties().forEach((k, v) -> {
110 if (k.equals("address")) {
111 assertEquals(v, "127.0.0.1");
112 }
113 if (k.equals("port")) {
114 assertEquals(v, "9092");
115 }
116 });
117 }
118
119 /**
120 * Reads in a telemetry config from the given resource and decodes it.
121 *
122 * @param resourceName resource to use to read the JSON for the rule
123 * @return decoded telemetry config
124 * @throws IOException if processing the resource fails
125 */
126 private TelemetryConfig getTelemetryConfig(String resourceName) throws IOException {
127 InputStream jsonStream = TelemetryConfigCodecTest.class.getResourceAsStream(resourceName);
128 JsonNode json = context.mapper().readTree(jsonStream);
129 MatcherAssert.assertThat(json, notNullValue());
130 TelemetryConfig config = telemetryConfigCodec.decode((ObjectNode) json, context);
131 assertThat(config, notNullValue());
132 return config;
133 }
134
135 /**
136 * Mock codec context for use in codec unit tests.
137 */
138 private class MockCodecContext implements CodecContext {
139 private final ObjectMapper mapper = new ObjectMapper();
140 private final CodecManager manager = new CodecManager();
141 private final Map<Class<?>, Object> services = new HashMap<>();
142
143 /**
144 * Constructs a new mock codec context.
145 */
146 public MockCodecContext() {
147 manager.activate();
148 }
149
150 @Override
151 public ObjectMapper mapper() {
152 return mapper;
153 }
154
155 @SuppressWarnings("unchecked")
156 @Override
157 public <T> JsonCodec<T> codec(Class<T> entityClass) {
158 if (entityClass == TelemetryConfig.class) {
159 return (JsonCodec<T>) telemetryConfigCodec;
160 }
161 return manager.getCodec(entityClass);
162 }
163
164 @SuppressWarnings("unchecked")
165 @Override
166 public <T> T getService(Class<T> serviceClass) {
167 return (T) services.get(serviceClass);
168 }
169
170 // for registering mock services
171 public <T> void registerService(Class<T> serviceClass, T impl) {
172 services.put(serviceClass, impl);
173 }
174 }
175}