2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 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.assertNotSame;
27 import static org.junit.Assert.assertSame;
28 import static org.junit.Assert.assertTrue;
29 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_MANAGED_SUFFIX;
30 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_TOPIC_SERVERS_SUFFIX;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Collections;
35 import java.util.List;
36 import java.util.Properties;
37 import org.apache.commons.lang3.RandomStringUtils;
38 import org.junit.After;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
43 public abstract class NoopTopicFactoryTest<F extends NoopTopicFactory<T>, T extends NoopTopicEndpoint>
44 extends TopicFactoryTestBase<T> {
46 private static final List<String> NOOP_SERVERS = Arrays.asList(CommInfrastructure.NOOP.toString());
47 private F factory = null;
49 protected abstract F buildFactory();
52 * Creates the object to be tested.
61 public void tearDown() {
66 public void testBuildListOfStringStringBoolean() {
69 T item1 = buildTopic(servers, MY_TOPIC, true);
72 assertEquals(servers, item1.getServers());
73 assertEquals(MY_TOPIC, item1.getTopic());
75 // managed topic - should not build a new one
76 assertEquals(item1, buildTopic(servers, MY_TOPIC, true));
78 T item2 = buildTopic(servers, TOPIC2, true);
80 assertNotSame(item1, item2);
82 // duplicate - should be the same, as these topics are managed
83 List<String> randomServers = new ArrayList<>();
84 randomServers.add(RandomStringUtils.randomAlphanumeric(8));
85 T item3 = buildTopic(randomServers, TOPIC2, true);
86 assertSame(item2, item3);
88 T item4 = buildTopic(Collections.emptyList(), TOPIC2, true);
89 assertSame(item3, item4);
93 assertEquals(NOOP_SERVERS, buildTopic(null, MY_TOPIC, true).getServers());
97 assertEquals(NOOP_SERVERS, buildTopic(Collections.emptyList(), MY_TOPIC, true).getServers());
101 item1 = buildTopic(servers, MY_TOPIC, false);
102 assertNotSame(item1, buildTopic(servers, MY_TOPIC, false));
105 @Test(expected = IllegalArgumentException.class)
106 public void testBuildListOfStringStringBoolean_NullTopic() {
107 buildTopic(servers, null, true);
110 @Test(expected = IllegalArgumentException.class)
111 public void testBuildListOfStringStringBoolean_EmptyTopic() {
112 buildTopic(servers, "", true);
116 public void testBuildProperties() {
119 assertEquals(1, buildTopics(makePropBuilder().makeTopic(MY_TOPIC).build()).size());
120 assertNotNull(factory.get(MY_TOPIC));
122 // unmanaged topic - get() will throw an exception
124 assertEquals(1, buildTopics(makePropBuilder().makeTopic(MY_TOPIC)
125 .setTopicProperty(PROPERTY_MANAGED_SUFFIX, "false").build()).size());
126 assertThatThrownBy(() -> factory.get(MY_TOPIC));
128 // managed undefined - default to true
130 assertEquals(1, buildTopics(
131 makePropBuilder().makeTopic(MY_TOPIC).removeTopicProperty(PROPERTY_MANAGED_SUFFIX).build())
133 assertNotNull(factory.get(MY_TOPIC));
135 // managed empty - default to true
137 assertEquals(1, buildTopics(
138 makePropBuilder().makeTopic(MY_TOPIC).setTopicProperty(PROPERTY_MANAGED_SUFFIX, "").build())
140 assertNotNull(factory.get(MY_TOPIC));
145 assertTrue(buildTopics(makePropBuilder().build()).isEmpty());
148 assertTrue(buildTopics(makePropBuilder().addTopic("").build()).isEmpty());
152 T endpoint = buildTopics(makePropBuilder().makeTopic(MY_TOPIC)
153 .removeTopicProperty(PROPERTY_TOPIC_SERVERS_SUFFIX).build()).get(0);
154 assertEquals(NOOP_SERVERS, endpoint.getServers());
158 endpoint = buildTopics(makePropBuilder().makeTopic(MY_TOPIC).setTopicProperty(PROPERTY_TOPIC_SERVERS_SUFFIX, "")
160 assertEquals(NOOP_SERVERS, endpoint.getServers());
162 // test other options
163 super.testBuildProperties_Multiple();
167 public void testDestroyString_testGet_testInventory() {
168 super.testDestroyString_testGet_testInventory();
169 super.testDestroyString_Ex();
173 public void testDestroy() {
178 public void testGet() {
183 protected void initFactory() {
184 if (factory != null) {
188 factory = buildFactory();
192 protected List<T> buildTopics(Properties properties) {
193 return factory.build(properties);
196 protected T buildTopic(List<String> servers, String topic, boolean managed) {
197 return factory.build(servers, topic, managed);
201 protected void destroyFactory() {
206 protected void destroyTopic(String topic) {
207 factory.destroy(topic);
211 protected List<T> getInventory() {
212 return factory.inventory();
216 protected T getTopic(String topic) {
217 return factory.get(topic);
221 protected TopicPropertyBuilder makePropBuilder() {
222 return new NoopTopicPropertyBuilder(factory.getTopicsPropertyName());