Merge of new rebased code
[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.adapter.factory.MessageService;
36 import org.openecomp.appc.listener.AbstractListener;
37 import org.openecomp.appc.listener.ListenerProperties;
38 import org.openecomp.appc.listener.ListenerProperties.KEYS;
39
40 public class TestListenerProperties {
41
42     private Properties good, bad, both;
43     private String prefix;
44
45     private ListenerProperties props;
46
47     @Before
48     public void setup() {
49         prefix = "test";
50         good = new Properties();
51         bad = new Properties();
52         both = new Properties();
53
54         good.setProperty(String.format("%s.%s", prefix, "a"), "1");
55         good.setProperty(String.format("%s.%s", prefix, "a.b"), "2");
56         good.setProperty(String.format("%s.%s", prefix, "a.b.c"), "3");
57
58         bad.setProperty(prefix, "NA");
59         bad.setProperty(prefix + ".", "NA");
60         bad.setProperty(String.format("%s.%s", prefix + "x", "bad"), "NA");
61         bad.setProperty(String.format("%s.%s", "x" + prefix, "bad"), "NA");
62
63         for (String key : good.stringPropertyNames()) {
64             both.put(key, good.getProperty(key));
65         }
66         for (String key : bad.stringPropertyNames()) {
67             both.put(key, bad.getProperty(key));
68         }
69
70         props = new ListenerProperties(prefix, both);
71     }
72
73     @Test
74     public void testConstructor() {
75         props = new ListenerProperties(prefix, good);
76         assertEquals(prefix, props.getPrefix());
77         assertEquals(good.size(), props.getProperties().size());
78
79         props = new ListenerProperties(prefix, bad);
80         assertEquals(prefix, props.getPrefix());
81         assertTrue(props.getProperties().isEmpty());
82
83         props = new ListenerProperties(prefix, both);
84         assertEquals(prefix, props.getPrefix());
85         assertEquals(good.size(), props.getProperties().size());
86
87         for (Object val : props.getProperties().values()) {
88             assertFalse("NA".equals(val.toString()));
89         }
90
91         assertTrue(props.toString().contains(prefix));
92     }
93
94     @Test
95     public void testGetClass() {
96         assertNull(props.getListenerClass());
97         props.setListenerClass(AbstractListener.class);
98         assertNotNull(props.getListenerClass());
99         assertEquals(AbstractListener.class, props.getListenerClass());
100     }
101
102     @Test
103     public void testMessageServices() {
104         // Hardcode count so tests must be updated when values are added
105         assertEquals(1, MessageService.values().length);
106
107         // Bad Input
108         MessageService def = MessageService.DMaaP;
109         assertEquals(def, MessageService.parse(null));
110         assertEquals(def, MessageService.parse(""));
111         assertEquals(def, MessageService.parse("NotDMaaP"));
112        
113         // DMaaP case sensitivity
114         assertEquals(MessageService.DMaaP, MessageService.parse("dmaap"));
115         assertEquals(MessageService.DMaaP, MessageService.parse("DMAAP"));
116         assertEquals(MessageService.DMaaP, 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 }