2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ============LICENSE_END=========================================================
24 package org.onap.appc.listener;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertNull;
30 import static org.junit.Assert.assertTrue;
31 import static org.junit.Assert.fail;
33 import java.util.Properties;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.onap.appc.adapter.factory.MessageService;
38 import org.onap.appc.listener.AbstractListener;
39 import org.onap.appc.listener.ListenerProperties;
40 import org.onap.appc.listener.ListenerProperties.KEYS;
42 public class ListenerPropertiesTest {
44 private Properties good, bad, both;
45 private String prefix;
47 private ListenerProperties props;
52 good = new Properties();
53 bad = new Properties();
54 both = new Properties();
56 good.setProperty(String.format("%s.%s", prefix, "a"), "1");
57 good.setProperty(String.format("%s.%s", prefix, "a.b"), "2");
58 good.setProperty(String.format("%s.%s", prefix, "a.b.c"), "3");
60 bad.setProperty(prefix, "NA");
61 bad.setProperty(prefix + ".", "NA");
62 bad.setProperty(String.format("%s.%s", prefix + "x", "bad"), "NA");
63 bad.setProperty(String.format("%s.%s", "x" + prefix, "bad"), "NA");
65 for (String key : good.stringPropertyNames()) {
66 both.put(key, good.getProperty(key));
68 for (String key : bad.stringPropertyNames()) {
69 both.put(key, bad.getProperty(key));
72 props = new ListenerProperties(prefix, both);
76 public void testConstructor() {
77 props = new ListenerProperties(prefix, good);
78 assertEquals(prefix, props.getPrefix());
79 assertEquals(good.size(), props.getProperties().size());
81 props = new ListenerProperties(prefix, bad);
82 assertEquals(prefix, props.getPrefix());
83 assertTrue(props.getProperties().isEmpty());
85 props = new ListenerProperties(prefix, both);
86 assertEquals(prefix, props.getPrefix());
87 assertEquals(good.size(), props.getProperties().size());
89 for (Object val : props.getProperties().values()) {
90 assertFalse("NA".equals(val.toString()));
93 assertTrue(props.toString().contains(prefix));
97 public void testGetClass() {
98 assertNull(props.getListenerClass());
99 props.setListenerClass(AbstractListener.class);
100 assertNotNull(props.getListenerClass());
101 assertEquals(AbstractListener.class, props.getListenerClass());
105 public void testMessageServices() {
106 // Hardcode count so tests must be updated when values are added
107 assertEquals(1, MessageService.values().length);
110 MessageService def = MessageService.DMaaP;
111 assertEquals(def, MessageService.parse(null));
112 assertEquals(def, MessageService.parse(""));
113 assertEquals(def, MessageService.parse("NotDMaaP"));
115 // DMaaP case sensitivity
116 assertEquals(MessageService.DMaaP, MessageService.parse("dmaap"));
117 assertEquals(MessageService.DMaaP, MessageService.parse("DMAAP"));
118 assertEquals(MessageService.DMaaP, MessageService.parse("DMaaP"));
122 public void testKeys() {
123 // Hardcode count so tests must be updated when values are added
124 assertEquals(19, ListenerProperties.KEYS.values().length);
126 Properties tmp = new Properties();
128 tmp.load(getClass().getResourceAsStream("/org/onap/appc/default.properties"));
129 } catch (Exception e) {
130 fail("Could not load properties to test");
132 String realPrefix = tmp.getProperty("test.prefix");
133 assertNotNull(realPrefix);
134 props = new ListenerProperties(realPrefix, tmp);
136 for (KEYS key : ListenerProperties.KEYS.values()) {
137 assertNotNull(key.getFullProp(realPrefix));
138 assertNotNull(props.getProperty(key));
139 assertNotNull(props.getProperty(key.getPropertySuffix()));
144 public void testDisabled() throws Exception {
145 assertFalse(props.isDisabled());
146 props.getProperties().put(KEYS.DISABLED.getPropertySuffix(), "TRUE");
147 assertTrue(props.isDisabled());
148 props.getProperties().put(KEYS.DISABLED.getPropertySuffix(), "N/A");
149 assertFalse(props.isDisabled());
150 props.getProperties().put(KEYS.DISABLED.getPropertySuffix(), "fAlse");
151 assertFalse(props.isDisabled());
152 props.getProperties().remove(KEYS.DISABLED.getPropertySuffix());
153 assertFalse(props.isDisabled());