summaryrefslogtreecommitdiffstats
path: root/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/TopicServiceTest.java
blob: 265ec9635ec7d981ada848deed61fe40cca82ff4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* ============LICENSE_START=======================================================
* ONAP : DATALAKE
* ================================================================================
* Copyright 2019 China Mobile
*=================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/

package org.onap.datalake.feeder.service;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.when;

import java.io.IOException;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.onap.datalake.feeder.config.ApplicationConfiguration;
import org.onap.datalake.feeder.domain.Db;
import org.onap.datalake.feeder.domain.Topic;
import org.onap.datalake.feeder.repository.TopicRepository;

/**
 * Test Service for Topic
 * 
 * @author Guobiao Mo
 *
 */
@RunWith(MockitoJUnitRunner.class)
public class TopicServiceTest {

	static String DEFAULT_TOPIC_NAME = "_DL_DEFAULT_";

	@Mock
	private ApplicationConfiguration config;
	
	@Mock
	private TopicRepository topicRepository;

	@Mock
	private ElasticsearchService elasticsearchService;

	@InjectMocks
	private TopicService topicService;

	@Test
	public void testGetTopic() {
		String name = "a";
		when(topicRepository.findById(name)).thenReturn(Optional.of(new Topic(name)));
		assertEquals(topicService.getTopic(name), new Topic(name));
	}

	@Test
	public void testGetTopicNull() {
		String name = null;
		when(topicRepository.findById(name)).thenReturn(Optional.empty());
		assertNull(topicService.getTopic(name));
	}


	@Test(expected = IOException.class)
	public void testGetEffectiveTopic() throws IOException {
		String name = "a";
		Topic topic = new Topic(name);
		topic.setEnabled(true);
		Set<Db> dbSet = new HashSet<>();
		dbSet.add(new Db("Elasticsearch"));
		topic.setDbs(dbSet);

		when(config.getDefaultTopicName()).thenReturn(DEFAULT_TOPIC_NAME);
		when(topicRepository.findById(DEFAULT_TOPIC_NAME)).thenReturn(Optional.of(topic));
		when(topicRepository.findById(name)).thenReturn(Optional.of(topic));
		when(topicRepository.findById(null)).thenReturn(Optional.empty());
		doThrow(IOException.class).when(elasticsearchService).ensureTableExist(name);

		assertEquals(topicService.getEffectiveTopic(name), topicService.getEffectiveTopic(name, false));

		assertNotNull(topicService.getEffectiveTopic(null));

		topicService.getEffectiveTopic(name, true);
	}
}