2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-2019 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.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX;
28 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS;
29 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX;
31 import java.util.Arrays;
32 import java.util.Collections;
33 import java.util.List;
34 import java.util.Properties;
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
39 public class NoopTopicSinkFactoryTest extends TopicFactoryTestBase<NoopTopicSink> {
41 private static final List<String> NOOP_SERVERS = Arrays.asList("noop");
43 private IndexedNoopTopicSinkFactory factory;
46 * Creates the object to be tested.
52 factory = new IndexedNoopTopicSinkFactory();
56 public void tearDown() {
61 public void testBuildListOfStringStringBoolean() {
64 NoopTopicSink item1 = buildTopic(servers, MY_TOPIC, true);
67 assertEquals(servers, item1.getServers());
68 assertEquals(MY_TOPIC, item1.getTopic());
70 // managed topic - should not build a new one
71 assertEquals(item1, buildTopic(servers, MY_TOPIC, true));
73 NoopTopicSink item2 = buildTopic(servers, TOPIC2, true);
75 assertTrue(item1 != item2);
77 // duplicate - should be the same, as these topics are managed
78 NoopTopicSink item3 = buildTopic(Collections.emptyList(), TOPIC2, true);
79 assertTrue(item2 == item3);
83 assertEquals(NOOP_SERVERS, buildTopic(null, MY_TOPIC, true).getServers());
87 assertEquals(NOOP_SERVERS, buildTopic(Collections.emptyList(), MY_TOPIC, true).getServers());
91 item1 = buildTopic(servers, MY_TOPIC, false);
92 assertTrue(item1 != buildTopic(servers, MY_TOPIC, false));
95 @Test(expected = IllegalArgumentException.class)
96 public void testBuildListOfStringStringBoolean_NullTopic() {
97 buildTopic(servers, null, true);
100 @Test(expected = IllegalArgumentException.class)
101 public void testBuildListOfStringStringBoolean_EmptyTopic() {
102 buildTopic(servers, "", true);
106 public void testBuildProperties() {
109 assertEquals(1, buildTopics(makePropBuilder().makeTopic(MY_TOPIC).build()).size());
110 assertNotNull(factory.get(MY_TOPIC));
112 // unmanaged topic - get() will throw an exception
114 assertEquals(1, buildTopics(makePropBuilder().makeTopic(MY_TOPIC)
115 .setTopicProperty(PROPERTY_MANAGED_SUFFIX, "false").build()).size());
116 assertThatThrownBy(() -> factory.get(MY_TOPIC));
118 // managed undefined - default to true
120 assertEquals(1, buildTopics(
121 makePropBuilder().makeTopic(MY_TOPIC).removeTopicProperty(PROPERTY_MANAGED_SUFFIX).build())
123 assertNotNull(factory.get(MY_TOPIC));
125 // managed empty - default to true
127 assertEquals(1, buildTopics(
128 makePropBuilder().makeTopic(MY_TOPIC).setTopicProperty(PROPERTY_MANAGED_SUFFIX, "").build())
130 assertNotNull(factory.get(MY_TOPIC));
135 assertTrue(buildTopics(makePropBuilder().build()).isEmpty());
138 assertTrue(buildTopics(makePropBuilder().addTopic("").build()).isEmpty());
142 NoopTopicSink sink = buildTopics(makePropBuilder().makeTopic(MY_TOPIC)
143 .removeTopicProperty(PROPERTY_TOPIC_SERVERS_SUFFIX).build()).get(0);
144 assertEquals(NOOP_SERVERS, sink.getServers());
148 sink = buildTopics(makePropBuilder().makeTopic(MY_TOPIC).setTopicProperty(PROPERTY_TOPIC_SERVERS_SUFFIX, "")
150 assertEquals(NOOP_SERVERS, sink.getServers());
152 // test other options
153 super.testBuildProperties_Multiple();
157 public void testDestroyString_testGet_testInventory() {
158 super.testDestroyString_testGet_testInventory();
159 super.testDestroyString_Ex();
163 public void testDestroy() {
168 public void testGet() {
173 protected void initFactory() {
174 if (factory != null) {
178 factory = new IndexedNoopTopicSinkFactory();
182 protected List<NoopTopicSink> buildTopics(Properties properties) {
183 return factory.build(properties);
186 protected NoopTopicSink buildTopic(List<String> servers, String topic, boolean managed) {
187 return factory.build(servers, topic, managed);
191 protected void destroyFactory() {
196 protected void destroyTopic(String topic) {
197 factory.destroy(topic);
201 protected List<NoopTopicSink> getInventory() {
202 return factory.inventory();
206 protected NoopTopicSink getTopic(String topic) {
207 return factory.get(topic);
211 protected TopicPropertyBuilder makePropBuilder() {
212 return new NoopTopicPropertyBuilder(PROPERTY_NOOP_SINK_TOPICS);