summaryrefslogtreecommitdiffstats
path: root/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/DbControllerTest.java
blob: 9318ee007bcd9a20469c8122c80ef6fc9b08e1ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*-
 * ============LICENSE_START=======================================================
 * ONAP : DATALAKE
 * ================================================================================
 * Copyright (C) 2018-2019 Huawei. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.datalake.feeder.controller;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import org.onap.datalake.feeder.controller.domain.PostReturnBody;
import org.onap.datalake.feeder.domain.Db;
import org.onap.datalake.feeder.domain.Topic;
import org.onap.datalake.feeder.dto.DbConfig;
import org.onap.datalake.feeder.repository.DbRepository;
import org.onap.datalake.feeder.service.DbService;
import org.onap.datalake.feeder.util.TestUtil;
import org.springframework.validation.BindingResult;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Collections;
import java.util.Optional;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;

import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class DbControllerTest {

    @Mock
    private HttpServletResponse httpServletResponse;

    @Mock
    private DbRepository dbRepository;

    @Mock
    private BindingResult mockBindingResult;

    @InjectMocks
    private DbService dbService1;
    
    public DbConfig getDbConfig() {
        DbConfig dbConfig = new DbConfig();
        dbConfig.setId(1);
        dbConfig.setName("Elecsticsearch");
        dbConfig.setHost("localhost");
        dbConfig.setLogin("root");
        dbConfig.setPass("root123");
        dbConfig.setDatabase("Elecsticsearch");
        dbConfig.setPort(123);
        dbConfig.setPoperties("driver");
      	dbConfig.setDbTypeId("ES");
        return dbConfig;
    }

    public void setAccessPrivateFields(DbController dbController) throws NoSuchFieldException,
            IllegalAccessException {
        Field dbRepository1 = dbController.getClass().getDeclaredField("dbRepository");
        dbRepository1.setAccessible(true);
        dbRepository1.set(dbController, dbRepository);
    }

    @Before
    public void setupTest() {
        MockitoAnnotations.initMocks(this);
        // While the default boolean return value for a mock is 'false',
        // it's good to be explicit anyway:
        when(mockBindingResult.hasErrors()).thenReturn(false);
    }

    @Test(expected = NullPointerException.class)
    public void testCreateDb() throws IOException, NoSuchFieldException, IllegalAccessException {
        DbController dbController = new DbController();
        DbConfig dbConfig = getDbConfig();
        setAccessPrivateFields(dbController);
        PostReturnBody<DbConfig> db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse);
        assertEquals(200, db.getStatusCode());
        when(mockBindingResult.hasErrors()).thenReturn(true);
        db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse);
        assertEquals(null, db);
    }

    @Test
    public void testUpdateDb() throws IOException, NoSuchFieldException, IllegalAccessException {
        DbController dbController = new DbController();
        DbConfig dbConfig = getDbConfig();
        when(mockBindingResult.hasErrors()).thenReturn(true);
        PostReturnBody<DbConfig> db = dbController.updateDb(dbConfig.getId(), dbConfig, mockBindingResult,
                                                            httpServletResponse);
        assertEquals(null, db);
        //when(mockBindingResult.hasErrors()).thenReturn(false);
        setAccessPrivateFields(dbController);
        //db = dbController.updateDb(dbConfig, mockBindingResult, httpServletResponse);
        assertEquals(null, db);
        //when(mockBindingResult.hasErrors()).thenReturn(false);
        String name = "Elecsticsearch";
        int testId = 1234;
        when(dbRepository.findById(testId)).thenReturn(Optional.of(TestUtil.newDb(name)));
        //db = dbController.updateDb(dbConfig, mockBindingResult, httpServletResponse);
        //assertEquals(200, db.getStatusCode());
        DbConfig elecsticsearch = dbController.getDb(testId, httpServletResponse);
        assertNotNull(elecsticsearch);
    }

    @Test
    public void testGetAllDbs() throws IOException, IllegalAccessException, NoSuchFieldException {
        DbController dbController = new DbController();
        String name = "Elecsticsearch";
        int testId = 1234;
        List<Db> dbs = new ArrayList<>();
        dbs.add(TestUtil.newDb(name));
        setAccessPrivateFields(dbController);
        when(dbRepository.findAll()).thenReturn(dbs);
        List<Integer> list = dbController.list();
        for (int id : list) {
            assertNotEquals(1234, id);
        }
        //dbController.deleteDb("Elecsticsearch", httpServletResponse);
    }


    @Test
    public void testDeleteDb() throws IOException, IllegalAccessException, NoSuchFieldException {
        DbController dbController = new DbController();
        String dbName = "Elecsticsearch";
        String topicName = "a";
        Topic topic = TestUtil.newTopic(topicName);
        topic.setEnabled(true);
        topic.setId(1);
        Set<Topic> topics = new HashSet<>();
        topics.add(topic);
        Db db1 = TestUtil.newDb(dbName);
        db1.setTopics(topics);
        setAccessPrivateFields(dbController);
        Set<Topic> elecsticsearch = dbController.getDbTopics(dbName, httpServletResponse);
        assertEquals(Collections.emptySet(), elecsticsearch);
        when(dbRepository.findByName(dbName)).thenReturn(db1);
        elecsticsearch = dbController.getDbTopics(dbName, httpServletResponse);
        for (Topic anElecsticsearch : elecsticsearch) {
        	Topic tmp = TestUtil.newTopic(topicName);
        	tmp.setId(2);
            assertNotEquals(tmp, anElecsticsearch);
        }
        //dbController.deleteDb(dbName, httpServletResponse);
    }

    @Test(expected = NullPointerException.class)
    public void testPostReturnBody() throws IOException, NoSuchFieldException, IllegalAccessException {
        DbController dbController = new DbController();
        DbConfig dbConfig = getDbConfig();
        setAccessPrivateFields(dbController);
        PostReturnBody<DbConfig> db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse);
        assertNotNull(db);
    }

    @Test
    public void testVerifyConnection() throws IOException {
        DbController dbController = new DbController();
        DbConfig dbConfig = getDbConfig();
        PostReturnBody<DbConfig> dbConfigPostReturnBody = dbController.verifyDbConnection(dbConfig, httpServletResponse);
        assertEquals(null, dbConfigPostReturnBody);
    }

}