3a9d9c8dccb0371ae0ab367c260ca573e15f1107
[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.junit.Assert.assertNotEquals;
49 import static org.junit.Assert.assertNotNull;
50 import static org.mockito.Mockito.when;
51
52 @RunWith(MockitoJUnitRunner.class)
53 public class DbControllerTest {
54
55     @Mock
56     private HttpServletResponse httpServletResponse;
57
58     @Mock
59     private DbRepository dbRepository;
60
61     @Mock
62     private BindingResult mockBindingResult;
63
64     @InjectMocks
65     private DbService dbService1;
66
67     public DbConfig getDbConfig() {
68         DbConfig dbConfig = new DbConfig();
69         dbConfig.setName("Elecsticsearch");
70         dbConfig.setHost("localhost");
71         dbConfig.setLogin("root");
72         dbConfig.setPassword("root123");
73         dbConfig.setDatabase("Elecsticsearch");
74         dbConfig.setPort(123);
75         dbConfig.setPoperties("driver");
76         return dbConfig;
77     }
78
79     public void setAccessPrivateFields(DbController dbController) throws NoSuchFieldException,
80             IllegalAccessException {
81         Field dbService = dbController.getClass().getDeclaredField("dbService");
82         dbService.setAccessible(true);
83         dbService.set(dbController, dbService1);
84         Field dbRepository1 = dbController.getClass().getDeclaredField("dbRepository");
85         dbRepository1.setAccessible(true);
86         dbRepository1.set(dbController, dbRepository);
87     }
88
89     @Before
90     public void setupTest() {
91         MockitoAnnotations.initMocks(this);
92         // While the default boolean return value for a mock is 'false',
93         // it's good to be explicit anyway:
94         when(mockBindingResult.hasErrors()).thenReturn(false);
95     }
96
97     @Test
98     public void testCreateDb() throws IOException, NoSuchFieldException, IllegalAccessException {
99         DbController dbController = new DbController();
100         DbConfig dbConfig = getDbConfig();
101         setAccessPrivateFields(dbController);
102         PostReturnBody<DbConfig> db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse);
103         assertEquals(200, db.getStatusCode());
104         when(mockBindingResult.hasErrors()).thenReturn(true);
105         db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse);
106         assertEquals(null, db);
107     }
108
109     @Test
110     public void testUpdateDb() throws IOException, NoSuchFieldException, IllegalAccessException {
111         DbController dbController = new DbController();
112         DbConfig dbConfig = getDbConfig();
113         when(mockBindingResult.hasErrors()).thenReturn(true);
114         PostReturnBody<DbConfig> db = dbController.updateDb(dbConfig, mockBindingResult,
115                                                             httpServletResponse);
116         assertEquals(null, db);
117         when(mockBindingResult.hasErrors()).thenReturn(false);
118         setAccessPrivateFields(dbController);
119         db = dbController.updateDb(dbConfig, mockBindingResult,
120                                    httpServletResponse);
121         assertEquals(null, db);
122         when(mockBindingResult.hasErrors()).thenReturn(false);
123         String name = "Elecsticsearch";
124         when(dbRepository.findByName(name)).thenReturn(new Db(name));
125         db = dbController.updateDb(dbConfig, mockBindingResult,
126                                    httpServletResponse);
127         assertEquals(200, db.getStatusCode());
128         Db elecsticsearch = dbController.getDb("Elecsticsearch", httpServletResponse);
129         assertNotNull(elecsticsearch);
130     }
131
132     @Test
133     public void testGetAllDbs() throws IOException, IllegalAccessException, NoSuchFieldException {
134         DbController dbController = new DbController();
135         String name = "Elecsticsearch";
136         List<Db> dbs = new ArrayList<>();
137         dbs.add(new Db(name));
138         setAccessPrivateFields(dbController);
139         when(dbRepository.findAll()).thenReturn(dbs);
140         List<String> list = dbController.list();
141         for (String dbName : list) {
142             assertEquals("Elecsticsearch", dbName);
143         }
144         dbController.deleteDb("Elecsticsearch", httpServletResponse);
145     }
146
147
148     @Test
149     public void testDeleteDb() throws IOException, IllegalAccessException, NoSuchFieldException {
150         DbController dbController = new DbController();
151         String dbName = "Elecsticsearch";
152         String topicName = "a";
153         Topic topic = new Topic(topicName);
154         topic.setEnabled(true);
155         topic.setId(1);
156         Set<Topic> topics = new HashSet<>();
157         topics.add(topic);
158         Db db1 = new Db(dbName);
159         db1.setTopics(topics);
160         setAccessPrivateFields(dbController);
161         Set<Topic> elecsticsearch = dbController.getDbTopics(dbName, httpServletResponse);
162         assertEquals(null, elecsticsearch);
163         when(dbRepository.findByName(dbName)).thenReturn(db1);
164         elecsticsearch = dbController.getDbTopics(dbName, httpServletResponse);
165         for (Topic anElecsticsearch : elecsticsearch) {
166                 Topic tmp = new Topic(topicName);
167                 tmp.setId(2);
168             assertNotEquals(tmp, anElecsticsearch);
169         }
170         dbController.deleteDb(dbName, httpServletResponse);
171     }
172
173     @Test
174     public void testPostReturnBody() throws IOException, NoSuchFieldException, IllegalAccessException {
175         DbController dbController = new DbController();
176         DbConfig dbConfig = getDbConfig();
177         setAccessPrivateFields(dbController);
178         String name = "Elecsticsearch";
179         when(dbRepository.findByName(name)).thenReturn(new Db(name));
180         PostReturnBody<DbConfig> db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse);
181         assertEquals(null, db);
182     }
183
184     @Test
185     public void testVerifyConnection() throws IOException {
186         DbController dbController = new DbController();
187         DbConfig dbConfig = getDbConfig();
188         PostReturnBody<DbConfig> dbConfigPostReturnBody = dbController.verifyDbConnection(dbConfig, httpServletResponse);
189         assertEquals(null, dbConfigPostReturnBody);
190     }
191
192 }