81e3075635362d21edc56468ec2b2feab8f9f1eb
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine - Common Modules
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
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.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_TOPIC_SOURCE_FETCH_LIMIT_SUFFIX;
27 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_TIMEOUT_SUFFIX;
28 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_UEB_SOURCE_TOPICS;
29
30 import java.util.Deque;
31 import java.util.LinkedList;
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;
37 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
38 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
39
40 public class UebTopicSourceFactoryTest extends UebTopicFactoryTestBase<UebTopicSource> {
41
42     private SourceFactory factory;
43
44     /**
45      * Creates the object to be tested.
46      */
47     @Before
48     @Override
49     public void setUp() {
50         super.setUp();
51
52         factory = new SourceFactory();
53     }
54
55     @After
56     public void tearDown() {
57         factory.destroy();
58     }
59
60     @Test
61     @Override
62     public void testBuildBusTopicParams() {
63         super.testBuildBusTopicParams();
64         super.testBuildBusTopicParams_Ex();
65     }
66
67     @Test
68     @Override
69     public void testBuildProperties() {
70
71         super.testBuildProperties();
72
73         // check source-specific parameters that were used
74         BusTopicParams params = factory.params.getFirst();
75         assertEquals(MY_CONS_GROUP, params.getConsumerGroup());
76         assertEquals(MY_CONS_INST, params.getConsumerInstance());
77         assertEquals(MY_FETCH_LIMIT, params.getFetchLimit());
78         assertEquals(MY_FETCH_TIMEOUT, params.getFetchTimeout());
79
80         super.testBuildProperties_Variations();
81         super.testBuildProperties_Multiple();
82
83         // check default values for source-specific parameters
84         checkDefault(PROPERTY_TOPIC_SOURCE_FETCH_LIMIT_SUFFIX,
85             params2 -> params2.getFetchLimit() == PolicyEndPointProperties.DEFAULT_LIMIT_FETCH,
86             null, "", "invalid-limit-number");
87
88         checkDefault(PROPERTY_TOPIC_SOURCE_FETCH_TIMEOUT_SUFFIX,
89             params2 -> params2.getFetchTimeout() == PolicyEndPointProperties.DEFAULT_TIMEOUT_MS_FETCH,
90             null, "", "invalid-timeout-number");
91     }
92
93     @Test
94     public void testBuildListOfStringStringStringString() {
95         UebTopicSource source1 = factory.build(servers, MY_TOPIC, MY_API_KEY, MY_API_SECRET);
96         assertNotNull(source1);
97
98         // check source-specific parameters that were used
99         BusTopicParams params = factory.params.getFirst();
100         assertEquals(MY_API_KEY, params.getApiKey());
101         assertEquals(MY_API_SECRET, params.getApiSecret());
102         assertEquals(PolicyEndPointProperties.DEFAULT_LIMIT_FETCH, params.getFetchLimit());
103         assertEquals(PolicyEndPointProperties.DEFAULT_TIMEOUT_MS_FETCH, params.getFetchTimeout());
104     }
105
106     @Test
107     @Override
108     public void testBuildListOfStringString() {
109         super.testBuildListOfStringString();
110
111         // check source-specific parameters that were used
112         BusTopicParams params = factory.params.getFirst();
113         assertEquals(null, params.getApiKey());
114         assertEquals(null, params.getApiSecret());
115         assertEquals(PolicyEndPointProperties.DEFAULT_LIMIT_FETCH, params.getFetchLimit());
116         assertEquals(PolicyEndPointProperties.DEFAULT_TIMEOUT_MS_FETCH, params.getFetchTimeout());
117
118         assertEquals(true, params.isAllowSelfSignedCerts());
119     }
120
121     @Test
122     @Override
123     public void testDestroyString_testGet_testInventory() {
124         super.testDestroyString_testGet_testInventory();
125         super.testDestroyString_Ex();
126     }
127
128     @Test
129     @Override
130     public void testDestroy() {
131         super.testDestroy();
132     }
133
134     @Test
135     public void testGet() {
136         super.testGet_Ex();
137     }
138
139     @Test
140     public void testToString() {
141         assertTrue(factory.toString().startsWith("IndexedUebTopicSourceFactory ["));
142     }
143
144     @Override
145     protected void initFactory() {
146         if (factory != null) {
147             factory.destroy();
148         }
149
150         factory = new SourceFactory();
151     }
152
153     @Override
154     protected List<UebTopicSource> buildTopics(Properties properties) {
155         return factory.build(properties);
156     }
157
158     @Override
159     protected UebTopicSource buildTopic(BusTopicParams params) {
160         return factory.build(params);
161     }
162
163     @Override
164     protected UebTopicSource buildTopic(List<String> servers, String topic) {
165         return factory.build(servers, topic);
166     }
167
168     @Override
169     protected void destroyFactory() {
170         factory.destroy();
171     }
172
173     @Override
174     protected void destroyTopic(String topic) {
175         factory.destroy(topic);
176     }
177
178     @Override
179     protected List<UebTopicSource> getInventory() {
180         return factory.inventory();
181     }
182
183     @Override
184     protected UebTopicSource getTopic(String topic) {
185         return factory.get(topic);
186     }
187
188     @Override
189     protected BusTopicParams getLastParams() {
190         return factory.params.getLast();
191     }
192
193     @Override
194     protected TopicPropertyBuilder makePropBuilder() {
195         return new UebTopicPropertyBuilder(PROPERTY_UEB_SOURCE_TOPICS);
196     }
197
198     /**
199      * Factory that records the parameters of all of the sources it creates.
200      */
201     private static class SourceFactory extends IndexedUebTopicSourceFactory {
202         private Deque<BusTopicParams> params = new LinkedList<>();
203
204         @Override
205         protected UebTopicSource makeSource(BusTopicParams busTopicParams) {
206             params.add(busTopicParams);
207             return super.makeSource(busTopicParams);
208         }
209     }
210 }