1f7506f81bcc5c37221def44a994da7918b3ee8f
[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     @Override
56     public void setUp() {
57         super.setUp();
58         initFactory();
59     }
60
61     @After
62     public void tearDown() {
63         factory.destroy();
64     }
65
66     @Test
67     public void testBuildListOfStringStringBoolean() {
68         initFactory();
69
70         T item1 = buildTopic(servers, MY_TOPIC, true);
71         assertNotNull(item1);
72
73         assertEquals(servers, item1.getServers());
74         assertEquals(MY_TOPIC, item1.getTopic());
75
76         // managed topic - should not build a new one
77         assertEquals(item1, buildTopic(servers, MY_TOPIC, true));
78
79         T item2 = buildTopic(servers, TOPIC2, true);
80         assertNotNull(item2);
81         assertNotSame(item1, item2);
82
83         // duplicate - should be the same, as these topics are managed
84         List<String> randomServers = new ArrayList<>();
85         randomServers.add(RandomStringUtils.randomAlphanumeric(8));
86         T item3 = buildTopic(randomServers, TOPIC2, true);
87         assertSame(item2, item3);
88
89         T item4 = buildTopic(Collections.emptyList(), TOPIC2, true);
90         assertSame(item3, item4);
91
92         // null server list
93         initFactory();
94         assertEquals(NOOP_SERVERS, buildTopic(null, MY_TOPIC, true).getServers());
95
96         // empty server list
97         initFactory();
98         assertEquals(NOOP_SERVERS, buildTopic(Collections.emptyList(), MY_TOPIC, true).getServers());
99
100         // unmanaged topic
101         initFactory();
102         item1 = buildTopic(servers, MY_TOPIC, false);
103         assertNotSame(item1, buildTopic(servers, MY_TOPIC, false));
104     }
105
106     @Test(expected = IllegalArgumentException.class)
107     public void testBuildListOfStringStringBoolean_NullTopic() {
108         buildTopic(servers, null, true);
109     }
110
111     @Test(expected = IllegalArgumentException.class)
112     public void testBuildListOfStringStringBoolean_EmptyTopic() {
113         buildTopic(servers, "", true);
114     }
115
116     @Test
117     public void testBuildProperties() {
118         // managed topic
119         initFactory();
120         assertEquals(1, buildTopics(makePropBuilder().makeTopic(MY_TOPIC).build()).size());
121         assertNotNull(factory.get(MY_TOPIC));
122
123         // unmanaged topic - get() will throw an exception
124         initFactory();
125         assertEquals(1, buildTopics(makePropBuilder().makeTopic(MY_TOPIC)
126                         .setTopicProperty(PROPERTY_MANAGED_SUFFIX, "false").build()).size());
127         assertThatThrownBy(() -> factory.get(MY_TOPIC));
128
129         // managed undefined - default to true
130         initFactory();
131         assertEquals(1, buildTopics(
132                         makePropBuilder().makeTopic(MY_TOPIC).removeTopicProperty(PROPERTY_MANAGED_SUFFIX).build())
133                                         .size());
134         assertNotNull(factory.get(MY_TOPIC));
135
136         // managed empty - default to true
137         initFactory();
138         assertEquals(1, buildTopics(
139                         makePropBuilder().makeTopic(MY_TOPIC).setTopicProperty(PROPERTY_MANAGED_SUFFIX, "").build())
140                                         .size());
141         assertNotNull(factory.get(MY_TOPIC));
142
143         initFactory();
144
145         // null topic list
146         assertTrue(buildTopics(makePropBuilder().build()).isEmpty());
147
148         // empty topic list
149         assertTrue(buildTopics(makePropBuilder().addTopic("").build()).isEmpty());
150
151         // null server list
152         initFactory();
153         T endpoint = buildTopics(makePropBuilder().makeTopic(MY_TOPIC)
154                         .removeTopicProperty(PROPERTY_TOPIC_SERVERS_SUFFIX).build()).get(0);
155         assertEquals(NOOP_SERVERS, endpoint.getServers());
156
157         // empty server list
158         initFactory();
159         endpoint = buildTopics(makePropBuilder().makeTopic(MY_TOPIC).setTopicProperty(PROPERTY_TOPIC_SERVERS_SUFFIX, "")
160                         .build()).get(0);
161         assertEquals(NOOP_SERVERS, endpoint.getServers());
162
163         // test other options
164         super.testBuildProperties_Multiple();
165     }
166
167     @Test
168     @Override
169     public void testDestroyString_testGet_testInventory() {
170         super.testDestroyString_testGet_testInventory();
171         super.testDestroyString_Ex();
172     }
173
174     @Test
175     @Override
176     public void testDestroy() {
177         super.testDestroy();
178     }
179
180     @Test
181     public void testGet() {
182         super.testGet_Ex();
183     }
184
185     @Override
186     protected void initFactory() {
187         if (factory != null) {
188             factory.destroy();
189         }
190
191         factory = buildFactory();
192     }
193
194     @Override
195     protected List<T> buildTopics(Properties properties) {
196         return factory.build(properties);
197     }
198
199     protected T buildTopic(List<String> servers, String topic, boolean managed) {
200         return factory.build(servers, topic, managed);
201     }
202
203     @Override
204     protected void destroyFactory() {
205         factory.destroy();
206     }
207
208     @Override
209     protected void destroyTopic(String topic) {
210         factory.destroy(topic);
211     }
212
213     @Override
214     protected List<T> getInventory() {
215         return factory.inventory();
216     }
217
218     @Override
219     protected T getTopic(String topic) {
220         return factory.get(topic);
221     }
222
223     @Override
224     protected TopicPropertyBuilder makePropBuilder() {
225         return new NoopTopicPropertyBuilder(factory.getTopicsPropertyName());
226     }
227 }