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