3dc836c35681d37f7b12dd1af3eaf5508af9f739
[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.controller.domain.PostReturnBody;
31 import org.onap.datalake.feeder.domain.Db;
32 import org.onap.datalake.feeder.domain.Topic;
33 import org.onap.datalake.feeder.dto.DbConfig;
34 import org.onap.datalake.feeder.repository.DbRepository;
35 import org.onap.datalake.feeder.service.DbService;
36 import org.onap.datalake.feeder.util.TestUtil;
37 import org.springframework.validation.BindingResult;
38
39 import javax.servlet.http.HttpServletResponse;
40 import java.io.IOException;
41 import java.lang.reflect.Field;
42 import java.util.ArrayList;
43 import java.util.HashSet;
44 import java.util.List;
45 import java.util.Set;
46 import java.util.Collections;
47
48 import static org.junit.Assert.assertEquals;
49 import static org.junit.Assert.assertNotEquals;
50 import static org.junit.Assert.assertNotNull;
51
52 import static org.mockito.Mockito.when;
53
54 @RunWith(MockitoJUnitRunner.class)
55 public class DbControllerTest {
56
57     @Mock
58     private HttpServletResponse httpServletResponse;
59
60     @Mock
61     private DbRepository dbRepository;
62
63     @Mock
64     private BindingResult mockBindingResult;
65
66     @InjectMocks
67     private DbService dbService1;
68     
69     public DbConfig getDbConfig() {
70         DbConfig dbConfig = new DbConfig();
71         dbConfig.setName("Elecsticsearch");
72         dbConfig.setHost("localhost");
73         dbConfig.setLogin("root");
74         dbConfig.setPass("root123");
75         dbConfig.setDatabase("Elecsticsearch");
76         dbConfig.setPort(123);
77         dbConfig.setPoperties("driver");
78         dbConfig.setDbTypeId("ES");
79         return dbConfig;
80     }
81
82     public void setAccessPrivateFields(DbController dbController) throws NoSuchFieldException,
83             IllegalAccessException {
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(expected = NullPointerException.class)
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, httpServletResponse);
120         assertEquals(null, db);
121         //when(mockBindingResult.hasErrors()).thenReturn(false);
122         String name = "Elecsticsearch";
123         when(dbRepository.findByName(name)).thenReturn(TestUtil.newDb(name));
124         //db = dbController.updateDb(dbConfig, mockBindingResult, httpServletResponse);
125         //assertEquals(200, db.getStatusCode());
126         Db elecsticsearch = dbController.getDb("Elecsticsearch", httpServletResponse);
127         assertNotNull(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(TestUtil.newDb(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 = TestUtil.newTopic(topicName);
152         topic.setEnabled(true);
153         topic.setId(1);
154         Set<Topic> topics = new HashSet<>();
155         topics.add(topic);
156         Db db1 = TestUtil.newDb(dbName);
157         db1.setTopics(topics);
158         setAccessPrivateFields(dbController);
159         Set<Topic> elecsticsearch = dbController.getDbTopics(dbName, httpServletResponse);
160         assertEquals(Collections.emptySet(), elecsticsearch);
161         when(dbRepository.findByName(dbName)).thenReturn(db1);
162         elecsticsearch = dbController.getDbTopics(dbName, httpServletResponse);
163         for (Topic anElecsticsearch : elecsticsearch) {
164                 Topic tmp = TestUtil.newTopic(topicName);
165                 tmp.setId(2);
166             assertNotEquals(tmp, anElecsticsearch);
167         }
168         //dbController.deleteDb(dbName, httpServletResponse);
169     }
170
171     @Test(expected = NullPointerException.class)
172     public void testPostReturnBody() throws IOException, NoSuchFieldException, IllegalAccessException {
173         DbController dbController = new DbController();
174         DbConfig dbConfig = getDbConfig();
175         setAccessPrivateFields(dbController);
176         PostReturnBody<DbConfig> db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse);
177         assertNotNull(db);
178     }
179
180     @Test
181     public void testVerifyConnection() throws IOException {
182         DbController dbController = new DbController();
183         DbConfig dbConfig = getDbConfig();
184         PostReturnBody<DbConfig> dbConfigPostReturnBody = dbController.verifyDbConnection(dbConfig, httpServletResponse);
185         assertEquals(null, dbConfigPostReturnBody);
186     }
187
188 }