Unit test 3 59/89859/1
authorZhangZihao <zhangzihao@chinamobile.com>
Fri, 14 Jun 2019 09:06:43 +0000 (17:06 +0800)
committerZhangZihao <zhangzihao@chinamobile.com>
Fri, 14 Jun 2019 09:06:57 +0000 (17:06 +0800)
Change-Id: I63afd2b6c70fb2ce0a182fcb988a579f42c9b23d
Issue-ID: DCAEGEN2-1468
Signed-off-by: ZhangZihao <zhangzihao@chinamobile.com>
components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/DesignTypeControllerTest.java [new file with mode: 0644]
components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/PortalControllerTest.java [new file with mode: 0644]
components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/PortalDesignControllerTest.java [new file with mode: 0644]

diff --git a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/DesignTypeControllerTest.java b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/DesignTypeControllerTest.java
new file mode 100644 (file)
index 0000000..ab9d901
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP : DATALAKE
+ * ================================================================================
+ * Copyright 2019 China Mobile
+ *=================================================================================
+ * 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.MockitoAnnotations;
+import org.mockito.junit.MockitoJUnitRunner;
+import org.onap.datalake.feeder.domain.DesignType;
+import org.onap.datalake.feeder.domain.Portal;
+import org.onap.datalake.feeder.service.DesignTypeService;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.Assert.*;
+
+@RunWith(MockitoJUnitRunner.class)
+public class DesignTypeControllerTest {
+
+    @InjectMocks
+    private DesignTypeService designTypeService;
+
+    @Before
+    public void setupTest() {
+        MockitoAnnotations.initMocks(this);
+    }
+
+    @Test(expected = NullPointerException.class)
+    public void getTemplateTypeName() throws NoSuchFieldException, IllegalAccessException {
+
+        DesignTypeController testDesignTypeController = new DesignTypeController();
+        setAccessPrivateFields(testDesignTypeController);
+        DesignType testDesignType = fillDomain();
+        List<String> designTypeNamesList = new ArrayList<>();
+        designTypeNamesList.add(testDesignType.getName());
+        assertEquals(1, testDesignTypeController.getTemplateTypeName().size());
+    }
+
+    public void setAccessPrivateFields(DesignTypeController designTypeController) throws NoSuchFieldException, IllegalAccessException {
+
+        Field testDesignTypeService = designTypeController.getClass().getDeclaredField("designTypeService");
+        testDesignTypeService.setAccessible(true);
+        testDesignTypeService.set(designTypeController, designTypeService);
+    }
+
+
+    public DesignType fillDomain(){
+        DesignType designType = new DesignType();
+        designType.setName("Kibana Dashboard");
+        Portal portal = new Portal();
+        portal.setName("Kibana");
+        portal.setHost("127.0.0.1");
+        portal.setPort(5601);
+        designType.setPortal(portal);
+        return designType;
+    }
+}
\ No newline at end of file
diff --git a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/PortalControllerTest.java b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/PortalControllerTest.java
new file mode 100644 (file)
index 0000000..21327f9
--- /dev/null
@@ -0,0 +1,121 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP : DATALAKE
+ * ================================================================================
+ * Copyright 2019 China Mobile
+ *=================================================================================
+ * 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.Portal;
+import org.onap.datalake.feeder.dto.PortalConfig;
+import org.onap.datalake.feeder.repository.PortalRepository;
+import org.onap.datalake.feeder.service.PortalService;
+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.List;
+import java.util.Optional;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public class PortalControllerTest {
+
+    @Mock
+    private HttpServletResponse httpServletResponse;
+
+    @Mock
+    private BindingResult mockBindingResult;
+
+    @Mock
+    private PortalRepository portalRepository;
+
+    @InjectMocks
+    private PortalService portalService;
+
+    @Before
+    public void setupTest() {
+        MockitoAnnotations.initMocks(this);
+        when(mockBindingResult.hasErrors()).thenReturn(false);
+    }
+
+
+    @Test
+    public void testUpdatePortal() throws NoSuchFieldException, IllegalAccessException, IOException {
+
+        PortalController testPortalController = new PortalController();
+        setAccessPrivateFields(testPortalController);
+        Portal testPortal = fillDomain();
+        when(portalRepository.findById("Kibana")).thenReturn(Optional.of(testPortal));
+        PostReturnBody<PortalConfig> postPortal = testPortalController.updatePortal(testPortal.getPortalConfig(), mockBindingResult, httpServletResponse);
+        assertEquals(postPortal.getStatusCode(), 200);
+        //when(mockBindingResult.hasErrors()).thenReturn(true);
+
+    }
+
+
+    @Test
+    public void testGetPortals() throws NoSuchFieldException, IllegalAccessException {
+
+        PortalController testPortalController = new PortalController();
+        setAccessPrivateFields(testPortalController);
+        Portal testPortal = fillDomain();
+        List<Portal> portalList = new ArrayList<>();
+        portalList.add(testPortal);
+        when(portalRepository.findAll()).thenReturn(portalList);
+        assertEquals(1, testPortalController.getPortals().size());
+
+    }
+
+
+    public void setAccessPrivateFields(PortalController portalController) throws NoSuchFieldException, IllegalAccessException {
+
+        Field testPortalService = portalController.getClass().getDeclaredField("portalService");
+        testPortalService.setAccessible(true);
+        testPortalService.set(portalController, portalService);
+        Field testPortalRepository = portalController.getClass().getDeclaredField("portalRepository");
+        testPortalRepository.setAccessible(true);
+        testPortalRepository.set(portalController, portalRepository);
+    }
+
+
+    public Portal fillDomain(){
+        Portal portal = new Portal();
+        portal.setName("Kibana");
+        portal.setEnabled(true);
+        portal.setHost("127.0.0.1");
+        portal.setPort(5601);
+        portal.setLogin("admin");
+        portal.setPass("password");
+        portal.setDb(new Db("Elasticsearch"));
+        return  portal;
+    }
+}
\ No newline at end of file
diff --git a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/PortalDesignControllerTest.java b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/PortalDesignControllerTest.java
new file mode 100644 (file)
index 0000000..ac9a17a
--- /dev/null
@@ -0,0 +1,176 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP : DATALAKE
+ * ================================================================================
+ * Copyright 2019 China Mobile
+ *=================================================================================
+ * 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.config.ApplicationConfiguration;
+import org.onap.datalake.feeder.controller.domain.PostReturnBody;
+import org.onap.datalake.feeder.domain.DesignType;
+import org.onap.datalake.feeder.domain.Portal;
+import org.onap.datalake.feeder.domain.PortalDesign;
+import org.onap.datalake.feeder.domain.Topic;
+import org.onap.datalake.feeder.dto.PortalDesignConfig;
+import org.onap.datalake.feeder.repository.DesignTypeRepository;
+import org.onap.datalake.feeder.repository.PortalDesignRepository;
+import org.onap.datalake.feeder.service.PortalDesignService;
+import org.onap.datalake.feeder.service.TopicService;
+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.List;
+import java.util.Optional;
+
+import static org.junit.Assert.*;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public class PortalDesignControllerTest {
+
+    @Mock
+    private HttpServletResponse httpServletResponse;
+
+    @Mock
+    private BindingResult mockBindingResult;
+
+    @Mock
+    private ApplicationConfiguration applicationConfiguration;
+
+    @Mock
+    private PortalDesignRepository portalDesignRepository;
+
+    @Mock
+    private TopicService topicService;
+
+    @Mock
+    private DesignTypeRepository designTypeRepository;
+
+    @InjectMocks
+    private PortalDesignService portalDesignService;
+
+
+    @Before
+    public void setupTest() {
+        MockitoAnnotations.initMocks(this);
+        when(mockBindingResult.hasErrors()).thenReturn(false);
+    }
+
+    @Test
+    public void testCreatePortalDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
+
+        PortalDesignController testPortalDesignController = new PortalDesignController();
+        setAccessPrivateFields(testPortalDesignController);
+        PortalDesign testPortalDesign = fillDomain();
+        when(topicService.getTopic("unauthenticated.SEC_FAULT_OUTPUT")).thenReturn(new Topic("unauthenticated.SEC_FAULT_OUTPUT"));
+        when(designTypeRepository.findById("Kibana Dashboard")).thenReturn(Optional.of(testPortalDesign.getDesignType()));
+        PostReturnBody<PortalDesignConfig> postPortal = testPortalDesignController.createPortalDesign(testPortalDesign.getPortalDesignConfig(), mockBindingResult, httpServletResponse);
+        assertEquals(postPortal.getStatusCode(), 200);
+    }
+
+    @Test
+    public void testUpdatePortalDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
+
+        PortalDesignController testPortalDesignController = new PortalDesignController();
+        setAccessPrivateFields(testPortalDesignController);
+        PortalDesign testPortalDesign = fillDomain();
+        Integer id = 1;
+        when(portalDesignRepository.findById(id)).thenReturn((Optional.of(testPortalDesign)));
+        when(topicService.getTopic("unauthenticated.SEC_FAULT_OUTPUT")).thenReturn(new Topic("unauthenticated.SEC_FAULT_OUTPUT"));
+        when(designTypeRepository.findById("Kibana Dashboard")).thenReturn(Optional.of(testPortalDesign.getDesignType()));
+        PostReturnBody<PortalDesignConfig> postPortal = testPortalDesignController.updatePortalDesign(testPortalDesign.getPortalDesignConfig(), mockBindingResult, id, httpServletResponse);
+        assertEquals(postPortal.getStatusCode(), 200);
+    }
+
+    @Test
+    public void testDeletePortalDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
+
+        PortalDesignController testPortalDesignController = new PortalDesignController();
+        setAccessPrivateFields(testPortalDesignController);
+        PortalDesign testPortalDesign = fillDomain();
+        Integer id = 1;
+        testPortalDesign.setId(1);
+        when(portalDesignRepository.findById(id)).thenReturn((Optional.of(testPortalDesign)));
+        testPortalDesignController.deletePortalDesign(id, httpServletResponse);
+    }
+
+    @Test
+    public void testQueryAllPortalDesign() throws NoSuchFieldException, IllegalAccessException {
+
+        PortalDesignController testPortalDesignController = new PortalDesignController();
+        setAccessPrivateFields(testPortalDesignController);
+        PortalDesign testPortalDesign = fillDomain();
+        List<PortalDesign> portalDesignList = new ArrayList<>();
+        portalDesignList.add(testPortalDesign);
+        when(portalDesignRepository.findAll()).thenReturn(portalDesignList);
+        assertEquals(1, testPortalDesignController.queryAllPortalDesign().size());
+    }
+
+    @Test
+    public void testDeployPortalDesign() throws NoSuchFieldException, IllegalAccessException, IOException {
+
+        PortalDesignController testPortalDesignController = new PortalDesignController();
+        setAccessPrivateFields(testPortalDesignController);
+        PortalDesign testPortalDesign = fillDomain();
+        Integer id = 1;
+        testPortalDesign.setId(1);
+        when(portalDesignRepository.findById(id)).thenReturn((Optional.of(testPortalDesign)));
+        when(applicationConfiguration.getKibanaDashboardImportApi()).thenReturn("/api/kibana/dashboards/import?exclude=index-pattern");
+        testPortalDesignController.deployPortalDesign(id, httpServletResponse);
+    }
+
+    public void setAccessPrivateFields(PortalDesignController portalDesignController) throws NoSuchFieldException, IllegalAccessException {
+
+        Field testPortalDesignService = portalDesignController.getClass().getDeclaredField("portalDesignService");
+        testPortalDesignService.setAccessible(true);
+        testPortalDesignService.set(portalDesignController, portalDesignService);
+        Field testPortalDesignRepository = portalDesignController.getClass().getDeclaredField("portalDesignRepository");
+        testPortalDesignRepository.setAccessible(true);
+        testPortalDesignRepository.set(portalDesignController, portalDesignRepository);
+    }
+
+
+    public PortalDesign fillDomain(){
+        PortalDesign portalDesign = new PortalDesign();
+        portalDesign.setName("Kibana");
+        portalDesign.setBody("jsonString");
+        portalDesign.setSubmitted(false);
+        portalDesign.setNote("test");
+        DesignType designType = new DesignType();
+        designType.setName("Kibana Dashboard");
+        Portal portal = new Portal();
+        portal.setName("Kibana");
+        portal.setHost("127.0.0.1");
+        portal.setPort(5601);
+        designType.setPortal(portal);
+        portalDesign.setDesignType(designType);
+        portalDesign.setTopic(new Topic("unauthenticated.SEC_FAULT_OUTPUT"));
+        return  portalDesign;
+    }
+}
\ No newline at end of file