Merge "Removed the DMAAP Accepted/Rejected Call"
[nonrtric.git] / policy-agent / src / test / java / org / oransc / policyagent / configuration / ApplicationConfigParserTest.java
1 /*-
2  * ========================LICENSE_START=================================
3  * O-RAN-SC
4  * %%
5  * Copyright (C) 2019 Nordix Foundation
6  * %%
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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===================================
19  */
20
21 package org.oransc.policyagent.configuration;
22
23 import static org.junit.jupiter.api.Assertions.assertAll;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25
26 import com.google.common.base.Charsets;
27 import com.google.common.io.Resources;
28 import com.google.gson.JsonIOException;
29 import com.google.gson.JsonObject;
30 import com.google.gson.JsonParser;
31 import com.google.gson.JsonSyntaxException;
32 import java.io.ByteArrayInputStream;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.io.InputStreamReader;
36 import java.net.URL;
37 import java.nio.charset.StandardCharsets;
38 import java.util.Properties;
39 import org.junit.jupiter.api.Test;
40 import org.onap.dmaap.mr.test.clients.ProtocolTypeConstants;
41 import org.springframework.http.MediaType;
42
43 public class ApplicationConfigParserTest {
44
45     @Test
46     public void whenCorrectDmaapConfig() throws Exception {
47         JsonObject jsonRootObject = getJsonRootObject();
48
49         ApplicationConfigParser parserUnderTest = new ApplicationConfigParser();
50
51         parserUnderTest.parse(jsonRootObject);
52
53         Properties actualPublisherConfig = parserUnderTest.getDmaapPublisherConfig();
54         assertAll("publisherConfig",
55             () -> assertEquals("localhost:6845/events", actualPublisherConfig.get("ServiceName")),
56             () -> assertEquals("A1-POLICY-AGENT-WRITE", actualPublisherConfig.get("topic")),
57             () -> assertEquals("localhost:6845", actualPublisherConfig.get("host")),
58             () -> assertEquals(MediaType.APPLICATION_JSON.toString(), actualPublisherConfig.get("contenttype")),
59             () -> assertEquals("admin", actualPublisherConfig.get("userName")),
60             () -> assertEquals("admin", actualPublisherConfig.get("password")),
61             () -> assertEquals(ProtocolTypeConstants.HTTPNOAUTH.toString(), actualPublisherConfig.get("TransportType")),
62             () -> assertEquals(15000, actualPublisherConfig.get("timeout")),
63             () -> assertEquals(1000, actualPublisherConfig.get("limit")));
64
65         Properties actualConsumerConfig = parserUnderTest.getDmaapConsumerConfig();
66         assertAll("consumerConfig",
67             () -> assertEquals("localhost:6845/events", actualConsumerConfig.get("ServiceName")),
68             () -> assertEquals("A1-POLICY-AGENT-READ", actualConsumerConfig.get("topic")),
69             () -> assertEquals("localhost:6845", actualConsumerConfig.get("host")),
70             () -> assertEquals(MediaType.APPLICATION_JSON.toString(), actualConsumerConfig.get("contenttype")),
71             () -> assertEquals("admin", actualConsumerConfig.get("userName")),
72             () -> assertEquals("admin", actualConsumerConfig.get("password")),
73             () -> assertEquals("users", actualConsumerConfig.get("group")),
74             () -> assertEquals("policy-agent", actualConsumerConfig.get("id")),
75             () -> assertEquals(ProtocolTypeConstants.HTTPNOAUTH.toString(), actualConsumerConfig.get("TransportType")),
76             () -> assertEquals(15000, actualConsumerConfig.get("timeout")),
77             () -> assertEquals(1000, actualConsumerConfig.get("limit")));
78     }
79
80     private JsonObject getJsonRootObject() throws JsonIOException, JsonSyntaxException, IOException {
81         JsonObject rootObject = JsonParser.parseReader(new InputStreamReader(getCorrectJson())).getAsJsonObject();
82         return rootObject;
83     }
84
85     private static InputStream getCorrectJson() throws IOException {
86         URL url = ApplicationConfigParser.class.getClassLoader()
87             .getResource("test_application_configuration_with_dmaap_config.json");
88         String string = Resources.toString(url, Charsets.UTF_8);
89         return new ByteArrayInputStream((string.getBytes(StandardCharsets.UTF_8)));
90     }
91
92 }