2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.adapter.messaging.dmaap.impl;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
30 import static org.junit.Assert.fail;
33 import java.io.FileInputStream;
34 import java.io.FileNotFoundException;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.lang.reflect.Field;
38 import java.util.Properties;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 import org.onap.appc.configuration.ConfigurationFactory;
44 public class TestDmaapUtil {
45 private static Class<?> configurationFactoryClass;
46 private static Field configField;
49 public void testCreateConsumerPropFile() {
50 String topic = "JunitTopicOne";
51 Properties junitProps = new Properties();
52 junitProps.put("host", "192.168.10.10");
53 junitProps.put("group", "junit-client");
54 junitProps.put("id", "junit-consumer-one");
55 junitProps.put("filter", "none");
57 String junitFile = null;
59 // ensure file path property is not set
60 if (System.getProperty(DmaapUtil.DMAAP_PROPERTIES_PATH) != null) {
61 System.clearProperty(DmaapUtil.DMAAP_PROPERTIES_PATH);
63 // set configuration to null to force reloading of properties
65 configField.set(null, null);
66 } catch (IllegalArgumentException | IllegalAccessException e1) {
67 // TODO Auto-generated catch block
72 junitFile = DmaapUtil.createConsumerPropFile(topic, junitProps);
73 } catch (IOException e) {
75 fail("Exception creating consumer property file");
78 assertNotNull(junitFile);
80 // open file and verify properties
81 File testFile = new File(junitFile);
82 assertTrue(testFile.exists());
84 InputStream is = null;
85 Properties testProps = new Properties();
87 is = new FileInputStream(testFile);
89 } catch (FileNotFoundException e) {
91 fail("Exception opening consumer property file");
92 } catch (IOException e) {
94 fail("Exception opening consumer property file");
100 } catch (IOException ex) {
101 ex.printStackTrace();
102 fail("Exception closing consumer property file");
106 assertFalse(testProps.isEmpty());
108 assertEquals(testProps.get("host"), "192.168.10.10");
109 assertEquals(testProps.get("group"), "junit-client");
110 assertEquals(testProps.get("id"), "junit-consumer-one");
111 assertEquals(testProps.get("filter"), "none");
112 assertEquals(testProps.get("TransportType"), "HTTPNOAUTH");
116 public void testCreateConsumerPropFileWithCustomProfile() {
117 String topic = "JunitTopicOne";
118 Properties junitProps = new Properties();
119 junitProps.put("host", "192.168.10.10");
120 junitProps.put("group", "junit-client");
121 junitProps.put("id", "junit-consumer-two");
122 junitProps.put("filter", "none");
124 String junitFile = null;
126 // set property for DMaaP profile
127 System.setProperty(DmaapUtil.DMAAP_PROPERTIES_PATH, "src/test/resources/org/onap/appc");
129 // set configuration to null to force reloading of properties
131 configField.set(null, null);
132 } catch (IllegalArgumentException | IllegalAccessException e1) {
133 // TODO Auto-generated catch block
134 e1.printStackTrace();
138 junitFile = DmaapUtil.createConsumerPropFile(topic, junitProps);
139 } catch (IOException e) {
141 fail("Exception creating consumer property file");
144 assertNotNull(junitFile);
146 // open file and verify properties
147 File testFile = new File(junitFile);
148 assertTrue(testFile.exists());
150 InputStream is = null;
151 Properties testProps = new Properties();
153 is = new FileInputStream(testFile);
155 } catch (FileNotFoundException e) {
157 fail("Exception opening consumer property file");
158 } catch (IOException e) {
160 fail("Exception opening consumer property file");
166 } catch (IOException ex) {
167 ex.printStackTrace();
168 fail("Exception closing consumer property file");
172 assertFalse(testProps.isEmpty());
174 assertEquals(testProps.get("host"), "192.168.10.10");
175 assertEquals(testProps.get("group"), "junit-client");
176 assertEquals(testProps.get("id"), "junit-consumer-two");
177 assertEquals(testProps.get("filter"), "none");
178 assertEquals(testProps.get("TransportType"), "HTTPAAF");
182 public void testCreateProducerPropFile() {
183 String topic = "JunitTopicOne";
184 Properties junitProps = new Properties();
185 junitProps.put("host", "192.168.10.10");
186 junitProps.put("group", "junit-client");
187 junitProps.put("id", "junit-producer-one");
188 junitProps.put("filter", "none");
190 String junitFile = null;
192 // ensure file path property is not set
193 if (System.getProperty(DmaapUtil.DMAAP_PROPERTIES_PATH) != null) {
194 System.clearProperty(DmaapUtil.DMAAP_PROPERTIES_PATH);
196 // set configuration to null to force reloading of properties
198 configField.set(null, null);
199 } catch (IllegalArgumentException | IllegalAccessException e1) {
200 // TODO Auto-generated catch block
201 e1.printStackTrace();
206 junitFile = DmaapUtil.createProducerPropFile(topic, junitProps);
207 } catch (IOException e) {
209 fail("Exception creating consumer property file");
212 assertNotNull(junitFile);
214 // open file and verify properties
215 File testFile = new File(junitFile);
216 assertTrue(testFile.exists());
218 InputStream is = null;
219 Properties testProps = new Properties();
221 is = new FileInputStream(testFile);
223 } catch (FileNotFoundException e) {
225 fail("Exception opening consumer property file");
226 } catch (IOException e) {
228 fail("Exception opening consumer property file");
234 } catch (IOException ex) {
235 ex.printStackTrace();
236 fail("Exception closing consumer property file");
240 assertFalse(testProps.isEmpty());
242 assertEquals(testProps.get("host"), "192.168.10.10");
243 assertEquals(testProps.get("group"), "junit-client");
244 assertEquals(testProps.get("id"), "junit-producer-one");
245 assertEquals(testProps.get("filter"), "none");
246 assertEquals("HTTPNOAUTH", testProps.get("TransportType"));
250 * Use reflection to locate fields and methods so that they can be
251 * manipulated during the test to change the internal state accordingly.
253 * @throws NoSuchFieldException
254 * if the field(s) dont exist
255 * @throws SecurityException
256 * if reflective access is not allowed
257 * @throws NoSuchMethodException
258 * If the method(s) dont exist
260 @SuppressWarnings("nls")
262 public static void once() throws NoSuchFieldException, SecurityException, NoSuchMethodException {
263 configurationFactoryClass = ConfigurationFactory.class;
265 configField = configurationFactoryClass.getDeclaredField("config");
266 configField.setAccessible(true);