4a6d6beefee17fde7e9088450b5f1413e465ee8e
[dcaegen2/services.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : DATALAKE
4  * ================================================================================
5  * Copyright (C) 2018-2019 Huawei. 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.feeder.controller;
22
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.InjectMocks;
27 import org.mockito.Mock;
28 import org.mockito.MockitoAnnotations;
29 import org.mockito.junit.MockitoJUnitRunner;
30 import org.onap.datalake.feeder.dto.DbConfig;
31 import org.onap.datalake.feeder.controller.domain.PostReturnBody;
32 import org.onap.datalake.feeder.domain.Db;
33 import org.onap.datalake.feeder.domain.Topic;
34 import org.onap.datalake.feeder.repository.DbRepository;
35 import org.onap.datalake.feeder.service.DbService;
36 import org.springframework.validation.BindingResult;
37
38 import javax.servlet.http.HttpServletResponse;
39 import java.io.IOException;
40 import java.lang.reflect.Field;
41 import java.util.ArrayList;
42 import java.util.HashSet;
43 import java.util.List;
44 import java.util.Optional;
45 import java.util.Set;
46
47 import static org.junit.Assert.assertEquals;
48 import static org.mockito.Mockito.when;
49
50 @RunWith(MockitoJUnitRunner.class)
51 public class DbControllerTest {
52
53     @Mock
54     private HttpServletResponse httpServletResponse;
55
56     @Mock
57     private DbRepository dbRepository;
58
59     @Mock
60     private BindingResult mockBindingResult;
61
62     @InjectMocks
63     private DbService dbService1;
64
65     public DbConfig getDbConfig() {
66         DbConfig dbConfig = new DbConfig();
67         dbConfig.setName("Elecsticsearch");
68         dbConfig.setHost("localhost");
69         dbConfig.setLogin("root");
70         dbConfig.setPassword("root123");
71         dbConfig.setDatabase("Elecsticsearch");
72         dbConfig.setPort(123);
73         dbConfig.setPoperties("driver");
74         return dbConfig;
75     }
76
77     public void setAccessPrivateFields(DbController dbController) throws NoSuchFieldException,
78             IllegalAccessException {
79         Field dbService = dbController.getClass().getDeclaredField("dbService");
80         dbService.setAccessible(true);
81         dbService.set(dbController, dbService1);
82         Field dbRepository1 = dbController.getClass().getDeclaredField("dbRepository");
83         dbRepository1.setAccessible(true);
84         dbRepository1.set(dbController, dbRepository);
85     }
86
87     @Before
88     public void setupTest() {
89         MockitoAnnotations.initMocks(this);
90         // While the default boolean return value for a mock is 'false',
91         // it's good to be explicit anyway:
92         when(mockBindingResult.hasErrors()).thenReturn(false);
93     }
94
95     @Test
96     public void testCreateDb() throws IOException, NoSuchFieldException, IllegalAccessException {
97         DbController dbController = new DbController();
98         DbConfig dbConfig = getDbConfig();
99         setAccessPrivateFields(dbController);
100         PostReturnBody<DbConfig> db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse);
101         assertEquals(200, db.getStatusCode());
102         when(mockBindingResult.hasErrors()).thenReturn(true);
103         db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse);
104         assertEquals(null, db);
105     }
106
107     @Test
108     public void testUpdateDb() throws IOException, NoSuchFieldException, IllegalAccessException {
109         DbController dbController = new DbController();
110         DbConfig dbConfig = getDbConfig();
111         when(mockBindingResult.hasErrors()).thenReturn(true);
112         PostReturnBody<DbConfig> db = dbController.updateDb(dbConfig, mockBindingResult,
113                                                             httpServletResponse);
114         assertEquals(null, db);
115         when(mockBindingResult.hasErrors()).thenReturn(false);
116         setAccessPrivateFields(dbController);
117         db = dbController.updateDb(dbConfig, mockBindingResult,
118                                    httpServletResponse);
119         assertEquals(null, db);
120         when(mockBindingResult.hasErrors()).thenReturn(false);
121         String name = "Elecsticsearch";
122         when(dbRepository.findById(name)).thenReturn(Optional.of(new Db(name)));
123         db = dbController.updateDb(dbConfig, mockBindingResult,
124                                    httpServletResponse);
125         assertEquals(200, db.getStatusCode());
126         Db elecsticsearch = dbController.getDb("Elecsticsearch", httpServletResponse);
127         assertEquals(null, elecsticsearch);
128     }
129
130     @Test
131     public void testGetAllDbs() throws IOException, IllegalAccessException, NoSuchFieldException {
132         DbController dbController = new DbController();
133         String name = "Elecsticsearch";
134         List<Db> dbs = new ArrayList<>();
135         dbs.add(new Db(name));
136         setAccessPrivateFields(dbController);
137         when(dbRepository.findAll()).thenReturn(dbs);
138         List<String> list = dbController.list();
139         for (String dbName : list) {
140             assertEquals("Elecsticsearch", dbName);
141         }
142         dbController.deleteDb("Elecsticsearch", httpServletResponse);
143     }
144
145
146     @Test
147     public void testDeleteDb() throws IOException, IllegalAccessException, NoSuchFieldException {
148         DbController dbController = new DbController();
149         String dbName = "Elecsticsearch";
150         String topicName = "a";
151         Topic topic = new Topic(topicName);
152         topic.setEnabled(true);
153         Set<Topic> topics = new HashSet<>();
154         topics.add(topic);
155         Db db1 = new Db(dbName);
156         db1.setTopics(topics);
157         setAccessPrivateFields(dbController);
158         Set<Topic> elecsticsearch = dbController.getDbTopics(dbName, httpServletResponse);
159         assertEquals(null, elecsticsearch);
160         when(dbRepository.findByName(dbName)).thenReturn(db1);
161         elecsticsearch = dbController.getDbTopics(dbName, httpServletResponse);
162         for (Topic anElecsticsearch : elecsticsearch) {
163             assertEquals(new Topic(topicName), anElecsticsearch);
164         }
165         dbController.deleteDb(dbName, httpServletResponse);
166     }
167
168     @Test
169     public void testPostReturnBody() throws IOException, NoSuchFieldException, IllegalAccessException {
170         DbController dbController = new DbController();
171         DbConfig dbConfig = getDbConfig();
172         setAccessPrivateFields(dbController);
173         String name = "Elecsticsearch";
174         when(dbRepository.findById(name)).thenReturn(Optional.of(new Db(name)));
175         PostReturnBody<DbConfig> db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse);
176         assertEquals(null, db);
177     }
178
179     @Test
180     public void testVerifyConnection() throws IOException {
181         DbController dbController = new DbController();
182         DbConfig dbConfig = getDbConfig();
183         PostReturnBody<DbConfig> dbConfigPostReturnBody = dbController.verifyDbConnection(dbConfig, httpServletResponse);
184         assertEquals(null, dbConfigPostReturnBody);
185     }
186
187 }