2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.openecomp.appc.listener;
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;
31 import java.util.Properties;
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;
40 public class TestListenerProperties {
42 private Properties good, bad, both;
43 private String prefix;
45 private ListenerProperties props;
50 good = new Properties();
51 bad = new Properties();
52 both = new Properties();
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");
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");
63 for (String key : good.stringPropertyNames()) {
64 both.put(key, good.getProperty(key));
66 for (String key : bad.stringPropertyNames()) {
67 both.put(key, bad.getProperty(key));
70 props = new ListenerProperties(prefix, both);
74 public void testConstructor() {
75 props = new ListenerProperties(prefix, good);
76 assertEquals(prefix, props.getPrefix());
77 assertEquals(good.size(), props.getProperties().size());
79 props = new ListenerProperties(prefix, bad);
80 assertEquals(prefix, props.getPrefix());
81 assertTrue(props.getProperties().isEmpty());
83 props = new ListenerProperties(prefix, both);
84 assertEquals(prefix, props.getPrefix());
85 assertEquals(good.size(), props.getProperties().size());
87 for (Object val : props.getProperties().values()) {
88 assertFalse("NA".equals(val.toString()));
91 assertTrue(props.toString().contains(prefix));
95 public void testGetClass() {
96 assertNull(props.getListenerClass());
97 props.setListenerClass(AbstractListener.class);
98 assertNotNull(props.getListenerClass());
99 assertEquals(AbstractListener.class, props.getListenerClass());
103 public void testMessageServices() {
104 // Hardcode count so tests must be updated when values are added
105 assertEquals(1, MessageService.values().length);
108 MessageService def = MessageService.DMaaP;
109 assertEquals(def, MessageService.parse(null));
110 assertEquals(def, MessageService.parse(""));
111 assertEquals(def, MessageService.parse("NotDMaaP"));
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"));
120 public void testKeys() {
121 // Hardcode count so tests must be updated when values are added
122 assertEquals(15, ListenerProperties.KEYS.values().length);
124 Properties tmp = new Properties();
126 tmp.load(getClass().getResourceAsStream("/org/openecomp/appc/default.properties"));
127 } catch (Exception e) {
128 fail("Could not load properties to test");
130 String realPrefix = tmp.getProperty("test.prefix");
131 assertNotNull(realPrefix);
132 props = new ListenerProperties(realPrefix, tmp);
134 for (KEYS key : ListenerProperties.KEYS.values()) {
135 assertNotNull(key.getFullProp(realPrefix));
136 assertNotNull(props.getProperty(key));
137 assertNotNull(props.getProperty(key.getPropertySuffix()));
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());