9318ee007bcd9a20469c8122c80ef6fc9b08e1ff
[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 import java.util.Optional;
48
49 import static org.junit.Assert.assertEquals;
50 import static org.junit.Assert.assertNotEquals;
51 import static org.junit.Assert.assertNotNull;
52
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.setId(1);
73         dbConfig.setName("Elecsticsearch");
74         dbConfig.setHost("localhost");
75         dbConfig.setLogin("root");
76         dbConfig.setPass("root123");
77         dbConfig.setDatabase("Elecsticsearch");
78         dbConfig.setPort(123);
79         dbConfig.setPoperties("driver");
80         dbConfig.setDbTypeId("ES");
81         return dbConfig;
82     }
83
84     public void setAccessPrivateFields(DbController dbController) throws NoSuchFieldException,
85             IllegalAccessException {
86         Field dbRepository1 = dbController.getClass().getDeclaredField("dbRepository");
87         dbRepository1.setAccessible(true);
88         dbRepository1.set(dbController, dbRepository);
89     }
90
91     @Before
92     public void setupTest() {
93         MockitoAnnotations.initMocks(this);
94         // While the default boolean return value for a mock is 'false',
95         // it's good to be explicit anyway:
96         when(mockBindingResult.hasErrors()).thenReturn(false);
97     }
98
99     @Test(expected = NullPointerException.class)
100     public void testCreateDb() throws IOException, NoSuchFieldException, IllegalAccessException {
101         DbController dbController = new DbController();
102         DbConfig dbConfig = getDbConfig();
103         setAccessPrivateFields(dbController);
104         PostReturnBody<DbConfig> db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse);
105         assertEquals(200, db.getStatusCode());
106         when(mockBindingResult.hasErrors()).thenReturn(true);
107         db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse);
108         assertEquals(null, db);
109     }
110
111     @Test
112     public void testUpdateDb() throws IOException, NoSuchFieldException, IllegalAccessException {
113         DbController dbController = new DbController();
114         DbConfig dbConfig = getDbConfig();
115         when(mockBindingResult.hasErrors()).thenReturn(true);
116         PostReturnBody<DbConfig> db = dbController.updateDb(dbConfig.getId(), dbConfig, mockBindingResult,
117                                                             httpServletResponse);
118         assertEquals(null, db);
119         //when(mockBindingResult.hasErrors()).thenReturn(false);
120         setAccessPrivateFields(dbController);
121         //db = dbController.updateDb(dbConfig, mockBindingResult, httpServletResponse);
122         assertEquals(null, db);
123         //when(mockBindingResult.hasErrors()).thenReturn(false);
124         String name = "Elecsticsearch";
125         int testId = 1234;
126         when(dbRepository.findById(testId)).thenReturn(Optional.of(TestUtil.newDb(name)));
127         //db = dbController.updateDb(dbConfig, mockBindingResult, httpServletResponse);
128         //assertEquals(200, db.getStatusCode());
129         DbConfig elecsticsearch = dbController.getDb(testId, 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         int testId = 1234;
138         List<Db> dbs = new ArrayList<>();
139         dbs.add(TestUtil.newDb(name));
140         setAccessPrivateFields(dbController);
141         when(dbRepository.findAll()).thenReturn(dbs);
142         List<Integer> list = dbController.list();
143         for (int id : list) {
144             assertNotEquals(1234, id);
145         }
146         //dbController.deleteDb("Elecsticsearch", httpServletResponse);
147     }
148
149
150     @Test
151     public void testDeleteDb() throws IOException, IllegalAccessException, NoSuchFieldException {
152         DbController dbController = new DbController();
153         String dbName = "Elecsticsearch";
154         String topicName = "a";
155         Topic topic = TestUtil.newTopic(topicName);
156         topic.setEnabled(true);
157         topic.setId(1);
158         Set<Topic> topics = new HashSet<>();
159         topics.add(topic);
160         Db db1 = TestUtil.newDb(dbName);
161         db1.setTopics(topics);
162         setAccessPrivateFields(dbController);
163         Set<Topic> elecsticsearch = dbController.getDbTopics(dbName, httpServletResponse);
164         assertEquals(Collections.emptySet(), elecsticsearch);
165         when(dbRepository.findByName(dbName)).thenReturn(db1);
166         elecsticsearch = dbController.getDbTopics(dbName, httpServletResponse);
167         for (Topic anElecsticsearch : elecsticsearch) {
168                 Topic tmp = TestUtil.newTopic(topicName);
169                 tmp.setId(2);
170             assertNotEquals(tmp, anElecsticsearch);
171         }
172         //dbController.deleteDb(dbName, httpServletResponse);
173     }
174
175     @Test(expected = NullPointerException.class)
176     public void testPostReturnBody() throws IOException, NoSuchFieldException, IllegalAccessException {
177         DbController dbController = new DbController();
178         DbConfig dbConfig = getDbConfig();
179         setAccessPrivateFields(dbController);
180         PostReturnBody<DbConfig> db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse);
181         assertNotNull(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 }