16d9e539a8524d88955241c733c2685fa6a54e65
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.common.endpoints.event.comm.bus;
22
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;
31
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;
42
43 public abstract class NoopTopicFactoryTest<F extends NoopTopicFactory<T>, T extends NoopTopicEndpoint>
44     extends TopicFactoryTestBase<T> {
45
46     private static final List<String> NOOP_SERVERS = Arrays.asList(CommInfrastructure.NOOP.toString());
47     private F factory = null;
48
49     protected abstract F buildFactory();
50
51     /**
52      * Creates the object to be tested.
53      */
54     @Before
55     public void setUp() {
56         super.setUp();
57         initFactory();
58     }
59
60     @After
61     public void tearDown() {
62         factory.destroy();
63     }
64
65     @Test
66     public void testBuildListOfStringStringBoolean() {
67         initFactory();
68
69         T item1 = buildTopic(servers, MY_TOPIC, true);
70         assertNotNull(item1);
71
72         assertEquals(servers, item1.getServers());
73         assertEquals(MY_TOPIC, item1.getTopic());
74
75         // managed topic - should not build a new one
76         assertEquals(item1, buildTopic(servers, MY_TOPIC, true));
77
78         T item2 = buildTopic(servers, TOPIC2, true);
79         assertNotNull(item2);
80         assertNotSame(item1, item2);
81
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);
87
88         T item4 = buildTopic(Collections.emptyList(), TOPIC2, true);
89         assertSame(item3, item4);
90
91         // null server list
92         initFactory();
93         assertEquals(NOOP_SERVERS, buildTopic(null, MY_TOPIC, true).getServers());
94
95         // empty server list
96         initFactory();
97         assertEquals(NOOP_SERVERS, buildTopic(Collections.emptyList(), MY_TOPIC, true).getServers());
98
99         // unmanaged topic
100         initFactory();
101         item1 = buildTopic(servers, MY_TOPIC, false);
102         assertNotSame(item1, buildTopic(servers, MY_TOPIC, false));
103     }
104
105     @Test(expected = IllegalArgumentException.class)
106     public void testBuildListOfStringStringBoolean_NullTopic() {
107         buildTopic(servers, null, true);
108     }
109
110     @Test(expected = IllegalArgumentException.class)
111     public void testBuildListOfStringStringBoolean_EmptyTopic() {
112         buildTopic(servers, "", true);
113     }
114
115     @Test
116     public void testBuildProperties() {
117         // managed topic
118         initFactory();
119         assertEquals(1, buildTopics(makePropBuilder().makeTopic(MY_TOPIC).build()).size());
120         assertNotNull(factory.get(MY_TOPIC));
121
122         // unmanaged topic - get() will throw an exception
123         initFactory();
124         assertEquals(1, buildTopics(makePropBuilder().makeTopic(MY_TOPIC)
125                         .setTopicProperty(PROPERTY_MANAGED_SUFFIX, "false").build()).size());
126         assertThatThrownBy(() -> factory.get(MY_TOPIC));
127
128         // managed undefined - default to true
129         initFactory();
130         assertEquals(1, buildTopics(
131                         makePropBuilder().makeTopic(MY_TOPIC).removeTopicProperty(PROPERTY_MANAGED_SUFFIX).build())
132                                         .size());
133         assertNotNull(factory.get(MY_TOPIC));
134
135         // managed empty - default to true
136         initFactory();
137         assertEquals(1, buildTopics(
138                         makePropBuilder().makeTopic(MY_TOPIC).setTopicProperty(PROPERTY_MANAGED_SUFFIX, "").build())
139                                         .size());
140         assertNotNull(factory.get(MY_TOPIC));
141
142         initFactory();
143
144         // null topic list
145         assertTrue(buildTopics(makePropBuilder().build()).isEmpty());
146
147         // empty topic list
148         assertTrue(buildTopics(makePropBuilder().addTopic("").build()).isEmpty());
149
150         // null server list
151         initFactory();
152         T endpoint = buildTopics(makePropBuilder().makeTopic(MY_TOPIC)
153                         .removeTopicProperty(PROPERTY_TOPIC_SERVERS_SUFFIX).build()).get(0);
154         assertEquals(NOOP_SERVERS, endpoint.getServers());
155
156         // empty server list
157         initFactory();
158         endpoint = buildTopics(makePropBuilder().makeTopic(MY_TOPIC).setTopicProperty(PROPERTY_TOPIC_SERVERS_SUFFIX, "")
159                         .build()).get(0);
160         assertEquals(NOOP_SERVERS, endpoint.getServers());
161
162         // test other options
163         super.testBuildProperties_Multiple();
164     }
165
166     @Test
167     public void testDestroyString_testGet_testInventory() {
168         super.testDestroyString_testGet_testInventory();
169         super.testDestroyString_Ex();
170     }
171
172     @Test
173     public void testDestroy() {
174         super.testDestroy();
175     }
176
177     @Test
178     public void testGet() {
179         super.testGet_Ex();
180     }
181
182     @Override
183     protected void initFactory() {
184         if (factory != null) {
185             factory.destroy();
186         }
187
188         factory = buildFactory();
189     }
190
191     @Override
192     protected List<T> buildTopics(Properties properties) {
193         return factory.build(properties);
194     }
195
196     protected T buildTopic(List<String> servers, String topic, boolean managed) {
197         return factory.build(servers, topic, managed);
198     }
199
200     @Override
201     protected void destroyFactory() {
202         factory.destroy();
203     }
204
205     @Override
206     protected void destroyTopic(String topic) {
207         factory.destroy(topic);
208     }
209
210     @Override
211     protected List<T> getInventory() {
212         return factory.inventory();
213     }
214
215     @Override
216     protected T getTopic(String topic) {
217         return factory.get(topic);
218     }
219
220     @Override
221     protected TopicPropertyBuilder makePropBuilder() {
222         return new NoopTopicPropertyBuilder(factory.getTopicsPropertyName());
223     }
224 }