a770f50f5f0ccf8e96a9938e39280f8d1418e5d9
[dcaegen2/services.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : DATALAKE
4  * ================================================================================
5  * Copyright (C) 2018-2019 Huawei. 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 package org.onap.datalake.feeder.controller;
21
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.InjectMocks;
26 import org.mockito.Mock;
27 import org.mockito.MockitoAnnotations;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.onap.datalake.feeder.config.ApplicationConfiguration;
30 import org.onap.datalake.feeder.domain.Db;
31 import org.onap.datalake.feeder.domain.Topic;
32 import org.onap.datalake.feeder.repository.TopicRepository;
33 import org.onap.datalake.feeder.service.DbService;
34 import org.onap.datalake.feeder.service.DmaapService;
35 import org.onap.datalake.feeder.service.TopicService;
36 import org.springframework.validation.BindingResult;
37
38 import javax.servlet.http.HttpServletResponse;
39 import java.io.IOException;
40 import java.lang.reflect.Field;
41 import java.util.ArrayList;
42 import java.util.HashSet;
43 import java.util.List;
44 import java.util.Optional;
45 import java.util.Set;
46
47 import static org.junit.Assert.assertEquals;
48 import static org.mockito.Mockito.when;
49
50 @RunWith(MockitoJUnitRunner.class)
51 public class TopicControllerTest {
52
53     static String DEFAULT_TOPIC_NAME = "_DL_DEFAULT_";
54
55     @Mock
56     private HttpServletResponse httpServletResponse;
57
58     @Mock
59     private BindingResult mockBindingResult;
60
61     @Mock
62     private TopicRepository topicRepository;
63
64     @InjectMocks
65     private TopicService topicService1;
66
67     @Mock
68     private ApplicationConfiguration config;
69
70     @Mock
71     private DbService dbService1;
72
73     @Mock
74     private DmaapService dmaapService1;
75
76     @Before
77     public void setupTest() {
78         MockitoAnnotations.initMocks(this);
79         // While the default boolean return value for a mock is 'false',
80         // it's good to be explicit anyway:
81         when(mockBindingResult.hasErrors()).thenReturn(false);
82     }
83
84     public void setAccessPrivateFields(TopicController topicController) throws NoSuchFieldException,
85             IllegalAccessException {
86         Field topicService = topicController.getClass().getDeclaredField("topicService");
87         topicService.setAccessible(true);
88         topicService.set(topicController, topicService1);
89         Field topicRepository1 = topicController.getClass().getDeclaredField("topicRepository");
90         topicRepository1.setAccessible(true);
91         topicRepository1.set(topicController, topicRepository);
92         Field dbService = topicController.getClass().getDeclaredField("dbService");
93         dbService.setAccessible(true);
94         dbService.set(topicController, dbService1);
95     }
96
97     @Test
98     public void testCreateTopic() throws IOException, NoSuchFieldException, IllegalAccessException {
99         TopicController topicController = new TopicController();
100         setAccessPrivateFields(topicController);
101         when(topicRepository.findById(DEFAULT_TOPIC_NAME)).thenReturn(Optional.of(new Topic(DEFAULT_TOPIC_NAME)));
102         when(config.getDefaultTopicName()).thenReturn(DEFAULT_TOPIC_NAME);
103         Topic topicName = topicController.createTopic(new Topic("a"), mockBindingResult, httpServletResponse);
104         assertEquals(new Topic("a"), topicName);
105         when(mockBindingResult.hasErrors()).thenReturn(true);
106         topicName = topicController.createTopic(new Topic("a"), mockBindingResult, httpServletResponse);
107         assertEquals(null, topicName);
108         when(mockBindingResult.hasErrors()).thenReturn(false);
109         Topic a = new Topic("a");
110         a.setName("a");
111         when(topicRepository.findById("a")).thenReturn(Optional.of(a));
112         topicName = topicController.createTopic(new Topic("a"), mockBindingResult, httpServletResponse);
113         assertEquals(null, topicName);
114     }
115
116     @Test
117     public void testUpdateTopic() throws IOException, NoSuchFieldException, IllegalAccessException {
118         TopicController topicController = new TopicController();
119         setAccessPrivateFields(topicController);
120         Topic topicName = topicController.updateTopic(new Topic("a"), mockBindingResult, httpServletResponse);
121         assertEquals(null, topicName);
122         Topic a = new Topic("a");
123         a.setName("a");
124         when(topicRepository.findById("a")).thenReturn(Optional.of(a));
125         topicName = topicController.updateTopic(new Topic("a"), mockBindingResult, httpServletResponse);
126         assertEquals(new Topic("a"), topicName);
127         when(mockBindingResult.hasErrors()).thenReturn(true);
128         topicName = topicController.updateTopic(new Topic("a"), mockBindingResult, httpServletResponse);
129         assertEquals(null, topicName);
130
131         ArrayList<Topic> topics = new ArrayList<>();
132         topics.add(a);
133         when(topicRepository.findAll()).thenReturn(topics);
134         Iterable<Topic> list = topicController.list();
135         for (Topic newTopic : list) {
136             assertEquals(a, newTopic);
137         }
138     }
139
140     @Test
141     public void testAddDb() throws NoSuchFieldException, IllegalAccessException, IOException {
142         TopicController topicController = new TopicController();
143         setAccessPrivateFields(topicController);
144         String dbName = "Elecsticsearch";
145         String name = "a";
146         Topic topic = new Topic(name);
147         topic.setEnabled(true);
148         Set<Db> dbSet = new HashSet<>();
149         dbSet.add(new Db(dbName));
150         topic.setDbs(dbSet);
151
152         when(topicRepository.findById(name)).thenReturn(Optional.of(topic));
153         topicController.addDb("a", dbName, httpServletResponse);
154         topicController.deleteDb("a", dbName, httpServletResponse);
155     }
156
157     @Test
158     public void testGetTopicDbs() throws NoSuchFieldException, IllegalAccessException, IOException {
159         TopicController topicController = new TopicController();
160         setAccessPrivateFields(topicController);
161         String dbName = "Elecsticsearch";
162         String name = "a";
163         Topic topic = new Topic(name);
164         topic.setEnabled(true);
165         Set<Db> dbSet = new HashSet<>();
166         dbSet.add(new Db(dbName));
167         topic.setDbs(dbSet);
168
169         when(topicRepository.findById(name)).thenReturn(Optional.of(topic));
170         topicController.getTopicDbs("a");
171     }
172
173     @Test
174     public void testListDmaapTopics() throws NoSuchFieldException, IllegalAccessException, IOException {
175         TopicController topicController = new TopicController();
176         Field dmaapService = topicController.getClass().getDeclaredField("dmaapService");
177         dmaapService.setAccessible(true);
178         dmaapService.set(topicController, dmaapService1);
179         ArrayList<String> topics = new ArrayList<>();
180         topics.add("a");
181         when(dmaapService1.getTopics()).thenReturn(topics);
182         List<String> strings = topicController.listDmaapTopics();
183         for (String topic : strings) {
184             assertEquals("a", topic);
185         }
186     }
187 }