c46a026b9cf6eb980292ade73f5a9806a45c6239
[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.setName("Elecsticsearch");
73         dbConfig.setHost("localhost");
74         dbConfig.setLogin("root");
75         dbConfig.setPass("root123");
76         dbConfig.setDatabase("Elecsticsearch");
77         dbConfig.setPort(123);
78         dbConfig.setPoperties("driver");
79         dbConfig.setDbTypeId("ES");
80         return dbConfig;
81     }
82
83     public void setAccessPrivateFields(DbController dbController) throws NoSuchFieldException,
84             IllegalAccessException {
85         Field dbRepository1 = dbController.getClass().getDeclaredField("dbRepository");
86         dbRepository1.setAccessible(true);
87         dbRepository1.set(dbController, dbRepository);
88     }
89
90     @Before
91     public void setupTest() {
92         MockitoAnnotations.initMocks(this);
93         // While the default boolean return value for a mock is 'false',
94         // it's good to be explicit anyway:
95         when(mockBindingResult.hasErrors()).thenReturn(false);
96     }
97
98     @Test(expected = NullPointerException.class)
99     public void testCreateDb() throws IOException, NoSuchFieldException, IllegalAccessException {
100         DbController dbController = new DbController();
101         DbConfig dbConfig = getDbConfig();
102         setAccessPrivateFields(dbController);
103         PostReturnBody<DbConfig> db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse);
104         assertEquals(200, db.getStatusCode());
105         when(mockBindingResult.hasErrors()).thenReturn(true);
106         db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse);
107         assertEquals(null, db);
108     }
109
110     @Test
111     public void testUpdateDb() throws IOException, NoSuchFieldException, IllegalAccessException {
112         DbController dbController = new DbController();
113         DbConfig dbConfig = getDbConfig();
114         when(mockBindingResult.hasErrors()).thenReturn(true);
115         PostReturnBody<DbConfig> db = dbController.updateDb(dbConfig, mockBindingResult,
116                                                             httpServletResponse);
117         assertEquals(null, db);
118         //when(mockBindingResult.hasErrors()).thenReturn(false);
119         setAccessPrivateFields(dbController);
120         //db = dbController.updateDb(dbConfig, mockBindingResult, httpServletResponse);
121         assertEquals(null, db);
122         //when(mockBindingResult.hasErrors()).thenReturn(false);
123         String name = "Elecsticsearch";
124         int testId = 1234;
125         when(dbRepository.findById(testId)).thenReturn(Optional.of(TestUtil.newDb(name)));
126         //db = dbController.updateDb(dbConfig, mockBindingResult, httpServletResponse);
127         //assertEquals(200, db.getStatusCode());
128         DbConfig elecsticsearch = dbController.getDb(testId, 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         int testId = 1234;
137         List<Db> dbs = new ArrayList<>();
138         dbs.add(TestUtil.newDb(name));
139         setAccessPrivateFields(dbController);
140         when(dbRepository.findAll()).thenReturn(dbs);
141         List<Integer> list = dbController.list();
142         for (int id : list) {
143             assertNotEquals(1234, id);
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(expected = NullPointerException.class)
175     public void testPostReturnBody() throws IOException, NoSuchFieldException, IllegalAccessException {
176         DbController dbController = new DbController();
177         DbConfig dbConfig = getDbConfig();
178         setAccessPrivateFields(dbController);
179         PostReturnBody<DbConfig> db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse);
180         assertNotNull(db);
181     }
182
183     @Test
184     public void testVerifyConnection() throws IOException {
185         DbController dbController = new DbController();
186         DbConfig dbConfig = getDbConfig();
187         PostReturnBody<DbConfig> dbConfigPostReturnBody = dbController.verifyDbConnection(dbConfig, httpServletResponse);
188         assertEquals(null, dbConfigPostReturnBody);
189     }
190
191 }