Moving all files to root directory
[appc.git] / appc-event-listener / appc-event-listener-bundle / src / test / java / org / openecomp / appc / listener / TestListenerProperties.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.appc.listener;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.junit.Assert.fail;
30
31 import java.util.Properties;
32
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.openecomp.appc.listener.AbstractListener;
36 import org.openecomp.appc.listener.ListenerProperties;
37 import org.openecomp.appc.listener.ListenerProperties.KEYS;
38
39 public class TestListenerProperties {
40
41     private Properties good, bad, both;
42     private String prefix;
43
44     private ListenerProperties props;
45
46     @Before
47     public void setup() {
48         prefix = "test";
49         good = new Properties();
50         bad = new Properties();
51         both = new Properties();
52
53         good.setProperty(String.format("%s.%s", prefix, "a"), "1");
54         good.setProperty(String.format("%s.%s", prefix, "a.b"), "2");
55         good.setProperty(String.format("%s.%s", prefix, "a.b.c"), "3");
56
57         bad.setProperty(prefix, "NA");
58         bad.setProperty(prefix + ".", "NA");
59         bad.setProperty(String.format("%s.%s", prefix + "x", "bad"), "NA");
60         bad.setProperty(String.format("%s.%s", "x" + prefix, "bad"), "NA");
61
62         for (String key : good.stringPropertyNames()) {
63             both.put(key, good.getProperty(key));
64         }
65         for (String key : bad.stringPropertyNames()) {
66             both.put(key, bad.getProperty(key));
67         }
68
69         props = new ListenerProperties(prefix, both);
70     }
71
72     @Test
73     public void testConstructor() {
74         props = new ListenerProperties(prefix, good);
75         assertEquals(prefix, props.getPrefix());
76         assertEquals(good.size(), props.getProperties().size());
77
78         props = new ListenerProperties(prefix, bad);
79         assertEquals(prefix, props.getPrefix());
80         assertTrue(props.getProperties().isEmpty());
81
82         props = new ListenerProperties(prefix, both);
83         assertEquals(prefix, props.getPrefix());
84         assertEquals(good.size(), props.getProperties().size());
85
86         for (Object val : props.getProperties().values()) {
87             assertFalse("NA".equals(val.toString()));
88         }
89
90         assertTrue(props.toString().contains(prefix));
91     }
92
93     @Test
94     public void testGetClass() {
95         assertNull(props.getListenerClass());
96         props.setListenerClass(AbstractListener.class);
97         assertNotNull(props.getListenerClass());
98         assertEquals(AbstractListener.class, props.getListenerClass());
99     }
100
101     @Test
102     public void testMessageServices() {
103         // Hardcode count so tests must be updated when values are added
104         assertEquals(1, ListenerProperties.MessageService.values().length);
105
106         // Bad Input
107         ListenerProperties.MessageService def = ListenerProperties.MessageService.DMaaP;
108         assertEquals(def, ListenerProperties.MessageService.parse(null));
109         assertEquals(def, ListenerProperties.MessageService.parse(""));
110         assertEquals(def, ListenerProperties.MessageService.parse("NotDmaapOrDMaaP"));
111        
112
113         // DMaaP case sensitivity
114         assertEquals(ListenerProperties.MessageService.DMaaP, ListenerProperties.MessageService.parse("dmaap"));
115         assertEquals(ListenerProperties.MessageService.DMaaP, ListenerProperties.MessageService.parse("DMAAP"));
116         assertEquals(ListenerProperties.MessageService.DMaaP, ListenerProperties.MessageService.parse("DMaaP"));
117     }
118
119     @Test
120     public void testKeys() {
121         // Hardcode count so tests must be updated when values are added
122         assertEquals(15, ListenerProperties.KEYS.values().length);
123
124         Properties tmp = new Properties();
125         try {
126             tmp.load(getClass().getResourceAsStream("/org/openecomp/appc/default.properties"));
127         } catch (Exception e) {
128             fail("Could not load properties to test");
129         }
130         String realPrefix = tmp.getProperty("test.prefix");
131         assertNotNull(realPrefix);
132         props = new ListenerProperties(realPrefix, tmp);
133
134         for (KEYS key : ListenerProperties.KEYS.values()) {
135             assertNotNull(key.getFullProp(realPrefix));
136             assertNotNull(props.getProperty(key));
137             assertNotNull(props.getProperty(key.getPropertySuffix()));
138         }
139     }
140
141     @Test
142     public void testDisabled() throws Exception {
143         assertFalse(props.isDisabled());
144         props.getProperties().put(KEYS.DISABLED.getPropertySuffix(), "TRUE");
145         assertTrue(props.isDisabled());
146         props.getProperties().put(KEYS.DISABLED.getPropertySuffix(), "N/A");
147         assertFalse(props.isDisabled());
148         props.getProperties().put(KEYS.DISABLED.getPropertySuffix(), "fAlse");
149         assertFalse(props.isDisabled());
150         props.getProperties().remove(KEYS.DISABLED.getPropertySuffix());
151         assertFalse(props.isDisabled());
152     }
153
154 }