add config modify LCM sample request
[appc.git] / appc-adapters / appc-dmaap-adapter / appc-dmaap-adapter-bundle / src / test / java / org / onap / appc / adapter / messaging / dmaap / impl / TestDmaapUtil.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ============LICENSE_END=========================================================
22  */
23
24 package org.onap.appc.adapter.messaging.dmaap.impl;
25
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;
31
32 import java.io.File;
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;
39
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 import org.onap.appc.configuration.ConfigurationFactory;
43
44 public class TestDmaapUtil {
45     private static Class<?>   configurationFactoryClass;
46     private static Field      configField;
47
48     @Test
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");
56
57         String junitFile = null;
58
59         // ensure file path property is not set
60         if (System.getProperty(DmaapUtil.DMAAP_PROPERTIES_PATH) != null) {
61             System.clearProperty(DmaapUtil.DMAAP_PROPERTIES_PATH);
62
63             // set configuration to null to force reloading of properties
64             try {
65                 configField.set(null, null);
66             } catch (IllegalArgumentException | IllegalAccessException e1) {
67                 // TODO Auto-generated catch block
68                 e1.printStackTrace();
69             }
70         }
71         try {
72             junitFile = DmaapUtil.createConsumerPropFile(topic, junitProps);
73         } catch (IOException e) {
74             e.printStackTrace();
75             fail("Exception creating consumer property file");
76         }
77
78         assertNotNull(junitFile);
79
80         // open file and verify properties
81         File testFile = new File(junitFile);
82         assertTrue(testFile.exists());
83
84         InputStream is = null;
85         Properties testProps = new Properties();
86         try {
87             is = new FileInputStream(testFile);
88             testProps.load(is);
89         } catch (FileNotFoundException e) {
90             e.printStackTrace();
91             fail("Exception opening consumer property file");
92         } catch (IOException e) {
93             e.printStackTrace();
94             fail("Exception opening consumer property file");
95         } finally {
96             try {
97                 if (is != null) {
98                     is.close();
99                 }
100             } catch (IOException ex) {
101                 ex.printStackTrace();
102                 fail("Exception closing consumer property file");
103             }
104         }
105
106         assertFalse(testProps.isEmpty());
107
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");
113     }
114
115     @Test
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");
123
124         String junitFile = null;
125
126         // set property for DMaaP profile
127         System.setProperty(DmaapUtil.DMAAP_PROPERTIES_PATH, "src/test/resources/org/onap/appc");
128
129         // set configuration to null to force reloading of properties
130         try {
131             configField.set(null, null);
132         } catch (IllegalArgumentException | IllegalAccessException e1) {
133             // TODO Auto-generated catch block
134             e1.printStackTrace();
135         }
136
137         try {
138             junitFile = DmaapUtil.createConsumerPropFile(topic, junitProps);
139         } catch (IOException e) {
140             e.printStackTrace();
141             fail("Exception creating consumer property file");
142         }
143
144         assertNotNull(junitFile);
145
146         // open file and verify properties
147         File testFile = new File(junitFile);
148         assertTrue(testFile.exists());
149
150         InputStream is = null;
151         Properties testProps = new Properties();
152         try {
153             is = new FileInputStream(testFile);
154             testProps.load(is);
155         } catch (FileNotFoundException e) {
156             e.printStackTrace();
157             fail("Exception opening consumer property file");
158         } catch (IOException e) {
159             e.printStackTrace();
160             fail("Exception opening consumer property file");
161         } finally {
162             try {
163                 if (is != null) {
164                     is.close();
165                 }
166             } catch (IOException ex) {
167                 ex.printStackTrace();
168                 fail("Exception closing consumer property file");
169             }
170         }
171
172         assertFalse(testProps.isEmpty());
173
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");
179     }
180
181     @Test
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");
189
190         String junitFile = null;
191
192         // ensure file path property is not set
193         if (System.getProperty(DmaapUtil.DMAAP_PROPERTIES_PATH) != null) {
194             System.clearProperty(DmaapUtil.DMAAP_PROPERTIES_PATH);
195
196             // set configuration to null to force reloading of properties
197             try {
198                 configField.set(null, null);
199             } catch (IllegalArgumentException | IllegalAccessException e1) {
200                 // TODO Auto-generated catch block
201                 e1.printStackTrace();
202             }
203         }
204
205         try {
206             junitFile = DmaapUtil.createProducerPropFile(topic, junitProps);
207         } catch (IOException e) {
208             e.printStackTrace();
209             fail("Exception creating consumer property file");
210         }
211
212         assertNotNull(junitFile);
213
214         // open file and verify properties
215         File testFile = new File(junitFile);
216         assertTrue(testFile.exists());
217
218         InputStream is = null;
219         Properties testProps = new Properties();
220         try {
221             is = new FileInputStream(testFile);
222             testProps.load(is);
223         } catch (FileNotFoundException e) {
224             e.printStackTrace();
225             fail("Exception opening consumer property file");
226         } catch (IOException e) {
227             e.printStackTrace();
228             fail("Exception opening consumer property file");
229         } finally {
230             try {
231                 if (is != null) {
232                     is.close();
233                 }
234             } catch (IOException ex) {
235                 ex.printStackTrace();
236                 fail("Exception closing consumer property file");
237             }
238         }
239
240         assertFalse(testProps.isEmpty());
241
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"));
247     }
248
249     /**
250      * Use reflection to locate fields and methods so that they can be
251      * manipulated during the test to change the internal state accordingly.
252      * 
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
259      */
260     @SuppressWarnings("nls")
261     @BeforeClass
262     public static void once() throws NoSuchFieldException, SecurityException, NoSuchMethodException {
263         configurationFactoryClass = ConfigurationFactory.class;
264
265         configField = configurationFactoryClass.getDeclaredField("config");
266         configField.setAccessible(true);
267     }
268 }