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