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