Dmaap micro service jar
[appc.git] / services / appc-dmaap-service / appc-event-listener-bundle / src / test / java / org / onap / appc / listener / ListenerPropertiesTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2019 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.listener;
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.assertNull;
30 import static org.junit.Assert.assertTrue;
31 import static org.junit.Assert.fail;
32
33 import java.util.Properties;
34
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.onap.appc.adapter.factory.MessageService;
38 import org.onap.appc.listener.AbstractListener;
39 import org.onap.appc.listener.ListenerProperties;
40 import org.onap.appc.listener.ListenerProperties.KEYS;
41
42 public class ListenerPropertiesTest {
43
44     private Properties good, bad, both;
45     private String prefix;
46
47     private ListenerProperties props;
48
49     @Before
50     public void setup() {
51         prefix = "test";
52         good = new Properties();
53         bad = new Properties();
54         both = new Properties();
55
56         good.setProperty(String.format("%s.%s", prefix, "a"), "1");
57         good.setProperty(String.format("%s.%s", prefix, "a.b"), "2");
58         good.setProperty(String.format("%s.%s", prefix, "a.b.c"), "3");
59
60         bad.setProperty(prefix, "NA");
61         bad.setProperty(prefix + ".", "NA");
62         bad.setProperty(String.format("%s.%s", prefix + "x", "bad"), "NA");
63         bad.setProperty(String.format("%s.%s", "x" + prefix, "bad"), "NA");
64
65         for (String key : good.stringPropertyNames()) {
66             both.put(key, good.getProperty(key));
67         }
68         for (String key : bad.stringPropertyNames()) {
69             both.put(key, bad.getProperty(key));
70         }
71
72         props = new ListenerProperties(prefix, both);
73     }
74
75     @Test
76     public void testConstructor() {
77         props = new ListenerProperties(prefix, good);
78         assertEquals(prefix, props.getPrefix());
79         assertEquals(good.size(), props.getProperties().size());
80
81         props = new ListenerProperties(prefix, bad);
82         assertEquals(prefix, props.getPrefix());
83         assertTrue(props.getProperties().isEmpty());
84
85         props = new ListenerProperties(prefix, both);
86         assertEquals(prefix, props.getPrefix());
87         assertEquals(good.size(), props.getProperties().size());
88
89         for (Object val : props.getProperties().values()) {
90             assertFalse("NA".equals(val.toString()));
91         }
92
93         assertTrue(props.toString().contains(prefix));
94     }
95
96     @Test
97     public void testGetClass() {
98         assertNull(props.getListenerClass());
99         props.setListenerClass(AbstractListener.class);
100         assertNotNull(props.getListenerClass());
101         assertEquals(AbstractListener.class, props.getListenerClass());
102     }
103
104     @Test
105     public void testMessageServices() {
106         // Hardcode count so tests must be updated when values are added
107         assertEquals(1, MessageService.values().length);
108
109         // Bad Input
110         MessageService def = MessageService.DMaaP;
111         assertEquals(def, MessageService.parse(null));
112         assertEquals(def, MessageService.parse(""));
113         assertEquals(def, MessageService.parse("NotDMaaP"));
114        
115         // DMaaP case sensitivity
116         assertEquals(MessageService.DMaaP, MessageService.parse("dmaap"));
117         assertEquals(MessageService.DMaaP, MessageService.parse("DMAAP"));
118         assertEquals(MessageService.DMaaP, MessageService.parse("DMaaP"));
119     }
120
121     @Test
122     public void testKeys() {
123         // Hardcode count so tests must be updated when values are added
124         assertEquals(19, ListenerProperties.KEYS.values().length);
125
126         Properties tmp = new Properties();
127         try {
128             tmp.load(getClass().getResourceAsStream("/org/onap/appc/default.properties"));
129         } catch (Exception e) {
130             fail("Could not load properties to test");
131         }
132         String realPrefix = tmp.getProperty("test.prefix");
133         assertNotNull(realPrefix);
134         props = new ListenerProperties(realPrefix, tmp);
135
136         for (KEYS key : ListenerProperties.KEYS.values()) {
137             assertNotNull(key.getFullProp(realPrefix));
138             assertNotNull(props.getProperty(key));
139             assertNotNull(props.getProperty(key.getPropertySuffix()));
140         }
141     }
142
143     @Test
144     public void testDisabled() throws Exception {
145         assertFalse(props.isDisabled());
146         props.getProperties().put(KEYS.DISABLED.getPropertySuffix(), "TRUE");
147         assertTrue(props.isDisabled());
148         props.getProperties().put(KEYS.DISABLED.getPropertySuffix(), "N/A");
149         assertFalse(props.isDisabled());
150         props.getProperties().put(KEYS.DISABLED.getPropertySuffix(), "fAlse");
151         assertFalse(props.isDisabled());
152         props.getProperties().remove(KEYS.DISABLED.getPropertySuffix());
153         assertFalse(props.isDisabled());
154     }
155
156 }