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
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.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_DMAAP_SOURCE_TOPICS;
27 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_LIMIT_SUFFIX;
28 import static org.onap.policy.common.endpoints.properties.PolicyEndPointProperties.PROPERTY_TOPIC_SOURCE_FETCH_TIMEOUT_SUFFIX;
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;
40 public class DmaapTopicSourceFactoryTest extends DmaapTopicFactoryTestBase<DmaapTopicSource> {
42 private SourceFactory factory;
45 * Creates the object to be tested.
52 factory = new SourceFactory();
56 public void tearDown() {
62 public void testBuildBusTopicParams() {
63 super.testBuildBusTopicParams();
64 super.testBuildBusTopicParams_Ex();
69 public void testBuildProperties() {
70 super.testBuildProperties();
72 // check source-specific parameters that were used
73 BusTopicParams params = factory.params.getFirst();
74 assertEquals(MY_CONS_GROUP, params.getConsumerGroup());
75 assertEquals(MY_CONS_INST, params.getConsumerInstance());
76 assertEquals(MY_FETCH_LIMIT, params.getFetchLimit());
77 assertEquals(MY_FETCH_TIMEOUT, params.getFetchTimeout());
79 super.testBuildProperties_Variations();
80 super.testBuildProperties_Multiple();
82 // check default values for source-specific parameters
83 checkDefault(PROPERTY_TOPIC_SOURCE_FETCH_LIMIT_SUFFIX,
84 params2 -> params2.getFetchLimit() == PolicyEndPointProperties.DEFAULT_LIMIT_FETCH,
85 null, "", "invalid-limit-number");
87 checkDefault(PROPERTY_TOPIC_SOURCE_FETCH_TIMEOUT_SUFFIX,
88 params2 -> params2.getFetchTimeout() == PolicyEndPointProperties.DEFAULT_TIMEOUT_MS_FETCH,
89 null, "", "invalid-timeout-number");
93 public void testBuildListOfStringStringStringString() {
94 DmaapTopicSource source1 = factory.build(servers, MY_TOPIC, MY_API_KEY, MY_API_SECRET);
95 assertNotNull(source1);
97 // check source-specific parameters that were used
98 BusTopicParams params = factory.params.getFirst();
99 assertEquals(MY_API_KEY, params.getApiKey());
100 assertEquals(MY_API_SECRET, params.getApiSecret());
101 assertEquals(PolicyEndPointProperties.DEFAULT_LIMIT_FETCH, params.getFetchLimit());
102 assertEquals(PolicyEndPointProperties.DEFAULT_TIMEOUT_MS_FETCH, params.getFetchTimeout());
107 public void testBuildListOfStringString() {
108 super.testBuildListOfStringString();
110 // check source-specific parameters that were used
111 BusTopicParams params = factory.params.getFirst();
112 assertEquals(null, params.getApiKey());
113 assertEquals(null, params.getApiSecret());
114 assertEquals(PolicyEndPointProperties.DEFAULT_LIMIT_FETCH, params.getFetchLimit());
115 assertEquals(PolicyEndPointProperties.DEFAULT_TIMEOUT_MS_FETCH, params.getFetchTimeout());
120 public void testDestroyString_testGet_testInventory() {
121 super.testDestroyString_testGet_testInventory();
122 super.testDestroyString_Ex();
127 public void testDestroy() {
132 public void testGet() {
137 public void testToString() {
138 assertTrue(factory.toString().startsWith("IndexedDmaapTopicSourceFactory ["));
142 protected void initFactory() {
143 if (factory != null) {
147 factory = new SourceFactory();
151 protected List<DmaapTopicSource> buildTopics(Properties properties) {
152 return factory.build(properties);
156 protected DmaapTopicSource buildTopic(BusTopicParams params) {
157 return factory.build(params);
161 protected DmaapTopicSource buildTopic(List<String> servers, String topic) {
162 return factory.build(servers, topic);
166 protected void destroyFactory() {
171 protected void destroyTopic(String topic) {
172 factory.destroy(topic);
176 protected List<DmaapTopicSource> getInventory() {
177 return factory.inventory();
181 protected DmaapTopicSource getTopic(String topic) {
182 return factory.get(topic);
186 protected BusTopicParams getLastParams() {
187 return factory.params.getLast();
191 protected TopicPropertyBuilder makePropBuilder() {
192 return new DmaapTopicPropertyBuilder(PROPERTY_DMAAP_SOURCE_TOPICS);
196 * Factory that records the parameters of all of the sources it creates.
198 private static class SourceFactory extends IndexedDmaapTopicSourceFactory {
199 private Deque<BusTopicParams> params = new LinkedList<>();
202 protected DmaapTopicSource makeSource(BusTopicParams busTopicParams) {
203 params.add(busTopicParams);
204 return super.makeSource(busTopicParams);