0e6db8362388241a328014f0e7f580cd27e18ed2
[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.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.mockito.Mockito.doThrow;
27 import static org.mockito.Mockito.when;
28
29 import java.io.IOException;
30 import java.util.HashSet;
31 import java.util.Optional;
32 import java.util.Set;
33
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.InjectMocks;
37 import org.mockito.Mock;
38 import org.mockito.junit.MockitoJUnitRunner;
39 import org.onap.datalake.feeder.domain.Db;
40 import org.onap.datalake.feeder.domain.Topic;
41 import org.onap.datalake.feeder.repository.TopicRepository;
42
43 /**
44  * Test Service for Topic
45  * 
46  * @author Guobiao Mo
47  *
48  */
49 @RunWith(MockitoJUnitRunner.class)
50 public class TopicServiceTest {
51
52         @Mock
53         private TopicRepository topicRepository;
54
55         @Mock
56         private ElasticsearchService elasticsearchService;
57
58         @InjectMocks
59         private TopicService topicService;
60
61         @Test
62         public void testGetTopic() {
63                 String name = "a";
64                 when(topicRepository.findById(name)).thenReturn(Optional.of(new Topic(name)));
65                 assertEquals(topicService.getTopic(name), new Topic(name));
66         }
67
68         @Test
69         public void testGetTopicNull() {
70                 String name = null;
71                 when(topicRepository.findById(name)).thenReturn(Optional.empty());
72                 assertNull(topicService.getTopic(name));
73         }
74
75         @Test
76         public void testGetDefaultTopic() {
77                 String name = "_DL_DEFAULT_";
78                 when(topicRepository.findById(name)).thenReturn(Optional.of(new Topic(name)));
79                 assertEquals(topicService.getDefaultTopic(), new Topic(name));
80         }
81
82         @Test(expected = IOException.class)
83         public void testGetEffectiveTopic() throws IOException {
84                 String name = "a";
85                 Topic topic = new Topic(name);
86                 topic.setEnabled(true);
87                 Set<Db> dbSet = new HashSet<>();
88                 dbSet.add(new Db("Elasticsearch"));
89                 topic.setDbs(dbSet);
90
91                 when(topicRepository.findById(name)).thenReturn(Optional.of(topic));
92                 when(topicRepository.findById(null)).thenReturn(Optional.empty());
93                 doThrow(IOException.class).when(elasticsearchService).ensureTableExist(name);
94
95                 assertEquals(topicService.getEffectiveTopic(name), topicService.getEffectiveTopic(name, false));
96
97                 assertNotNull(topicService.getEffectiveTopic(null));
98
99                 topicService.getEffectiveTopic(name, true);
100         }
101 }