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