eea475017f7d1d4dc4078bff3ac302f11fa6eb56
[dcaegen2/services.git] /
1 /*
2 * ============LICENSE_START=======================================================
3 * ONAP : DATALAKE
4 * ================================================================================
5 * Copyright 2019 China Mobile
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.datalake.feeder.service;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.Mockito.doThrow;
29 import static org.mockito.Mockito.when;
30
31 import java.io.IOException;
32 import java.util.*;
33
34 import org.elasticsearch.client.IndicesClient;
35 import org.elasticsearch.client.RequestOptions;
36 import org.elasticsearch.client.RestHighLevelClient;
37 import org.elasticsearch.client.indices.GetIndexRequest;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.InjectMocks;
41 import org.mockito.Mock;
42 import org.mockito.Mockito;
43 import org.mockito.invocation.InvocationOnMock;
44 import org.mockito.junit.MockitoJUnitRunner;
45 import org.mockito.stubbing.Answer;
46 import org.onap.datalake.feeder.config.ApplicationConfiguration;
47 import org.onap.datalake.feeder.domain.*;
48 import org.onap.datalake.feeder.dto.TopicConfig;
49 import org.onap.datalake.feeder.enumeration.DbTypeEnum;
50 import org.onap.datalake.feeder.repository.DbRepository;
51 import org.onap.datalake.feeder.repository.TopicNameRepository;
52 import org.onap.datalake.feeder.repository.TopicRepository;
53 import org.onap.datalake.feeder.service.db.ElasticsearchService;
54
55 /**
56  * Test Service for Topic
57  * 
58  * @author Guobiao Mo
59  *
60  */
61 @RunWith(MockitoJUnitRunner.class)
62 public class TopicServiceTest {
63
64         static String DEFAULT_TOPIC_NAME = "_DL_DEFAULT_";
65
66         @Mock
67         private ApplicationConfiguration config;
68         
69         @Mock
70         private TopicRepository topicRepository;
71
72         @Mock
73         private ElasticsearchService elasticsearchService;
74
75         @Mock
76         private DbService dbService;
77
78         @Mock
79         private DbRepository dbRepository;
80
81         @Mock
82         private TopicNameRepository topicNameRepository;
83
84         @InjectMocks
85         private TopicService topicService;
86
87         @Test(expected = NullPointerException.class)
88         public void testGetTopic() throws IOException{
89                 List<Topic> topics = new ArrayList<>();
90                 Topic topic = new Topic();
91                 DbType dbType = new DbType();
92                 Set<Kafka> kafkas = new HashSet<>();
93                 Set<Db> dbs = new HashSet<>();
94                 Db db = new Db();
95                 db.setName("Elasticsearch");
96                 dbs.add(db);
97
98                 dbType.setId("ES");
99                 db.setDbType(dbType);
100
101                 Kafka kafka = new Kafka();
102                 kafka.setName("1234");
103                 kafkas.add(kafka);
104
105                 TopicName topicName = new TopicName();
106                 topicName.setId("1234");
107
108                 topic.setTopicName(topicName);
109                 topic.setKafkas(kafkas);
110                 topic.setEnabled(true);
111                 topic.setDbs(dbs);
112                 topics.add(topic);
113                 when(topicRepository.findAll()).thenReturn(topics);
114                 when((ElasticsearchService)dbService.findDbStoreService(db)).thenReturn(new ElasticsearchService(db));
115                 topicService.findTopics(kafka,topicName.getId());
116                 topicService.getEnabledEffectiveTopic(kafka,topicName.getId(),true);
117
118         }
119         @Test
120         public void testGetTopicNull() {
121                 Topic topic = new Topic();
122                 TopicName topicName = new TopicName();
123                 topicName.setId("_DL_DEFAULT_");
124                 topic.setId(1234);
125                 topic.setTopicName(topicName);
126                 Optional<Topic> optional = Optional.of(topic);
127                 when(topicRepository.findById(0)).thenReturn(optional);
128                 when(config.getDefaultTopicName()).thenReturn("_DL_DEFAULT_");
129                 assertEquals(topic,topicService.getTopic(0));
130                 assertTrue(topicService.isDefaultTopic(topic));
131         }
132
133         @Test
134         public void testFillTopic(){
135                 TopicConfig tConfig = new TopicConfig();
136                 tConfig.setId(1234);
137                 tConfig.setName("1234");
138                 tConfig.setLogin("1234");
139                 tConfig.setPassword("1234");
140                 tConfig.setEnabled(true);
141                 tConfig.setSaveRaw(true);
142                 tConfig.setDataFormat("1234");
143                 tConfig.setTtl(1234);
144                 tConfig.setCorrelateClearedMessage(true);
145                 tConfig.setMessageIdPath("1234");
146                 tConfig.setAggregateArrayPath("1234");
147                 tConfig.setFlattenArrayPath("1234");
148                 List<Integer> sinkdbs = new ArrayList<>();
149                 sinkdbs.add(1234);
150                 tConfig.setSinkdbs(sinkdbs);
151
152                 Db db = new Db();
153                 db.setId(1234);
154
155                 TopicName topicName = new TopicName();
156                 topicName.setId("1234");
157
158                 Optional<TopicName> optional = Optional.of(topicName);
159                 when(dbRepository.findById(1234)).thenReturn(Optional.of(db));
160                 when(topicNameRepository.findById(tConfig.getName())).thenReturn(optional);
161
162                 topicService.fillTopicConfiguration(tConfig);
163         }
164
165 /*
166         @Test
167         public void testGetEffectiveTopic() throws IOException {
168                 String name = "a";
169                 Topic topic = new Topic(name);
170                 topic.setEnabled(true);
171                 Set<Db> dbSet = new HashSet<>();
172                 dbSet.add(new Db("Elasticsearch"));
173                 topic.setDbs(dbSet);
174
175                 when(config.getDefaultTopicName()).thenReturn(DEFAULT_TOPIC_NAME);
176                 when(topicRepository.findById(DEFAULT_TOPIC_NAME)).thenReturn(Optional.of(topic));
177                 when(topicRepository.findById(name)).thenReturn(Optional.of(topic));
178                 when(topicRepository.findById(null)).thenReturn(Optional.empty());
179
180                 assertEquals(topicService.getEffectiveTopic(name), topicService.getEffectiveTopic(name, false));
181
182                 assertNotNull(topicService.getEffectiveTopic(null));
183
184                 topicService.getEffectiveTopic(name, true);
185         }
186 */
187 }