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