06aa61dba5724e62bc2b2c86e122c9f27f00454e
[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
64         int id = 123;
65         KafkaConfig kafkaConfig = new KafkaConfig();
66         kafkaConfig.setId(id);
67         kafkaConfig.setName("123");
68         when(kafkaService.getKafkaById(kafkaConfig.getId())).thenReturn(null).thenReturn(kafka);
69         when(kafkaRepository.save(kafka)).thenReturn(null);
70         when(kafkaService.fillKafkaConfiguration(kafkaConfig)).thenReturn(kafka);
71         when(mockBindingResult.hasErrors()).thenReturn(false,true,false,true);
72
73         kafkaController.createKafka(kafkaConfig,mockBindingResult,httpServletResponse);
74         kafkaController.createKafka(kafkaConfig,mockBindingResult,httpServletResponse);
75
76         kafkaController.updateKafka(kafkaConfig,mockBindingResult,id,httpServletResponse);
77         kafkaController.updateKafka(kafkaConfig,mockBindingResult,id,httpServletResponse);
78
79         kafkaController.deleteKafka(id,httpServletResponse);
80
81         when(kafkaService.getAllKafka()).thenReturn(null);
82         kafkaController.queryAllKafka();
83     }
84 }