2  * ============LICENSE_START=======================================================
 
   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
 
  11  *     http://www.apache.org/licenses/LICENSE-2.0
 
  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=========================================================
 
  21 package org.onap.datalake.feeder.controller;
 
  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;
 
  34 import javax.servlet.http.HttpServletResponse;
 
  36 import java.io.IOException;
 
  38 import static org.junit.Assert.*;
 
  39 import static org.mockito.Mockito.when;
 
  41 @RunWith(MockitoJUnitRunner.class)
 
  42 public class KafkaControllerTest {
 
  45     private HttpServletResponse httpServletResponse;
 
  48     private BindingResult mockBindingResult;
 
  51     private KafkaService kafkaService;
 
  54     private KafkaRepository kafkaRepository;
 
  60     private KafkaController kafkaController;
 
  62     public void createKafka() throws IOException {
 
  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);
 
  73         kafkaController.createKafka(kafkaConfig,mockBindingResult,httpServletResponse);
 
  74         kafkaController.createKafka(kafkaConfig,mockBindingResult,httpServletResponse);
 
  76         kafkaController.updateKafka(kafkaConfig,mockBindingResult,id,httpServletResponse);
 
  77         kafkaController.updateKafka(kafkaConfig,mockBindingResult,id,httpServletResponse);
 
  79         kafkaController.deleteKafka(id,httpServletResponse);
 
  81         when(kafkaService.getAllKafka()).thenReturn(null);
 
  82         kafkaController.queryAllKafka();