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