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