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