2 * ========================LICENSE_START=================================
5 * Copyright (C) 2019 Nordix Foundation
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ========================LICENSE_END===================================
21 package org.oransc.policyagent.configuration;
23 import static org.junit.jupiter.api.Assertions.assertAll;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import com.google.common.base.Charsets;
26 import com.google.common.io.Resources;
27 import com.google.gson.JsonIOException;
28 import com.google.gson.JsonObject;
29 import com.google.gson.JsonParser;
30 import com.google.gson.JsonSyntaxException;
31 import java.io.ByteArrayInputStream;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.io.InputStreamReader;
36 import java.nio.charset.StandardCharsets;
37 import java.util.Properties;
38 import org.junit.jupiter.api.Test;
39 import org.onap.dmaap.mr.test.clients.ProtocolTypeConstants;
40 import org.springframework.http.MediaType;
42 public class ApplicationConfigParserTest {
45 public void whenCorrectDmaapConfig() throws Exception {
46 JsonObject jsonRootObject = getJsonRootObject();
48 ApplicationConfigParser parserUnderTest = new ApplicationConfigParser();
50 parserUnderTest.parse(jsonRootObject);
52 Properties actualPublisherConfig = parserUnderTest.getDmaapPublisherConfig();
53 assertAll("publisherConfig",
54 () -> assertEquals("localhost:6845/events", actualPublisherConfig.get("ServiceName")),
55 () -> assertEquals("A1-POLICY-AGENT-WRITE", actualPublisherConfig.get("topic")),
56 () -> assertEquals("localhost:6845", actualPublisherConfig.get("host")),
57 () -> assertEquals(MediaType.APPLICATION_JSON.toString(), actualPublisherConfig.get("contenttype")),
58 () -> assertEquals("admin", actualPublisherConfig.get("userName")),
59 () -> assertEquals("admin", actualPublisherConfig.get("password")),
60 () -> assertEquals(ProtocolTypeConstants.HTTPNOAUTH.toString(), actualPublisherConfig.get("TransportType")),
61 () -> assertEquals(15000, actualPublisherConfig.get("timeout")),
62 () -> assertEquals(100, actualPublisherConfig.get("limit")));
64 Properties actualConsumerConfig = parserUnderTest.getDmaapConsumerConfig();
65 assertAll("consumerConfig",
66 () -> assertEquals("localhost:6845/events", actualConsumerConfig.get("ServiceName")),
67 () -> assertEquals("A1-POLICY-AGENT-READ", actualConsumerConfig.get("topic")),
68 () -> assertEquals("localhost:6845", actualConsumerConfig.get("host")),
69 () -> assertEquals(MediaType.APPLICATION_JSON.toString(), actualConsumerConfig.get("contenttype")),
70 () -> assertEquals("admin", actualConsumerConfig.get("userName")),
71 () -> assertEquals("admin", actualConsumerConfig.get("password")),
72 () -> assertEquals("users", actualConsumerConfig.get("group")),
73 () -> assertEquals("policy-agent", actualConsumerConfig.get("id")),
74 () -> assertEquals(ProtocolTypeConstants.HTTPNOAUTH.toString(), actualConsumerConfig.get("TransportType")),
75 () -> assertEquals(15000, actualConsumerConfig.get("timeout")),
76 () -> assertEquals(100, actualConsumerConfig.get("limit")));
79 private JsonObject getJsonRootObject() throws JsonIOException, JsonSyntaxException, IOException {
80 JsonObject rootObject = JsonParser.parseReader(new InputStreamReader(getCorrectJson())).getAsJsonObject();
84 private static InputStream getCorrectJson() throws IOException {
85 URL url = ApplicationConfigParser.class.getClassLoader()
86 .getResource("test_application_configuration_with_dmaap_config.json");
87 String string = Resources.toString(url, Charsets.UTF_8);
88 return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));