Removed unused imports
[dcaegen2/services.git] / components / datalake-handler / feeder / src / test / java / org / onap / datalake / feeder / controller / TopicControllerTest.java
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.junit.MockitoJUnitRunner;
28 import org.onap.datalake.feeder.config.ApplicationConfiguration;
29 import org.onap.datalake.feeder.controller.domain.PostReturnBody;
30 import org.onap.datalake.feeder.domain.Topic;
31 import org.onap.datalake.feeder.dto.TopicConfig;
32 import org.onap.datalake.feeder.repository.TopicNameRepository;
33 import org.onap.datalake.feeder.repository.TopicRepository;
34 import org.onap.datalake.feeder.service.DbService;
35 import org.onap.datalake.feeder.service.DmaapService;
36 import org.onap.datalake.feeder.service.TopicService;
37 import org.onap.datalake.feeder.util.TestUtil;
38 import org.springframework.validation.BindingResult;
39
40 import javax.servlet.http.HttpServletResponse;
41 import java.io.IOException;
42 import java.util.ArrayList;
43 import java.util.List;
44
45 import static org.junit.Assert.assertEquals;
46 import static org.junit.Assert.assertNull;
47 import static org.mockito.Mockito.when;
48
49 @RunWith(MockitoJUnitRunner.class)
50 public class TopicControllerTest {
51
52         static String DEFAULT_TOPIC_NAME = "_DL_DEFAULT_";
53
54         @Mock
55         private HttpServletResponse httpServletResponse;
56
57         @Mock
58         private BindingResult mockBindingResult;
59
60         @Mock
61         private TopicRepository topicRepository;
62
63         @Mock
64         private TopicService topicService;
65
66         @Mock
67         private TopicNameRepository topicNameRepository;
68
69         @InjectMocks
70         TopicController topicController;
71
72         @Mock
73         private ApplicationConfiguration config;
74
75         @Mock
76         private DbService dbService;
77
78         @Mock
79         private DmaapService dmaapService;
80
81         @Before
82         public void setupTest() throws NoSuchFieldException, IllegalAccessException {
83                 // While the default boolean return value for a mock is 'false',
84                 // it's good to be explicit anyway:
85                 when(mockBindingResult.hasErrors()).thenReturn(false);
86         }
87
88         @Test
89         public void testListTopic() throws IOException, NoSuchFieldException, IllegalAccessException {
90         }
91
92         @Test
93         public void testCreateTopic() throws IOException {
94                 Topic a = TestUtil.newTopic("a");
95                 a.setId(1);
96                 a.setEnabled(true);
97
98                 TopicConfig ac = a.getTopicConfig();
99
100                 when(topicService.fillTopicConfiguration(ac)).thenReturn(a);
101                 PostReturnBody<TopicConfig> postTopic = topicController.createTopic(ac, mockBindingResult, httpServletResponse);
102                 assertEquals(postTopic.getStatusCode(), 200);
103
104                 when(mockBindingResult.hasErrors()).thenReturn(true);
105                 PostReturnBody<TopicConfig> topicConfig = topicController.createTopic(ac, mockBindingResult, httpServletResponse);
106                 assertEquals(null, topicConfig);
107         }
108
109         @Test
110         public void testUpdateTopic() throws IOException {
111                 Topic a = TestUtil.newTopic("a");
112                 a.setId(1);
113                 a.setEnabled(true);
114
115                 TopicConfig ac = a.getTopicConfig();
116
117                 when(topicService.getTopic(1)).thenReturn(a);
118                 PostReturnBody<TopicConfig> postConfig1 = topicController.updateTopic(1, ac, mockBindingResult, httpServletResponse);
119                 assertEquals(200, postConfig1.getStatusCode());
120                 TopicConfig ret = postConfig1.getReturnBody();
121                 assertEquals("a", ret.getName());
122                 assertEquals(true, ret.isEnabled());
123
124                 topicController.updateTopic(0, ac, mockBindingResult, httpServletResponse);
125
126                 when(topicService.getTopic(1)).thenReturn(null);
127                 topicController.updateTopic(1, ac, mockBindingResult, httpServletResponse);
128
129                 when(mockBindingResult.hasErrors()).thenReturn(true);
130                 PostReturnBody<TopicConfig> postConfig2 = topicController.updateTopic(1, ac, mockBindingResult, httpServletResponse);
131                 assertNull(postConfig2);
132
133         }
134
135         @Test
136         public void testGetTopic() throws IOException {
137                 Topic a = TestUtil.newTopic("a");
138                 a.setId(1);
139                 a.setEnabled(true);
140
141                 when(topicService.getTopic(1)).thenReturn(a);
142                 TopicConfig ac = topicController.getTopic(1, httpServletResponse);
143                 when(topicService.getTopic(1)).thenReturn(null);
144                 ac = topicController.getTopic(1, httpServletResponse);
145         }
146
147         @Test
148         public void testDeleteTopic() throws IOException {
149                 Topic a = TestUtil.newTopic("a");
150                 a.setId(1);
151                 a.setEnabled(true);
152
153                 when(topicService.getTopic(1)).thenReturn(a);
154                 topicController.deleteTopic(1, httpServletResponse);
155                 when(topicService.getTopic(1)).thenReturn(null);
156                 topicController.deleteTopic(1, httpServletResponse);
157         }
158
159         @Test
160         public void testList() {
161                 ArrayList<Topic> topics = new ArrayList<>();
162                 topics.add(TestUtil.newTopic("a"));
163                 topics.add(TestUtil.newTopic(DEFAULT_TOPIC_NAME));
164                 when(topicRepository.findAll()).thenReturn(topics);
165
166                 List<String> strings = topicController.list();
167                 for (String topic : strings) {
168                         System.out.println(topic);
169                 }
170         }
171 }