254afcb54b7e94f3137b2074b4e9fb50474ea757
[dcaegen2/services.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : DataLake DES
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
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.des.service;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.Mockito.when;
25
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.Optional;
29
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;
43
44 /**
45  * Test DB exposure Service.
46  *
47  * @author Kai Lu
48  */
49 @RunWith(MockitoJUnitRunner.class)
50 public class DataExposureServiceTest {
51
52     @Mock
53     private DbType dbType;
54
55     @Mock
56     private ApplicationContext context;
57
58     @Mock
59     private DbRepository dbRepository;
60
61     @Mock
62     private DataExposureRepository dataExposureRepository;
63
64     @InjectMocks
65     private DataExposureService dataExposureService;
66
67     /**
68      * Generate data exposure config.
69      *
70      * @return DataExposureConfig object
71      *
72      */
73     public DataExposureConfig getDataExposureConfig() {
74         DataExposureConfig deConfig = new DataExposureConfig();
75         deConfig.setDbId(3);
76         deConfig.setId("test");
77         deConfig.setNote("testSql");
78         deConfig.setSqlTemplate("select name from datalake");
79         return deConfig;
80     }
81
82     /**
83      * Generate DB config.
84      *
85      * @return DbConfig object
86      *
87      */
88     public DbConfig getDbConfig() {
89         DbConfig dbConfig = new DbConfig();
90         dbConfig.setId(1);
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");
99         return dbConfig;
100     }
101
102     @Test
103     public void testQueryAllDataExposure() {
104         Db newdb = new Db();
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();
115         de.setDb(newdb);
116         de.setId(deConfig.getId());
117         de.setNote(deConfig.getNote());
118         de.setSqlTemplate(deConfig.getSqlTemplate());
119         List<DataExposure> deList = new ArrayList<>();
120         deList.add(de);
121         when(dataExposureRepository.findAll()).thenReturn(deList);
122         List<DataExposureConfig> deConfigList = dataExposureService.queryAllDataExposure();
123         assertEquals(de.getId(), deConfigList.get(0).getId());
124     }
125
126     @Test
127     public void testFillDataExposureConfiguration() {
128         Db newdb = new Db();
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());
141     }
142
143     @Test
144     public void testFillDataExposureConfigurationWithTwoPara() {
145         Db newdb = new Db();
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();
157         de.setDb(newdb);
158         de.setId(deConfig.getId());
159         de.setNote(deConfig.getNote());
160         de.setSqlTemplate(deConfig.getSqlTemplate());
161         dataExposureService.fillDataExposureConfiguration(deConfig, de);
162     }
163
164 }