2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.common.endpoints.event.comm.bus;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX;
27 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS;
28 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX;
30 import java.util.Arrays;
31 import java.util.Collections;
32 import java.util.List;
33 import java.util.Properties;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
38 public class NoopTopicSinkFactoryTest extends TopicFactoryTestBase<NoopTopicSink> {
40 private static final List<String> NOOP_SERVERS = Arrays.asList("noop");
42 private IndexedNoopTopicSinkFactory factory;
45 * Creates the object to be tested.
51 factory = new IndexedNoopTopicSinkFactory();
55 public void tearDown() {
60 public void testBuildListOfStringStringBoolean() {
63 NoopTopicSink item1 = buildTopic(servers, MY_TOPIC, true);
66 assertEquals(servers, item1.getServers());
67 assertEquals(MY_TOPIC, item1.getTopic());
69 // managed topic - should not build a new one
70 assertEquals(item1, buildTopic(servers, MY_TOPIC, true));
72 NoopTopicSink item2 = buildTopic(servers, TOPIC2, true);
74 assertTrue(item1 != item2);
76 // duplicate - should be the same, as these topics are managed
77 NoopTopicSink item3 = buildTopic(Collections.emptyList(), TOPIC2, true);
78 assertTrue(item2 == item3);
82 assertEquals(NOOP_SERVERS, buildTopic(null, MY_TOPIC, true).getServers());
86 assertEquals(NOOP_SERVERS, buildTopic(Collections.emptyList(), MY_TOPIC, true).getServers());
90 item1 = buildTopic(servers, MY_TOPIC, false);
91 assertTrue(item1 != buildTopic(servers, MY_TOPIC, false));
94 @Test(expected = IllegalArgumentException.class)
95 public void testBuildListOfStringStringBoolean_NullTopic() {
96 buildTopic(servers, null, true);
99 @Test(expected = IllegalArgumentException.class)
100 public void testBuildListOfStringStringBoolean_EmptyTopic() {
101 buildTopic(servers, "", true);
105 public void testBuildProperties() {
108 assertEquals(1, buildTopics(makePropBuilder().makeTopic(MY_TOPIC).build()).size());
109 assertNotNull(factory.get(MY_TOPIC));
111 // unmanaged topic - get() will throw an exception
113 assertEquals(1, buildTopics(makePropBuilder().makeTopic(MY_TOPIC)
114 .setTopicProperty(PROPERTY_MANAGED_SUFFIX, "false").build()).size());
115 assertNotNull(expectException(() -> factory.get(MY_TOPIC)));
117 // managed undefined - default to true
119 assertEquals(1, buildTopics(
120 makePropBuilder().makeTopic(MY_TOPIC).removeTopicProperty(PROPERTY_MANAGED_SUFFIX).build())
122 assertNotNull(factory.get(MY_TOPIC));
124 // managed empty - default to true
126 assertEquals(1, buildTopics(
127 makePropBuilder().makeTopic(MY_TOPIC).setTopicProperty(PROPERTY_MANAGED_SUFFIX, "").build())
129 assertNotNull(factory.get(MY_TOPIC));
134 assertTrue(buildTopics(makePropBuilder().build()).isEmpty());
137 assertTrue(buildTopics(makePropBuilder().addTopic("").build()).isEmpty());
141 NoopTopicSink sink = buildTopics(makePropBuilder().makeTopic(MY_TOPIC)
142 .removeTopicProperty(PROPERTY_TOPIC_SERVERS_SUFFIX).build()).get(0);
143 assertEquals(NOOP_SERVERS, sink.getServers());
147 sink = buildTopics(makePropBuilder().makeTopic(MY_TOPIC).setTopicProperty(PROPERTY_TOPIC_SERVERS_SUFFIX, "")
149 assertEquals(NOOP_SERVERS, sink.getServers());
151 // test other options
152 super.testBuildProperties_Multiple();
156 public void testDestroyString_testGet_testInventory() {
157 super.testDestroyString_testGet_testInventory();
158 super.testDestroyString_Ex();
162 public void testDestroy() {
167 public void testGet() {
172 protected void initFactory() {
173 if (factory != null) {
177 factory = new IndexedNoopTopicSinkFactory();
181 protected List<NoopTopicSink> buildTopics(Properties properties) {
182 return factory.build(properties);
185 protected NoopTopicSink buildTopic(List<String> servers, String topic, boolean managed) {
186 return factory.build(servers, topic, managed);
190 protected void destroyFactory() {
195 protected void destroyTopic(String topic) {
196 factory.destroy(topic);
200 protected List<NoopTopicSink> getInventory() {
201 return factory.inventory();
205 protected NoopTopicSink getTopic(String topic) {
206 return factory.get(topic);
210 protected TopicPropertyBuilder makePropBuilder() {
211 return new NoopTopicPropertyBuilder(PROPERTY_NOOP_SINK_TOPICS);