2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright 2020 China Mobile. 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
 
  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.des.service;
 
  23 import static org.junit.Assert.assertEquals;
 
  24 import static org.mockito.Mockito.when;
 
  26 import java.util.ArrayList;
 
  27 import java.util.List;
 
  28 import java.util.Optional;
 
  30 import org.junit.Test;
 
  31 import org.junit.runner.RunWith;
 
  32 import org.mockito.InjectMocks;
 
  33 import org.mockito.Mock;
 
  34 import org.mockito.junit.MockitoJUnitRunner;
 
  35 import org.onap.datalake.des.domain.DataExposure;
 
  36 import org.onap.datalake.des.domain.Db;
 
  37 import org.onap.datalake.des.domain.DbType;
 
  38 import org.onap.datalake.des.dto.DataExposureConfig;
 
  39 import org.onap.datalake.des.dto.DbConfig;
 
  40 import org.onap.datalake.des.repository.DataExposureRepository;
 
  41 import org.onap.datalake.des.repository.DbRepository;
 
  42 import org.springframework.context.ApplicationContext;
 
  45  * Test DB exposure Service.
 
  49 @RunWith(MockitoJUnitRunner.class)
 
  50 public class DataExposureServiceTest {
 
  53     private DbType dbType;
 
  56     private ApplicationContext context;
 
  59     private DbRepository dbRepository;
 
  62     private DataExposureRepository dataExposureRepository;
 
  65     private DataExposureService dataExposureService;
 
  68      * Generate data exposure config.
 
  70      * @return DataExposureConfig object
 
  73     public DataExposureConfig getDataExposureConfig() {
 
  74         DataExposureConfig deConfig = new DataExposureConfig();
 
  76         deConfig.setId("test");
 
  77         deConfig.setNote("testSql");
 
  78         deConfig.setSqlTemplate("select name from datalake");
 
  85      * @return DbConfig object
 
  88     public DbConfig getDbConfig() {
 
  89         DbConfig dbConfig = new DbConfig();
 
  91         dbConfig.setName("Elecsticsearch");
 
  92         dbConfig.setHost("localhost");
 
  93         dbConfig.setLogin("root");
 
  94         dbConfig.setPass("root123");
 
  95         dbConfig.setDatabase("Elecsticsearch");
 
  96         dbConfig.setPort(123);
 
  97         dbConfig.setPoperties("driver");
 
  98         dbConfig.setDbTypeId("ES");
 
 103     public void testQueryAllDataExposure() {
 
 105         DbConfig dbConfig = getDbConfig();
 
 106         newdb.setName(dbConfig.getName());
 
 107         newdb.setHost(dbConfig.getHost());
 
 108         newdb.setPort(dbConfig.getPort());
 
 109         newdb.setEnabled(dbConfig.isEnabled());
 
 110         newdb.setLogin(dbConfig.getLogin());
 
 111         newdb.setPass(dbConfig.getPass());
 
 112         newdb.setEncrypt(dbConfig.isEncrypt());
 
 113         DataExposureConfig deConfig = getDataExposureConfig();
 
 114         DataExposure de = new DataExposure();
 
 116         de.setId(deConfig.getId());
 
 117         de.setNote(deConfig.getNote());
 
 118         de.setSqlTemplate(deConfig.getSqlTemplate());
 
 119         List<DataExposure> deList = new ArrayList<>();
 
 121         when(dataExposureRepository.findAll()).thenReturn(deList);
 
 122         List<DataExposureConfig> deConfigList = dataExposureService.queryAllDataExposure();
 
 123         assertEquals(de.getId(), deConfigList.get(0).getId());
 
 127     public void testFillDataExposureConfiguration() {
 
 129         DbConfig dbConfig = getDbConfig();
 
 130         newdb.setName(dbConfig.getName());
 
 131         newdb.setHost(dbConfig.getHost());
 
 132         newdb.setPort(dbConfig.getPort());
 
 133         newdb.setEnabled(dbConfig.isEnabled());
 
 134         newdb.setLogin(dbConfig.getLogin());
 
 135         newdb.setPass(dbConfig.getPass());
 
 136         newdb.setEncrypt(dbConfig.isEncrypt());
 
 137         DataExposureConfig deConfig = getDataExposureConfig();
 
 138         when(dbRepository.findById(deConfig.getDbId())).thenReturn(Optional.of(newdb));
 
 139         DataExposure de = dataExposureService.fillDataExposureConfiguration(deConfig);
 
 140         assertEquals(de.getId(), deConfig.getId());
 
 144     public void testFillDataExposureConfigurationWithTwoPara() {
 
 146         DbConfig dbConfig = getDbConfig();
 
 147         newdb.setName(dbConfig.getName());
 
 148         newdb.setHost(dbConfig.getHost());
 
 149         newdb.setPort(dbConfig.getPort());
 
 150         newdb.setEnabled(dbConfig.isEnabled());
 
 151         newdb.setLogin(dbConfig.getLogin());
 
 152         newdb.setPass(dbConfig.getPass());
 
 153         newdb.setEncrypt(dbConfig.isEncrypt());
 
 154         DataExposureConfig deConfig = getDataExposureConfig();
 
 155         when(dbRepository.findById(deConfig.getDbId())).thenReturn(Optional.of(newdb));
 
 156         DataExposure de = new DataExposure();
 
 158         de.setId(deConfig.getId());
 
 159         de.setNote(deConfig.getNote());
 
 160         de.setSqlTemplate(deConfig.getSqlTemplate());
 
 161         dataExposureService.fillDataExposureConfiguration(deConfig, de);