6fa80a417625b8eb764673bf4b09cf1619ca1cd2
[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_KAFKA_SOURCE_TOPICS;
27
28 import java.util.Deque;
29 import java.util.LinkedList;
30 import java.util.List;
31 import java.util.Properties;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.onap.policy.common.endpoints.event.comm.Topic;
36 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
37 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
38
39 public class KafkaTopicSourceFactoryTest extends KafkaTopicFactoryTestBase<KafkaTopicSource> {
40
41     private SourceFactory factory;
42
43     /**
44      * Creates the object to be tested.
45      */
46     @Before
47     @Override
48     public void setUp() {
49         super.setUp();
50
51         factory = new SourceFactory();
52     }
53
54     @After
55     public void tearDown() {
56         factory.destroy();
57     }
58
59     @Test
60     @Override
61     public void testBuildBusTopicParams() {
62         super.testBuildBusTopicParams_Ex();
63     }
64
65     @Test
66     @Override
67     public void testBuildProperties() {
68
69         initFactory();
70
71         List<KafkaTopicSource> topics = buildTopics(makePropBuilder().makeTopic(MY_TOPIC).build());
72         assertEquals(1, topics.size());
73         assertEquals(MY_TOPIC, topics.get(0).getTopic());
74     }
75
76     @Test
77     @Override
78     public void testDestroyString_testGet_testInventory() {
79         super.testDestroyString_Ex();
80     }
81
82     @Test
83     public void testGet() {
84         super.testGet_Ex();
85     }
86
87     @Test
88     public void testToString() {
89         assertTrue(factory.toString().startsWith("IndexedKafkaTopicSourceFactory ["));
90     }
91
92     @Override
93     protected void initFactory() {
94         if (factory != null) {
95             factory.destroy();
96         }
97
98         factory = new SourceFactory();
99     }
100
101     @Override
102     protected List<KafkaTopicSource> buildTopics(Properties properties) {
103         return factory.build(properties);
104     }
105
106     @Override
107     protected KafkaTopicSource buildTopic(BusTopicParams params) {
108         return factory.build(params);
109     }
110
111     @Override
112     protected KafkaTopicSource buildTopic(List<String> servers, String topic) {
113         return factory.build(servers, topic);
114     }
115
116     @Override
117     protected void destroyFactory() {
118         factory.destroy();
119     }
120
121     @Override
122     protected void destroyTopic(String topic) {
123         factory.destroy(topic);
124     }
125
126     @Override
127     protected List<KafkaTopicSource> getInventory() {
128         return factory.inventory();
129     }
130
131     @Override
132     protected KafkaTopicSource getTopic(String topic) {
133         return factory.get(topic);
134     }
135
136     @Override
137     protected BusTopicParams getLastParams() {
138         return factory.params.getLast();
139     }
140
141     @Override
142     protected TopicPropertyBuilder makePropBuilder() {
143         return new KafkaTopicPropertyBuilder(PROPERTY_KAFKA_SOURCE_TOPICS);
144     }
145
146     /**
147      * Factory that records the parameters of all of the sources it creates.
148      */
149     private static class SourceFactory extends IndexedKafkaTopicSourceFactory {
150         private Deque<BusTopicParams> params = new LinkedList<>();
151
152         @Override
153         protected KafkaTopicSource makeSource(BusTopicParams busTopicParams) {
154             params.add(busTopicParams);
155             return super.makeSource(busTopicParams);
156         }
157     }
158 }