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
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;
39 public class DmaapTopicSourceFactoryTest extends DmaapTopicFactoryTestBase<DmaapTopicSource> {
41 private SourceFactory factory;
44 * Creates the object to be tested.
50 factory = new SourceFactory();
54 public void tearDown() {
59 public void testBuildBusTopicParams() {
60 super.testBuildBusTopicParams();
61 super.testBuildBusTopicParams_Ex();
65 public void testBuildProperties() {
66 super.testBuildProperties();
68 // check source-specific parameters that were used
69 BusTopicParams params = factory.params.getFirst();
70 assertEquals(MY_CONS_GROUP, params.getConsumerGroup());
71 assertEquals(MY_CONS_INST, params.getConsumerInstance());
72 assertEquals(MY_FETCH_LIMIT, params.getFetchLimit());
73 assertEquals(MY_FETCH_TIMEOUT, params.getFetchTimeout());
75 super.testBuildProperties_Variations();
76 super.testBuildProperties_Multiple();
78 // check default values for source-specific parameters
79 checkDefault(PROPERTY_TOPIC_SOURCE_FETCH_LIMIT_SUFFIX,
80 params2 -> params2.getFetchLimit() == DmaapTopicSource.DEFAULT_LIMIT_FETCH,
81 null, "", "invalid-limit-number");
83 checkDefault(PROPERTY_TOPIC_SOURCE_FETCH_TIMEOUT_SUFFIX,
84 params2 -> params2.getFetchTimeout() == DmaapTopicSource.DEFAULT_TIMEOUT_MS_FETCH,
85 null, "", "invalid-timeout-number");
89 public void testBuildListOfStringStringStringString() {
90 DmaapTopicSource source1 = factory.build(servers, MY_TOPIC, MY_API_KEY, MY_API_SECRET);
91 assertNotNull(source1);
93 // check source-specific parameters that were used
94 BusTopicParams params = factory.params.getFirst();
95 assertEquals(MY_API_KEY, params.getApiKey());
96 assertEquals(MY_API_SECRET, params.getApiSecret());
97 assertEquals(DmaapTopicSource.DEFAULT_LIMIT_FETCH, params.getFetchLimit());
98 assertEquals(DmaapTopicSource.DEFAULT_TIMEOUT_MS_FETCH, params.getFetchTimeout());
102 public void testBuildListOfStringString() {
103 super.testBuildListOfStringString();
105 // check source-specific parameters that were used
106 BusTopicParams params = factory.params.getFirst();
107 assertEquals(null, params.getApiKey());
108 assertEquals(null, params.getApiSecret());
109 assertEquals(DmaapTopicSource.DEFAULT_LIMIT_FETCH, params.getFetchLimit());
110 assertEquals(DmaapTopicSource.DEFAULT_TIMEOUT_MS_FETCH, params.getFetchTimeout());
114 public void testDestroyString_testGet_testInventory() {
115 super.testDestroyString_testGet_testInventory();
116 super.testDestroyString_Ex();
120 public void testDestroy() {
125 public void testGet() {
130 public void testToString() {
131 assertTrue(factory.toString().startsWith("IndexedDmaapTopicSourceFactory ["));
135 protected void initFactory() {
136 if (factory != null) {
140 factory = new SourceFactory();
144 protected List<DmaapTopicSource> buildTopics(Properties properties) {
145 return factory.build(properties);
149 protected DmaapTopicSource buildTopic(BusTopicParams params) {
150 return factory.build(params);
154 protected DmaapTopicSource buildTopic(List<String> servers, String topic) {
155 return factory.build(servers, topic);
159 protected void destroyFactory() {
164 protected void destroyTopic(String topic) {
165 factory.destroy(topic);
169 protected List<DmaapTopicSource> getInventory() {
170 return factory.inventory();
174 protected DmaapTopicSource getTopic(String topic) {
175 return factory.get(topic);
179 protected BusTopicParams getLastParams() {
180 return factory.params.getLast();
184 protected TopicPropertyBuilder makePropBuilder() {
185 return new DmaapTopicPropertyBuilder(PROPERTY_DMAAP_SOURCE_TOPICS);
189 * Factory that records the parameters of all of the sources it creates.
191 private static class SourceFactory extends IndexedDmaapTopicSourceFactory {
192 private Deque<BusTopicParams> params = new LinkedList<>();
195 protected DmaapTopicSource makeSource(BusTopicParams busTopicParams) {
196 params.add(busTopicParams);
197 return super.makeSource(busTopicParams);