54ee2b2dee0de55ee22578521aaf0dce18f2017b
[dcaegen2/services.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : DataLake
4  * ================================================================================
5  * Copyright 2019 China Mobile
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
21 package org.onap.datalake.feeder.controller;
22
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.domain.Kafka;
29 import org.onap.datalake.feeder.dto.KafkaConfig;
30 import org.onap.datalake.feeder.repository.KafkaRepository;
31 import org.onap.datalake.feeder.service.KafkaService;
32 import org.springframework.validation.BindingResult;
33
34 import javax.servlet.http.HttpServletResponse;
35
36 import java.io.IOException;
37
38 import static org.junit.Assert.*;
39 import static org.mockito.Mockito.when;
40
41 @RunWith(MockitoJUnitRunner.class)
42 public class KafkaControllerTest {
43
44     @Mock
45     private HttpServletResponse httpServletResponse;
46
47     @Mock
48     private BindingResult mockBindingResult;
49
50     @Mock
51     private KafkaService kafkaService;
52
53     @Mock
54     private KafkaRepository kafkaRepository;
55
56     @Mock
57     private Kafka kafka;
58
59     @InjectMocks
60     private KafkaController kafkaController;
61     @Test
62     public void createKafka() throws IOException {
63         int id = 123;
64         KafkaConfig kafkaConfig = new KafkaConfig();
65         kafkaConfig.setId(id);
66         kafkaConfig.setName("123");
67         when(kafkaService.getKafkaById(kafkaConfig.getId())).thenReturn(null).thenReturn(kafka);
68         when(kafkaRepository.save(kafka)).thenReturn(null);
69         when(kafkaService.fillKafkaConfiguration(kafkaConfig)).thenReturn(kafka);
70         when(mockBindingResult.hasErrors()).thenReturn(false,true,false,true);
71
72         kafkaController.createKafka(kafkaConfig,mockBindingResult,httpServletResponse);
73         kafkaController.createKafka(kafkaConfig,mockBindingResult,httpServletResponse);
74
75         kafkaController.updateKafka(kafkaConfig,mockBindingResult,id,httpServletResponse);
76         kafkaController.updateKafka(kafkaConfig,mockBindingResult,id,httpServletResponse);
77
78         kafkaController.deleteKafka(id,httpServletResponse);
79
80         when(kafkaService.getAllKafka()).thenReturn(null);
81         kafkaController.queryAllKafka();
82     }
83 }