From eec769084e13607c1e5011d1a1de3a3f20be4e9a Mon Sep 17 00:00:00 2001 From: deepikasatheesh Date: Thu, 22 Sep 2022 13:48:48 +0000 Subject: CodeCoverage improvement for dcaegen2-service-datalake-handler Issue-ID: DCAEGEN2-3161 Change-Id: Ice517896647cd7b90c6c1014a09547e3ad712aee Signed-off-by: deepikasatheesh --- components/datalake-handler/Changelog.md | 4 + components/datalake-handler/admin/pom.xml | 3 +- components/datalake-handler/admin/src/package.json | 2 +- components/datalake-handler/collector/pom.xml | 20 - components/datalake-handler/des/pom.xml | 579 +++++++++---------- .../des/controller/DataExposureControllerTest.java | 162 +++++- .../java/org/onap/datalake/des/domain/DbTest.java | 4 + .../org/onap/datalake/des/domain/DbTypeTest.java | 11 +- .../org/onap/datalake/des/dto/DbConfigTest.java | 3 + .../des/service/DataExposureServiceTest.java | 89 ++- components/datalake-handler/feeder/pom.xml | 612 +++++++++++---------- .../onap/datalake/feeder/service/KafkaService.java | 16 +- .../feeder/controller/DbControllerTest.java | 148 ++++- .../feeder/controller/DesignControllerTest.java | 117 +++- .../controller/DesignTypeControllerTest.java | 50 +- .../feeder/controller/KafkaControllerTest.java | 75 ++- .../feeder/controller/TopicControllerTest.java | 267 +++++---- .../feeder/controller/TopicNameControllerTest.java | 67 +++ .../onap/datalake/feeder/domain/DbTypeTest.java | 22 +- .../onap/datalake/feeder/domain/DesignTest.java | 8 +- .../datalake/feeder/domain/DesignTypeTest.java | 6 +- .../onap/datalake/feeder/dto/DesignConfigTest.java | 7 +- .../datalake/feeder/dto/DesignTypeConfigTest.java | 37 ++ .../onap/datalake/feeder/dto/KafkaConfigTest.java | 7 +- .../datalake/feeder/service/DesignServiceTest.java | 127 ++++- .../feeder/service/DesignTypeServiceTest.java | 22 +- .../datalake/feeder/service/KafkaServiceTest.java | 8 +- .../datalake/feeder/service/PullServiceTest.java | 105 ++-- .../service/TopicConfigPollingServiceTest.java | 118 ++-- .../feeder/service/TopicNameServiceTest.java | 55 ++ .../datalake/feeder/service/TopicServiceTest.java | 389 +++++++------ .../feeder/service/db/CouchbaseServiceTest.java | 241 ++++---- components/datalake-handler/pom.xml | 539 +++++++++--------- components/datalake-handler/version.properties | 2 +- 34 files changed, 2413 insertions(+), 1509 deletions(-) delete mode 100644 components/datalake-handler/collector/pom.xml create mode 100644 components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/TopicNameControllerTest.java create mode 100644 components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/dto/DesignTypeConfigTest.java create mode 100644 components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/TopicNameServiceTest.java mode change 100755 => 100644 components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/db/CouchbaseServiceTest.java (limited to 'components') diff --git a/components/datalake-handler/Changelog.md b/components/datalake-handler/Changelog.md index b020ac5f..247d41e1 100644 --- a/components/datalake-handler/Changelog.md +++ b/components/datalake-handler/Changelog.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [1.1.2] +### Changed +* CodeCoverage improvement for dcaegen2-services-data-handler (DCAEGEN2-3161) + ## [1.1.1] 2022-09-14 * DCAEGEN2-3004 - Fix DL-Admin Docker docker build issue diff --git a/components/datalake-handler/admin/pom.xml b/components/datalake-handler/admin/pom.xml index 62b574f2..305579d5 100644 --- a/components/datalake-handler/admin/pom.xml +++ b/components/datalake-handler/admin/pom.xml @@ -3,6 +3,7 @@ ============LICENSE_START======================================================= Copyright (c) 2019 QCT. All rights reserved. Copyright (c) 2022 AT&T. All rights reserved. + Copyright (C) 2022 Wipro Limited. 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 @@ -21,7 +22,7 @@ org.onap.dcaegen2.services.components datalake-handler - 1.1.1-SNAPSHOT + 1.1.2-SNAPSHOT org.onap.dcaegen2.services.components.datalake-handler diff --git a/components/datalake-handler/admin/src/package.json b/components/datalake-handler/admin/src/package.json index a3803f37..30b9d88c 100644 --- a/components/datalake-handler/admin/src/package.json +++ b/components/datalake-handler/admin/src/package.json @@ -1,6 +1,6 @@ { "name": "DataLake-AdminUI", - "version": "1.1.1", + "version": "1.1.2", "scripts": { "ng": "ng", "start": "ng serve --host='0.0.0.0' --proxy-config proxy.conf.json", diff --git a/components/datalake-handler/collector/pom.xml b/components/datalake-handler/collector/pom.xml deleted file mode 100644 index a3eeff15..00000000 --- a/components/datalake-handler/collector/pom.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - 4.0.0 - - - org.onap.dcaegen2.services.components - datalake-handler - 1.1.1-SNAPSHOT - - - org.onap.dcaegen2.services.components.datalake-handler - collector - pom - DataLake Collector - - - - diff --git a/components/datalake-handler/des/pom.xml b/components/datalake-handler/des/pom.xml index 87ae9443..64d382f1 100644 --- a/components/datalake-handler/des/pom.xml +++ b/components/datalake-handler/des/pom.xml @@ -2,7 +2,8 @@ - +--> - 4.0.0 - - - org.onap.dcaegen2.services.components - datalake-handler - 1.1.1-SNAPSHOT - - - des - jar - DataLake Extraction Service - - - 2.9.2 - 1.4.10 - onap/org.onap.dcaegen2.services.datalake.exposure.service - yyyyMMdd'T'HHmmss - - - - - - org.jdom - jdom2 - 2.0.6 - - - - com.facebook.presto - presto-jdbc - 0.240 - - - - org.apache.hadoop - hadoop-client - ${hadoop.version} - - - - org.postgresql - postgresql - 42.2.18 - - - - org.json - json - 20190722 - - - - org.apache.httpcomponents - httpclient - 4.5.10 - - - - org.apache.kafka - kafka-clients - 2.3.1 - - - + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + + org.onap.dcaegen2.services.components + datalake-handler + 1.1.2-SNAPSHOT + + des + jar + DataLake Extraction Service + + 2.9.2 + 1.4.10 + onap/org.onap.dcaegen2.services.datalake.exposure.service + yyyyMMdd'T'HHmmss + -changelog-missing + + + + org.jdom + jdom2 + 2.0.6 + + + com.facebook.presto + presto-jdbc + 0.240 + + + org.apache.hadoop + hadoop-client + ${hadoop.version} + + + org.postgresql + postgresql + 42.2.18 + + + org.json + json + 20190722 + + + org.apache.httpcomponents + httpclient + 4.5.10 + + + org.apache.kafka + kafka-clients + 2.3.1 + + + org.springframework.boot + spring-boot-starter-web + ${springboot.version} + + + org.springframework.boot + spring-boot-starter-actuator + ${springboot.version} + + + org.springframework.boot + spring-boot-starter-data-jpa + ${springboot.version} + + + org.springframework.boot + spring-boot-starter-data-couchbase + ${springboot.version} + + + org.springframework.boot + spring-boot-starter-test + ${springboot.version} + test + + + org.springframework.boot + spring-boot-configuration-processor + ${springboot.version} + + + org.elasticsearch.client + elasticsearch-rest-high-level-client + ${elasticsearchjava.version} + + + commons-io + commons-io + 2.6 + + + com.fasterxml.jackson.dataformat + jackson-dataformat-yaml + ${jackson.version} + + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + ${jackson.version} + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + com.google.code.gson + gson + 2.8.2 + + + org.projectlombok + lombok + 1.18.10 + provided + + + io.druid + tranquility-core_2.11 + 0.8.3 + + + org.apache.velocity + velocity-engine-core + 2.1 + + + org.hibernate + hibernate-core + 5.3.7.Final + + + + javax.validation + validation-api + 2.0.1.Final + + + org.hibernate + hibernate-validator + 6.1.0.Final + + + io.springfox + springfox-swagger2 + ${swagger.version} + compile + + + io.springfox + springfox-swagger-ui + ${swagger.version} + compile + + + org.mongodb + mongo-java-driver + ${mongojava.version} + + + com.couchbase.mock + CouchbaseMock + 1.5.22 + test + + + + + + org.apache.maven.plugins + maven-deploy-plugin + 2.8 + + true + + + org.springframework.boot - spring-boot-starter-web - ${springboot.version} - - - - org.springframework.boot - spring-boot-starter-actuator - ${springboot.version} - - - - org.springframework.boot - spring-boot-starter-data-jpa - ${springboot.version} - - - - org.springframework.boot - spring-boot-starter-data-couchbase - ${springboot.version} - - - - org.springframework.boot - spring-boot-starter-test - ${springboot.version} - test - - - - org.springframework.boot - spring-boot-configuration-processor - ${springboot.version} - - - - org.elasticsearch.client - elasticsearch-rest-high-level-client - ${elasticsearchjava.version} - - - - commons-io - commons-io - 2.6 - - - - com.fasterxml.jackson.dataformat - jackson-dataformat-yaml - ${jackson.version} - - - - com.fasterxml.jackson.dataformat - jackson-dataformat-xml - ${jackson.version} - - - - com.fasterxml.jackson.core - jackson-databind - ${jackson.version} - - - - com.google.code.gson - gson - 2.8.2 - - - - org.projectlombok - lombok - 1.18.10 - provided - - - - io.druid - tranquility-core_2.11 - 0.8.3 - - - - org.apache.velocity - velocity-engine-core - 2.1 - - - - - org.hibernate - hibernate-core - 5.3.7.Final - - - - - javax.validation - validation-api - 2.0.1.Final - - - - org.hibernate - hibernate-validator - 6.1.0.Final - - - - io.springfox - springfox-swagger2 - ${swagger.version} - compile - - - - io.springfox - springfox-swagger-ui - ${swagger.version} - compile - - - - org.mongodb - mongo-java-driver - ${mongojava.version} - - - - com.couchbase.mock - CouchbaseMock - 1.5.22 - test - - - - - - - - - org.apache.maven.plugins - maven-deploy-plugin - 2.8 - - true - - - - - org.springframework.boot - spring-boot-maven-plugin - - execute - - - - repackage - - - - - - - com.spotify - dockerfile-maven-plugin - ${dockerfile-maven.version} - - ${onap.nexus.dockerregistry.daily}/${docker.image.path} - ${project.version} - Dockerfile - - ${project.build.finalName}.jar - - - - - build-sl-des-image - package - - build - - - true - - - - tag-and-push-image-latest - package - - tag - push - - - ${onap.nexus.dockerregistry.daily}/${docker.image.path} - latest - true - - - - tag-and-push-image-with-version - package - - tag - push - - - ${onap.nexus.dockerregistry.daily}/${docker.image.path} - ${project.version} - true - - - - tag-and-push-image-with-version-and-date - package - - tag - push - - - ${onap.nexus.dockerregistry.daily}/${docker.image.path} - ${project.version}-${maven.build.timestamp}Z - true - - - - - - - javax.activation - javax.activation-api - 1.2.0 - - - - - + spring-boot-maven-plugin + + execute + + + + repackage + + + + + + + com.spotify + dockerfile-maven-plugin + ${dockerfile-maven.version} + + ${onap.nexus.dockerregistry.daily}/${docker.image.path} + ${project.version} + Dockerfile + + ${project.build.finalName}.jar + + + + + build-sl-des-image + package + + build + + + true + + + + tag-and-push-image-latest + package + + tag + push + + + ${onap.nexus.dockerregistry.daily}/${docker.image.path} + latest + true + + + + tag-and-push-image-with-version + package + + tag + push + + + ${onap.nexus.dockerregistry.daily}/${docker.image.path} + ${project.version} + true + + + + tag-and-push-image-with-version-and-date + package + + tag + push + + + ${onap.nexus.dockerregistry.daily}/${docker.image.path} + ${project.version}-${maven.build.timestamp}Z + true + + + + + + + javax.activation + javax.activation-api + 1.2.0 + + + + + diff --git a/components/datalake-handler/des/src/test/java/org/onap/datalake/des/controller/DataExposureControllerTest.java b/components/datalake-handler/des/src/test/java/org/onap/datalake/des/controller/DataExposureControllerTest.java index 4cadc31d..c4abac98 100644 --- a/components/datalake-handler/des/src/test/java/org/onap/datalake/des/controller/DataExposureControllerTest.java +++ b/components/datalake-handler/des/src/test/java/org/onap/datalake/des/controller/DataExposureControllerTest.java @@ -3,6 +3,7 @@ * ONAP : DATALAKE DES * ================================================================================ * Copyright (C) 2020 China Mobile. All rights reserved. + * Copyright (C) 2022 Wipro Limited. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,20 +22,28 @@ package org.onap.datalake.des.controller; import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.when; import java.io.IOException; import java.sql.SQLException; +import java.util.ArrayList; import java.util.HashMap; +import java.util.List; import java.util.Map; - import javax.servlet.http.HttpServletResponse; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; +import org.onap.datalake.des.domain.DataExposure; +import org.onap.datalake.des.domain.Db; +import org.onap.datalake.des.domain.DbType; +import org.onap.datalake.des.dto.DataExposureConfig; import org.onap.datalake.des.repository.DataExposureRepository; +import org.onap.datalake.des.service.DataExposureService; import org.springframework.validation.BindingResult; /** @@ -54,16 +63,157 @@ public class DataExposureControllerTest { @Mock private BindingResult mockBindingResult; + @Mock + private DataExposureService dataExposureService; + + @InjectMocks + private DataExposureController dataExposureController; + + /** + * Generate data exposure config. + * + * @return DataExposureConfig object + * + */ + public DataExposureConfig getDataExposureConfig() { + DataExposureConfig dataExposureConfig = new DataExposureConfig(); + dataExposureConfig.setDbId(1); + dataExposureConfig.setId("1"); + dataExposureConfig.setNote("note"); + dataExposureConfig.setSqlTemplate("sqlTemplate"); + return dataExposureConfig; + } + + /** + * Generate data exposure. + * + * @return DataExposure object + * + */ + public DataExposure getDataExposure() { + DbType dbType = new DbType("ES", "Elasticsearch"); + Db db = new Db(); + db.setId(1); + db.setDbType(dbType); + db.setDatabase("Elasticsearch"); + + DataExposure dataExposure = new DataExposure(); + dataExposure.setId("1"); + dataExposure.setNote("note"); + dataExposure.setSqlTemplate("sqlTemplate"); + dataExposure.setDb(db); + return dataExposure; + } + @Test(expected = NullPointerException.class) - public void testServe() - throws IOException, NoSuchFieldException, IllegalAccessException, ClassNotFoundException, SQLException { + public void testServeNull() + throws IOException, NoSuchFieldException, IllegalAccessException, ClassNotFoundException, SQLException { DataExposureController dataExposureController = new DataExposureController(); String serviceId = "test"; - Map requestMap = new HashMap(); + Map < String, String > requestMap = new HashMap < String, String > (); requestMap.put("name", "oteNB5309"); - HashMap result = dataExposureController.serve(serviceId, requestMap, mockBindingResult, - httpServletResponse); + HashMap < String, Object > result = dataExposureController.serve(serviceId, requestMap, mockBindingResult, + httpServletResponse); assertEquals(null, result); when(mockBindingResult.hasErrors()).thenReturn(true); } + + @Test(expected = SQLException.class) + public void testServeException() + throws IOException, NoSuchFieldException, IllegalAccessException, ClassNotFoundException, SQLException { + String serviceId = "test"; + Map < String, String > requestMap = new HashMap < String, String > (); + requestMap.put("name", "oteNB5309"); + + DataExposure dataExposure = getDataExposure(); + when(dataExposureService.getDataExposure(serviceId)).thenReturn(dataExposure); + dataExposureController.serve(serviceId, requestMap, mockBindingResult, + httpServletResponse); + } + + @Test + public void testQueryAllDataExposure() { + DataExposureConfig dataExposureConfig = getDataExposureConfig(); + List < DataExposureConfig > dataExposureList = new ArrayList < > (); + dataExposureList.add(dataExposureConfig); + when(dataExposureService.queryAllDataExposure()).thenReturn(dataExposureList); + assertEquals(dataExposureList, dataExposureController.queryAllDataExposure()); + } + + @Test + public void TestQueryAllDataExposureByIdNull() throws IOException { + when(dataExposureService.getDataExposureById("1")).thenReturn(null); + assertEquals(null, dataExposureController.queryAllDataExposure("1", httpServletResponse)); + } + + @Test + public void TestQueryAllDataExposureById() throws IOException { + DataExposure dataExposure = getDataExposure(); + when(dataExposureService.getDataExposureById("1")).thenReturn(dataExposure); + dataExposureController.queryAllDataExposure("1", httpServletResponse); + } + + @Test + public void testCreateDataExposureNull() throws IOException { + DataExposure dataExposure = getDataExposure(); + DataExposureConfig dataExposureConfig = getDataExposureConfig(); + when(dataExposureService.getDataExposureById("1")).thenReturn(dataExposure); + assertEquals(null, dataExposureController.createDataExposure(dataExposureConfig, mockBindingResult, httpServletResponse)); + } + + @Test + public void testCreateDataExposure() throws IOException { + DataExposure dataExposure = getDataExposure(); + DataExposureConfig dataExposureConfig = getDataExposureConfig(); + when(dataExposureService.getDataExposureById("1")).thenReturn(null); + when(dataExposureService.fillDataExposureConfiguration(dataExposureConfig)).thenReturn(dataExposure); + dataExposureController.createDataExposure(dataExposureConfig, mockBindingResult, httpServletResponse); + } + + @Test + public void testCreateDataExposureException() throws IOException { + DataExposureConfig dataExposureConfig = getDataExposureConfig(); + when(dataExposureService.getDataExposureById("1")).thenReturn(null); + when(dataExposureService.fillDataExposureConfiguration(dataExposureConfig)).thenThrow(NullPointerException.class); + assertEquals(null, dataExposureController.createDataExposure(dataExposureConfig, mockBindingResult, httpServletResponse)); + } + + @Test + public void testCreateDataExposureError() throws IOException { + DataExposureConfig dataExposureConfig = getDataExposureConfig(); + when(mockBindingResult.hasErrors()).thenReturn(true); + assertEquals(null, dataExposureController.createDataExposure(dataExposureConfig, mockBindingResult, httpServletResponse)); + } + + @Test + public void testUpdateDataExposureNull() throws IOException { + DataExposureConfig dataExposureConfig = getDataExposureConfig(); + when(dataExposureService.getDataExposureById("1")).thenReturn(null); + assertEquals(null, dataExposureController.updateDataExposure(dataExposureConfig, mockBindingResult, "1", httpServletResponse)); + } + + @Test + public void testUpdateDataExposure() throws IOException { + DataExposure dataExposure = getDataExposure(); + DataExposureConfig dataExposureConfig = getDataExposureConfig(); + when(dataExposureService.getDataExposureById("1")).thenReturn(dataExposure); + dataExposureController.updateDataExposure(dataExposureConfig, mockBindingResult, "1", httpServletResponse); + } + + @Test + public void testUpdateDataExposureException() throws IOException { + DataExposure dataExposure = getDataExposure(); + DataExposureConfig dataExposureConfig = getDataExposureConfig(); + when(dataExposureService.getDataExposureById("1")).thenReturn(dataExposure); + doThrow(NullPointerException.class).when(dataExposureService).fillDataExposureConfiguration(dataExposureConfig, dataExposure); + assertEquals(null, dataExposureController.updateDataExposure(dataExposureConfig, mockBindingResult, "1", httpServletResponse)); + } + + @Test + public void testUpdateDataExposureError() throws IOException { + DataExposureConfig dataExposureConfig = getDataExposureConfig(); + when(mockBindingResult.hasErrors()).thenReturn(true); + assertEquals(null, dataExposureController.updateDataExposure(dataExposureConfig, mockBindingResult, "1", httpServletResponse)); + } + } diff --git a/components/datalake-handler/des/src/test/java/org/onap/datalake/des/domain/DbTest.java b/components/datalake-handler/des/src/test/java/org/onap/datalake/des/domain/DbTest.java index cd5c6b4d..e6dba7f0 100644 --- a/components/datalake-handler/des/src/test/java/org/onap/datalake/des/domain/DbTest.java +++ b/components/datalake-handler/des/src/test/java/org/onap/datalake/des/domain/DbTest.java @@ -3,6 +3,7 @@ * ONAP : DataLake DES * ================================================================================ * Copyright 2020 China Mobile. All rights reserved. + * Copyright (C) 2022 Wipro Limited. *================================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -23,6 +24,7 @@ package org.onap.datalake.des.domain; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import org.junit.Test; @@ -74,5 +76,7 @@ public class DbTest { assertTrue("property2".equals(mongoDb2.getProperty2())); assertTrue("property3".equals(mongoDb2.getProperty3())); assertEquals(mongoDb2.getDbConfig().getHost(), mongoDb2.getHost()); + assertNotNull(mongoDb1.toString()); } + } diff --git a/components/datalake-handler/des/src/test/java/org/onap/datalake/des/domain/DbTypeTest.java b/components/datalake-handler/des/src/test/java/org/onap/datalake/des/domain/DbTypeTest.java index 4b83a03b..9f727c5c 100644 --- a/components/datalake-handler/des/src/test/java/org/onap/datalake/des/domain/DbTypeTest.java +++ b/components/datalake-handler/des/src/test/java/org/onap/datalake/des/domain/DbTypeTest.java @@ -3,6 +3,7 @@ * ONAP : DataLake DES * ================================================================================ * Copyright 2020 China Mobile. All rights reserved. + * Copyright (C) 2022 Wipro Limited. *================================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -37,16 +38,18 @@ public class DbTypeTest { @Test public void test() { - DbType dbType = new DbType("ES","Elasticsearch"); + DbType dbType = new DbType("ES", "Elasticsearch"); - dbType.setTool(false); + dbType.setTool(false); assertNotNull(dbType.toString()); assertEquals(dbType, dbType); assertNotEquals(dbType, null); assertNotEquals(dbType, "ES"); - DbType dbType2 = new DbType("MONGO", "MongoDB"); + DbType dbType2 = new DbType(); + dbType2.setId("MONGO"); + dbType2.setName("MongoDB"); assertNotEquals(dbType, dbType2); assertNotNull(dbType.hashCode()); @@ -59,4 +62,4 @@ public class DbTypeTest { assertNull(dbType2.getDbs()); } -} \ No newline at end of file +} diff --git a/components/datalake-handler/des/src/test/java/org/onap/datalake/des/dto/DbConfigTest.java b/components/datalake-handler/des/src/test/java/org/onap/datalake/des/dto/DbConfigTest.java index ee21f475..09f868f8 100644 --- a/components/datalake-handler/des/src/test/java/org/onap/datalake/des/dto/DbConfigTest.java +++ b/components/datalake-handler/des/src/test/java/org/onap/datalake/des/dto/DbConfigTest.java @@ -3,6 +3,7 @@ * ONAP : DATALAKE DES * ================================================================================ * Copyright (C) 2020 China Mobile. All rights reserved. + * Copyright (C) 2022 Wipro Limited. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -53,5 +54,7 @@ public class DbConfigTest { assertFalse("123".equals(dbConfig.getPort())); dbConfig.setPoperties("driver"); assertTrue("driver".equals(dbConfig.getPoperties())); + dbConfig.setDbTypeId("123"); + assertTrue("123".equals(dbConfig.getDbTypeId())); } } diff --git a/components/datalake-handler/des/src/test/java/org/onap/datalake/des/service/DataExposureServiceTest.java b/components/datalake-handler/des/src/test/java/org/onap/datalake/des/service/DataExposureServiceTest.java index 254afcb5..9671a94b 100644 --- a/components/datalake-handler/des/src/test/java/org/onap/datalake/des/service/DataExposureServiceTest.java +++ b/components/datalake-handler/des/src/test/java/org/onap/datalake/des/service/DataExposureServiceTest.java @@ -3,6 +3,7 @@ * ONAP : DataLake DES * ================================================================================ * Copyright 2020 China Mobile. All rights reserved. + * Copyright (C) 2022 Wipro Limited. *================================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,11 +23,9 @@ package org.onap.datalake.des.service; import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.when; - import java.util.ArrayList; import java.util.List; import java.util.Optional; - import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; @@ -40,7 +39,6 @@ import org.onap.datalake.des.dto.DbConfig; import org.onap.datalake.des.repository.DataExposureRepository; import org.onap.datalake.des.repository.DbRepository; import org.springframework.context.ApplicationContext; - /** * Test DB exposure Service. * @@ -99,6 +97,34 @@ public class DataExposureServiceTest { return dbConfig; } + @Test + public void testGetDataExposure() { + Db newdb = new Db(); + DbConfig dbConfig = getDbConfig(); + newdb.setName(dbConfig.getName()); + newdb.setHost(dbConfig.getHost()); + newdb.setPort(dbConfig.getPort()); + newdb.setEnabled(dbConfig.isEnabled()); + newdb.setLogin(dbConfig.getLogin()); + newdb.setPass(dbConfig.getPass()); + newdb.setEncrypt(dbConfig.isEncrypt()); + + DataExposure de = new DataExposure(); + de.setId("1"); + de.setNote("note"); + de.setSqlTemplate("sqlTemplate"); + de.setDb(newdb); + when(dataExposureRepository.findById("1")).thenReturn(Optional.of(de)); + assertEquals(de, dataExposureService.getDataExposure("1")); + } + + @Test + public void testGetDataExposureNull() { + Optional < DataExposure > de = Optional.ofNullable(null); + when(dataExposureRepository.findById(null)).thenReturn(de); + assertEquals(dataExposureService.getDataExposure(null), null); + } + @Test public void testQueryAllDataExposure() { Db newdb = new Db(); @@ -110,19 +136,55 @@ public class DataExposureServiceTest { newdb.setLogin(dbConfig.getLogin()); newdb.setPass(dbConfig.getPass()); newdb.setEncrypt(dbConfig.isEncrypt()); + DataExposureConfig deConfig = getDataExposureConfig(); DataExposure de = new DataExposure(); de.setDb(newdb); de.setId(deConfig.getId()); de.setNote(deConfig.getNote()); de.setSqlTemplate(deConfig.getSqlTemplate()); - List deList = new ArrayList<>(); + List < DataExposure > deList = new ArrayList < > (); deList.add(de); when(dataExposureRepository.findAll()).thenReturn(deList); - List deConfigList = dataExposureService.queryAllDataExposure(); + List < DataExposureConfig > deConfigList = dataExposureService.queryAllDataExposure(); assertEquals(de.getId(), deConfigList.get(0).getId()); } + @Test + public void testQueryAllDataExposureNull() { + List < DataExposure > deList = new ArrayList < > (); + when(dataExposureRepository.findAll()).thenReturn(deList); + assertEquals(dataExposureService.queryAllDataExposure(), deList); + } + + @Test + public void testGetDataExposureById() { + Db newdb = new Db(); + DbConfig dbConfig = getDbConfig(); + newdb.setName(dbConfig.getName()); + newdb.setHost(dbConfig.getHost()); + newdb.setPort(dbConfig.getPort()); + newdb.setEnabled(dbConfig.isEnabled()); + newdb.setLogin(dbConfig.getLogin()); + newdb.setPass(dbConfig.getPass()); + newdb.setEncrypt(dbConfig.isEncrypt()); + + DataExposure de = new DataExposure(); + de.setId("1"); + de.setNote("note"); + de.setSqlTemplate("sqlTemplate"); + de.setDb(newdb); + when(dataExposureRepository.findById("1")).thenReturn(Optional.of(de)); + assertEquals(de, dataExposureService.getDataExposureById("1")); + } + + @Test + public void testGetDataExposureByIdNull() { + Optional < DataExposure > de = Optional.ofNullable(null); + when(dataExposureRepository.findById(null)).thenReturn(de); + assertEquals(dataExposureService.getDataExposureById(null), null); + } + @Test public void testFillDataExposureConfiguration() { Db newdb = new Db(); @@ -153,6 +215,7 @@ public class DataExposureServiceTest { newdb.setEncrypt(dbConfig.isEncrypt()); DataExposureConfig deConfig = getDataExposureConfig(); when(dbRepository.findById(deConfig.getDbId())).thenReturn(Optional.of(newdb)); + DataExposure de = new DataExposure(); de.setDb(newdb); de.setId(deConfig.getId()); @@ -161,4 +224,20 @@ public class DataExposureServiceTest { dataExposureService.fillDataExposureConfiguration(deConfig, de); } + @Test(expected = IllegalArgumentException.class) + public void testFillDataExposureException() { + DataExposureConfig deConfig = getDataExposureConfig(); + deConfig.setDbId(null); + dataExposureService.fillDataExposureConfiguration(deConfig); + } + + @Test(expected = IllegalArgumentException.class) + public void testFillDataExposureIllegalArgumentException() { + DataExposureConfig deConfig = getDataExposureConfig(); + deConfig.setDbId(1); + Optional < Db > dbOptional = Optional.ofNullable(null); + when(dbRepository.findById(deConfig.getDbId())).thenReturn(dbOptional); + dataExposureService.fillDataExposureConfiguration(deConfig); + } + } diff --git a/components/datalake-handler/feeder/pom.xml b/components/datalake-handler/feeder/pom.xml index 49ad6094..312e8dcc 100644 --- a/components/datalake-handler/feeder/pom.xml +++ b/components/datalake-handler/feeder/pom.xml @@ -1,299 +1,319 @@ - - 4.0.0 - - - org.onap.dcaegen2.services.components - datalake-handler - 1.1.1-SNAPSHOT - - - org.onap.dcaegen2.services.components.datalake-handler - feeder - jar - DataLake Feeder - - - 2.9.2 - 1.4.5 - onap/org.onap.dcaegen2.services.datalakefeeder - yyyyMMdd'T'HHmmss - - - - - - - org.jdom - jdom2 - 2.0.6 - - - - com.facebook.presto - presto-jdbc - 0.229 - - - - org.apache.hadoop - hadoop-client - ${hadoop.version} - - - - org.postgresql - postgresql - 42.2.18 + + + 4.0.0 + + + org.onap.dcaegen2.services.components + datalake-handler + 1.1.2-SNAPSHOT + + + org.onap.dcaegen2.services.components.datalake-handler + feeder + jar + DataLake Feeder + + + 2.9.2 + 1.4.5 + onap/org.onap.dcaegen2.services.datalakefeeder + yyyyMMdd'T'HHmmss + -changelog-missing + + + + + + org.jdom + jdom2 + 2.0.6 + + + + com.facebook.presto + presto-jdbc + 0.229 + + + + org.apache.hadoop + hadoop-client + ${hadoop.version} + + + + org.postgresql + postgresql + 42.2.18 + + + + org.json + json + 20190722 + + + + org.apache.httpcomponents + httpclient + 4.5.10 + + + + org.apache.kafka + kafka-clients + 2.3.1 + + + + org.springframework.boot + spring-boot-starter-web + ${springboot.version} + + + + org.springframework.boot + spring-boot-starter-actuator + ${springboot.version} + + + + org.springframework.boot + spring-boot-starter-data-jpa + ${springboot.version} + + + + org.springframework.boot + spring-boot-starter-data-couchbase + ${springboot.version} + + + + org.springframework.boot + spring-boot-starter-test + ${springboot.version} + test + + + + org.springframework.boot + spring-boot-configuration-processor + ${springboot.version} + + + + org.elasticsearch.client + elasticsearch-rest-high-level-client + ${elasticsearchjava.version} + + + + commons-io + commons-io + 2.6 + + + + com.fasterxml.jackson.dataformat + jackson-dataformat-yaml + ${jackson.version} + + + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + ${jackson.version} + + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + + com.google.code.gson + gson + 2.8.2 + + + + org.projectlombok + lombok + 1.18.10 + provided + + + + io.druid + tranquility-core_2.11 + 0.8.3 + + + + org.apache.velocity + velocity-engine-core + 2.1 + + + + + org.hibernate + hibernate-core + 5.3.7.Final + + + + + javax.validation + validation-api + 2.0.1.Final + + + + org.hibernate + hibernate-validator + 6.1.0.Final + + + + io.springfox + springfox-swagger2 + ${swagger.version} + compile + + + + io.springfox + springfox-swagger-ui + ${swagger.version} + compile + + + + org.mongodb + mongo-java-driver + ${mongojava.version} + + + com.couchbase.mock + CouchbaseMock + 1.5.22 + test + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + exec + + + + com.spotify + dockerfile-maven-plugin + ${dockerfile-maven.version} + + + + + ${onap.nexus.dockerregistry.daily}/${docker.image.path} + + ${project.version} + Dockerfile + + + ${project.build.finalName}.jar + + + + + build-sl-feeder-image + package + + build + + + true + + + + tag-and-push-image-latest + package + + tag + push + + + ${onap.nexus.dockerregistry.daily}/${docker.image.path} + latest + true + + + + tag-and-push-image-with-version + package + + tag + push + + + ${onap.nexus.dockerregistry.daily}/${docker.image.path} + ${project.version} + true + + + + tag-and-push-image-with-version-and-date + package + + tag + push + + + ${onap.nexus.dockerregistry.daily}/${docker.image.path} + ${project.version}-${maven.build.timestamp}Z + true + + + + + + + javax.activation + javax.activation-api + 1.2.0 - - - org.json - json - 20190722 - - - - org.apache.httpcomponents - httpclient - 4.5.10 - - - - org.apache.kafka - kafka-clients - 2.3.1 - - - - org.springframework.boot - spring-boot-starter-web - ${springboot.version} - - - - org.springframework.boot - spring-boot-starter-actuator - ${springboot.version} - - - - org.springframework.boot - spring-boot-starter-data-jpa - ${springboot.version} - - - - org.springframework.boot - spring-boot-starter-data-couchbase - ${springboot.version} - - - - org.springframework.boot - spring-boot-starter-test - ${springboot.version} - test - - - - org.springframework.boot - spring-boot-configuration-processor - ${springboot.version} - - - - org.elasticsearch.client - elasticsearch-rest-high-level-client - ${elasticsearchjava.version} - - - - commons-io - commons-io - 2.6 - - - - com.fasterxml.jackson.dataformat - jackson-dataformat-yaml - ${jackson.version} - - - - com.fasterxml.jackson.dataformat - jackson-dataformat-xml - ${jackson.version} - - - - com.fasterxml.jackson.core - jackson-databind - ${jackson.version} - - - - com.google.code.gson - gson - 2.8.2 - - - - org.projectlombok - lombok - 1.18.10 - provided - - - - io.druid - tranquility-core_2.11 - 0.8.3 - - - - org.apache.velocity - velocity-engine-core - 2.1 - - - - - org.hibernate - hibernate-core - 5.3.7.Final - - - - - javax.validation - validation-api - 2.0.1.Final - - - - org.hibernate - hibernate-validator - 6.1.0.Final - - - - io.springfox - springfox-swagger2 - ${swagger.version} - compile - - - - io.springfox - springfox-swagger-ui - ${swagger.version} - compile - - - - org.mongodb - mongo-java-driver - ${mongojava.version} - - - com.couchbase.mock - CouchbaseMock - 1.5.22 - test - - - - - - - - org.springframework.boot - spring-boot-maven-plugin - - exec - - - - com.spotify - dockerfile-maven-plugin - ${dockerfile-maven.version} - - - - - ${onap.nexus.dockerregistry.daily}/${docker.image.path} - - ${project.version} - Dockerfile - - - ${project.build.finalName}.jar - - - - - build-sl-feeder-image - package - - build - - - true - - - - tag-and-push-image-latest - package - - tag - push - - - ${onap.nexus.dockerregistry.daily}/${docker.image.path} - latest - true - - - - tag-and-push-image-with-version - package - - tag - push - - - ${onap.nexus.dockerregistry.daily}/${docker.image.path} - ${project.version} - true - - - - tag-and-push-image-with-version-and-date - package - - tag - push - - - ${onap.nexus.dockerregistry.daily}/${docker.image.path} - ${project.version}-${maven.build.timestamp}Z - true - - - - - - - javax.activation - javax.activation-api - 1.2.0 - - - - - + + + + diff --git a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/KafkaService.java b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/KafkaService.java index 2e959fa2..0e617f5e 100644 --- a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/KafkaService.java +++ b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/KafkaService.java @@ -3,6 +3,7 @@ * ONAP : DataLake * ================================================================================ * Copyright 2019 China Mobile + * Copyright (C) 2022 Wipro Limited. *================================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,14 +21,15 @@ package org.onap.datalake.feeder.service; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; import org.onap.datalake.feeder.domain.Kafka; import org.onap.datalake.feeder.dto.KafkaConfig; import org.onap.datalake.feeder.repository.KafkaRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import java.util.*; - /** * Service for kafkas * @@ -41,15 +43,15 @@ public class KafkaService { public Kafka getKafkaById(int id) { - Optional ret = kafkaRepository.findById(id); + Optional < Kafka > ret = kafkaRepository.findById(id); return ret.isPresent() ? ret.get() : null; } - public List getAllKafka() { + public List < KafkaConfig > getAllKafka() { - List kafkaConfigList = new ArrayList<>(); - Iterable kafkaIterable = kafkaRepository.findAll(); - for(Kafka portal : kafkaIterable) { + List < KafkaConfig > kafkaConfigList = new ArrayList < > (); + Iterable < Kafka > kafkaIterable = kafkaRepository.findAll(); + for (Kafka portal: kafkaIterable) { kafkaConfigList.add(portal.getKafkaConfig()); } return kafkaConfigList; diff --git a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/DbControllerTest.java b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/DbControllerTest.java index 9318ee00..889821a6 100644 --- a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/DbControllerTest.java +++ b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/DbControllerTest.java @@ -3,6 +3,7 @@ * ONAP : DATALAKE * ================================================================================ * Copyright (C) 2018-2019 Huawei. All rights reserved. + * Copyright (C) 2022 Wipro Limited. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,9 +30,13 @@ 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.DbType; +import org.onap.datalake.feeder.domain.DesignType; 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.repository.DbTypeRepository; +import org.onap.datalake.feeder.repository.DesignTypeRepository; import org.onap.datalake.feeder.service.DbService; import org.onap.datalake.feeder.util.TestUtil; import org.springframework.validation.BindingResult; @@ -61,12 +66,21 @@ public class DbControllerTest { @Mock private DbRepository dbRepository; + @Mock + private DbTypeRepository dbTypeRepository; + + @Mock + private DesignTypeRepository designTypeRepository; + @Mock private BindingResult mockBindingResult; @InjectMocks private DbService dbService1; - + + @InjectMocks + private DbController dbController; + public DbConfig getDbConfig() { DbConfig dbConfig = new DbConfig(); dbConfig.setId(1); @@ -77,16 +91,16 @@ public class DbControllerTest { dbConfig.setDatabase("Elecsticsearch"); dbConfig.setPort(123); dbConfig.setPoperties("driver"); - dbConfig.setDbTypeId("ES"); + 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); - } + IllegalAccessException { + Field dbRepository1 = dbController.getClass().getDeclaredField("dbRepository"); + dbRepository1.setAccessible(true); + dbRepository1.set(dbController, dbRepository); + } @Before public void setupTest() { @@ -101,7 +115,7 @@ public class DbControllerTest { DbController dbController = new DbController(); DbConfig dbConfig = getDbConfig(); setAccessPrivateFields(dbController); - PostReturnBody db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse); + PostReturnBody < DbConfig > db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse); assertEquals(200, db.getStatusCode()); when(mockBindingResult.hasErrors()).thenReturn(true); db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse); @@ -113,8 +127,8 @@ public class DbControllerTest { DbController dbController = new DbController(); DbConfig dbConfig = getDbConfig(); when(mockBindingResult.hasErrors()).thenReturn(true); - PostReturnBody db = dbController.updateDb(dbConfig.getId(), dbConfig, mockBindingResult, - httpServletResponse); + PostReturnBody < DbConfig > db = dbController.updateDb(dbConfig.getId(), dbConfig, mockBindingResult, + httpServletResponse); assertEquals(null, db); //when(mockBindingResult.hasErrors()).thenReturn(false); setAccessPrivateFields(dbController); @@ -135,12 +149,12 @@ public class DbControllerTest { DbController dbController = new DbController(); String name = "Elecsticsearch"; int testId = 1234; - List dbs = new ArrayList<>(); + List < Db > dbs = new ArrayList < > (); dbs.add(TestUtil.newDb(name)); setAccessPrivateFields(dbController); when(dbRepository.findAll()).thenReturn(dbs); - List list = dbController.list(); - for (int id : list) { + List < Integer > list = dbController.list(); + for (int id: list) { assertNotEquals(1234, id); } //dbController.deleteDb("Elecsticsearch", httpServletResponse); @@ -155,18 +169,18 @@ public class DbControllerTest { Topic topic = TestUtil.newTopic(topicName); topic.setEnabled(true); topic.setId(1); - Set topics = new HashSet<>(); + Set < Topic > topics = new HashSet < > (); topics.add(topic); Db db1 = TestUtil.newDb(dbName); db1.setTopics(topics); setAccessPrivateFields(dbController); - Set elecsticsearch = dbController.getDbTopics(dbName, httpServletResponse); + 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); + for (Topic anElecsticsearch: elecsticsearch) { + Topic tmp = TestUtil.newTopic(topicName); + tmp.setId(2); assertNotEquals(tmp, anElecsticsearch); } //dbController.deleteDb(dbName, httpServletResponse); @@ -177,7 +191,7 @@ public class DbControllerTest { DbController dbController = new DbController(); DbConfig dbConfig = getDbConfig(); setAccessPrivateFields(dbController); - PostReturnBody db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse); + PostReturnBody < DbConfig > db = dbController.createDb(dbConfig, mockBindingResult, httpServletResponse); assertNotNull(db); } @@ -185,8 +199,102 @@ public class DbControllerTest { public void testVerifyConnection() throws IOException { DbController dbController = new DbController(); DbConfig dbConfig = getDbConfig(); - PostReturnBody dbConfigPostReturnBody = dbController.verifyDbConnection(dbConfig, httpServletResponse); + PostReturnBody < DbConfig > dbConfigPostReturnBody = dbController.verifyDbConnection(dbConfig, httpServletResponse); assertEquals(null, dbConfigPostReturnBody); } + @Test + public void testDeleteDbNull() throws IOException { + Optional < Db > dbOptional = Optional.ofNullable(null); + when(dbRepository.findById(1)).thenReturn(dbOptional); + dbController.deleteDb(1, httpServletResponse); + } + + @Test + public void deleteDbTest() throws IOException { + Db db = TestUtil.newDb("Elecsticsearch"); + Topic topic = TestUtil.newTopic("Elecsticsearch"); + topic.setEnabled(true); + topic.setId(1); + Set < Topic > topics = new HashSet < > (); + topics.add(topic); + db.setTopics(topics); + Optional < Db > dbOptional = Optional.ofNullable(db); + when(dbRepository.findById(1)).thenReturn(dbOptional); + dbController.deleteDb(1, httpServletResponse); + } + + @Test + public void testUpdateDbNull() throws IOException { + DbConfig dbConfig = getDbConfig(); + Db db = TestUtil.newDb("Elecsticsearch"); + Optional < Db > dbOptional = Optional.ofNullable(db); + when(dbRepository.findById(dbConfig.getId())).thenReturn(dbOptional); + dbController.updateDb(dbConfig.getId(), dbConfig, mockBindingResult, httpServletResponse); + } + + @Test + public void testDblistByTool() { + List < DbType > dbTypeList = new ArrayList < > (); + DbType dbType = new DbType("ES", "Elasticsearch"); + Set < Db > dbs = new HashSet < > (); + dbs.add(TestUtil.newDb("MongoDB")); + dbType.setDbs(dbs); + dbTypeList.add(dbType); + when(dbTypeRepository.findByTool(false)).thenReturn(dbTypeList); + dbController.dblistByTool(true); + } + + @Test + public void testListIdAndName() { + DesignType designType = new DesignType(); + DbType dbType = new DbType("ES", "Elasticsearch"); + Set < Db > dbs = new HashSet < > (); + dbs.add(TestUtil.newDb("MongoDB")); + dbType.setDbs(dbs); + designType.setName("Kibana"); + designType.setDbType(dbType); + when(designTypeRepository.findById("1")).thenReturn(Optional.of(designType)); + dbController.listIdAndName("1"); + } + + @Test + public void testCreateDbError() throws IOException { + when(mockBindingResult.hasErrors()).thenReturn(true); + assertEquals(null, dbController.createDb(getDbConfig(), mockBindingResult, httpServletResponse)); + } + + @Test(expected = NullPointerException.class) + public void testCreateDbException() throws IOException { + DbConfig dbConfig = getDbConfig(); + dbConfig.setDbTypeId(""); + dbController.createDb(dbConfig, mockBindingResult, httpServletResponse); + } + + @Test + public void createDbTest() throws IOException { + DbConfig dbConfig = getDbConfig(); + DbType dbType = new DbType("ES", "Elasticsearch"); + when(dbTypeRepository.findById(dbConfig.getDbTypeId())).thenReturn(Optional.of(dbType)); + dbController.createDb(dbConfig, mockBindingResult, httpServletResponse); + } + + @Test + public void testGetDb() throws IOException { + DbConfig elecsticsearch = dbController.getDb(1, httpServletResponse); + assertEquals(null, elecsticsearch); + } + + @Test + public void testGetDbTypes() throws IOException { + List < DbType > dbTypeList = new ArrayList < > (); + DbType dbType = new DbType("ES", "Elasticsearch"); + Set < Db > dbs = new HashSet < > (); + dbs.add(TestUtil.newDb("MongoDB")); + dbType.setDbs(dbs); + dbTypeList.add(dbType); + when(dbTypeRepository.findAll()).thenReturn(dbTypeList); + dbController.getDbTypes(httpServletResponse); + } + } diff --git a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/DesignControllerTest.java b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/DesignControllerTest.java index 4b933bee..ca670998 100644 --- a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/DesignControllerTest.java +++ b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/DesignControllerTest.java @@ -3,6 +3,7 @@ * ONAP : DATALAKE * ================================================================================ * Copyright 2019 China Mobile + * Copyright (C) 2022 Wipro Limited. *================================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,10 +30,12 @@ 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.*; import org.onap.datalake.feeder.domain.Design; +import org.onap.datalake.feeder.domain.DesignType; +import org.onap.datalake.feeder.domain.TopicName; import org.onap.datalake.feeder.dto.DesignConfig; import org.onap.datalake.feeder.repository.DesignTypeRepository; +import org.onap.datalake.feeder.repository.TopicNameRepository; import org.onap.datalake.feeder.repository.DesignRepository; import org.onap.datalake.feeder.service.DesignService; import org.onap.datalake.feeder.service.TopicService; @@ -46,12 +49,14 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class DesignControllerTest { - + //static String Kibana_Dashboard_Import_Api = "/api/kibana/dashboards/import?exclude=index-pattern"; @Mock @@ -72,9 +77,14 @@ public class DesignControllerTest { @Mock private DesignTypeRepository designTypeRepository; - @InjectMocks + @Mock + private TopicNameRepository topicNameRepository; + + @Mock private DesignService designService; + @InjectMocks + private DesignController testDesignController; @Before public void setupTest() { @@ -85,40 +95,82 @@ public class DesignControllerTest { @Test public void testCreateDesign() throws NoSuchFieldException, IllegalAccessException, IOException { - DesignController testDesignController = new DesignController(); - setAccessPrivateFields(testDesignController); Design testDesign = fillDomain(); + DesignConfig designConfig = new DesignConfig(); + when(designService.fillDesignConfiguration(designConfig)).thenReturn(testDesign); + testDesignController.createDesign(designConfig, mockBindingResult, httpServletResponse); + } + + @Test + public void testCreateDesignNull() throws NoSuchFieldException, IllegalAccessException, IOException { + + DesignConfig designConfig = new DesignConfig(); + when(designService.fillDesignConfiguration(designConfig)).thenThrow(NullPointerException.class); + testDesignController.createDesign(designConfig, mockBindingResult, httpServletResponse); + } + + @Test + public void testCreateDesignError() throws NoSuchFieldException, IllegalAccessException, IOException { + + DesignConfig designConfig = new DesignConfig(); + when(mockBindingResult.hasErrors()).thenReturn(true); + assertEquals(null, testDesignController.createDesign(designConfig, mockBindingResult, httpServletResponse)); + } + + @Test + public void testUpdateDesignNull() throws NoSuchFieldException, IllegalAccessException, IOException { + + Design testDesign = fillDomain(); + Integer id = 1; //when(topicService.getTopic(0)).thenReturn(new Topic("unauthenticated.SEC_FAULT_OUTPUT")); -// when(designTypeRepository.findById("Kibana Dashboard")).thenReturn(Optional.of(testDesign.getDesignType())); - PostReturnBody postPortal = testDesignController.createDesign(testDesign.getDesignConfig(), mockBindingResult, httpServletResponse); + // when(designTypeRepository.findById("Kibana Dashboard")).thenReturn(Optional.of(testDesign.getDesignType())); + PostReturnBody < DesignConfig > postPortal = testDesignController.updateDesign(testDesign.getDesignConfig(), mockBindingResult, id, httpServletResponse); //assertEquals(postPortal.getStatusCode(), 200); assertNull(postPortal); } + @Test + public void testUpdateDesignError() throws NoSuchFieldException, IllegalAccessException, IOException { + Design testDesign = fillDomain(); + Integer id = 1; + when(mockBindingResult.hasErrors()).thenReturn(true); + assertEquals(null, testDesignController.updateDesign(testDesign.getDesignConfig(), mockBindingResult, id, httpServletResponse)); + } + @Test public void testUpdateDesign() throws NoSuchFieldException, IllegalAccessException, IOException { + Design testDesign = fillDomain(); + Integer id = 1; + when(designService.getDesign(id)).thenReturn(testDesign); + testDesignController.updateDesign(testDesign.getDesignConfig(), mockBindingResult, id, httpServletResponse); + } - DesignController testDesignController = new DesignController(); - setAccessPrivateFields(testDesignController); + @Test + public void testUpdateDesignException() throws NoSuchFieldException, IllegalAccessException, IOException { Design testDesign = fillDomain(); + DesignConfig designConfig = new DesignConfig(); Integer id = 1; - when(designRepository.findById(id)).thenReturn((Optional.of(testDesign))); - //when(topicService.getTopic(0)).thenReturn(new Topic("unauthenticated.SEC_FAULT_OUTPUT")); - // when(designTypeRepository.findById("Kibana Dashboard")).thenReturn(Optional.of(testDesign.getDesignType())); - PostReturnBody postPortal = testDesignController.updateDesign(testDesign.getDesignConfig(), mockBindingResult, id, httpServletResponse); - //assertEquals(postPortal.getStatusCode(), 200); - assertNull(postPortal); + when(designService.getDesign(id)).thenReturn(testDesign); + doThrow(NullPointerException.class).when(designService).fillDesignConfiguration(designConfig, testDesign); + testDesignController.updateDesign(designConfig, mockBindingResult, id, httpServletResponse); + } + + @Test + public void testDeleteDesignNull() throws NoSuchFieldException, IllegalAccessException, IOException { + + Design testDesign = fillDomain(); + Integer id = 1; + testDesign.setId(1); + testDesignController.deleteDesign(id, httpServletResponse); } @Test public void testDeleteDesign() throws NoSuchFieldException, IllegalAccessException, IOException { - DesignController testDesignController = new DesignController(); - setAccessPrivateFields(testDesignController); Design testDesign = fillDomain(); Integer id = 1; testDesign.setId(1); - when(designRepository.findById(id)).thenReturn((Optional.of(testDesign))); + when(designService.getDesign(id)).thenReturn(testDesign); testDesignController.deleteDesign(id, httpServletResponse); } @@ -128,22 +180,27 @@ public class DesignControllerTest { DesignController testDesignController = new DesignController(); setAccessPrivateFields(testDesignController); Design testDesign = fillDomain(); - List designList = new ArrayList<>(); + List < Design > designList = new ArrayList < > (); designList.add(testDesign); - when(designRepository.findAll()).thenReturn(designList); - assertEquals(1, testDesignController.queryAllDesign().size()); + assertEquals(0, testDesignController.queryAllDesign().size()); + } + + @Test + public void testDeployDesignNull() throws NoSuchFieldException, IllegalAccessException, IOException { + + Design testDesign = fillDomain(); + Integer id = 1; + testDesign.setId(1); + testDesignController.deployDesign(id, httpServletResponse); } - @Test(expected = NullPointerException.class) + @Test public void testDeployDesign() throws NoSuchFieldException, IllegalAccessException, IOException { - DesignController testDesignController = new DesignController(); - setAccessPrivateFields(testDesignController); Design testDesign = fillDomain(); Integer id = 1; testDesign.setId(1); - //when(applicationConfiguration.getKibanaDashboardImportApi()).thenReturn(Kibana_Dashboard_Import_Api); - when(designRepository.findById(id)).thenReturn((Optional.of(testDesign))); + when(designRepository.findById(id)).thenReturn(Optional.of(new Design())); testDesignController.deployDesign(id, httpServletResponse); } @@ -158,16 +215,18 @@ public class DesignControllerTest { } - public Design fillDomain(){ + public Design fillDomain() { Design design = new Design(); + design.setId(1); design.setName("Kibana"); design.setBody("jsonString"); design.setSubmitted(false); design.setNote("test"); DesignType designType = new DesignType(); + designType.setId("1"); designType.setName("Kibana Dashboard"); design.setDesignType(designType); design.setTopicName(new TopicName("unauthenticated.SEC_FAULT_OUTPUT")); return design; } -} \ No newline at end of file +} 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 index 91af11ca..531b0b28 100644 --- 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 @@ -3,6 +3,7 @@ * ONAP : DATALAKE * ================================================================================ * Copyright 2019 China Mobile + * Copyright (C) 2022 Wipro Limited. *================================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,50 +25,51 @@ 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.domain.DesignType; +import org.onap.datalake.feeder.dto.DesignTypeConfig; +import org.onap.datalake.feeder.repository.DesignTypeRepository; 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.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class DesignTypeControllerTest { - @InjectMocks + @Mock + private DesignTypeRepository designTypeRepository; + + @Mock private DesignTypeService designTypeService; + @InjectMocks + private DesignTypeController designTypeController; + @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 designTypeNamesList = new ArrayList<>(); - designTypeNamesList.add(testDesignType.getName()); - assertEquals(1, testDesignTypeController.getDesignType().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(){ + public DesignType fillDomain() { DesignType designType = new DesignType(); designType.setName("Kibana Dashboard"); return designType; } -} \ No newline at end of file + + @Test + public void testGetDesignType() { + List < DesignTypeConfig > designTypeNamesList = new ArrayList < > (); + List < DesignType > designTypeList = new ArrayList < > (); + DesignType designType = fillDomain(); + designTypeList.add(designType); + when(designTypeService.getDesignTypes()).thenReturn(designTypeNamesList); + assertNotNull(designTypeController.getDesignType()); + } + +} diff --git a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/KafkaControllerTest.java b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/KafkaControllerTest.java index 06aa61db..fd685fef 100644 --- a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/KafkaControllerTest.java +++ b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/KafkaControllerTest.java @@ -3,6 +3,7 @@ * ONAP : DataLake * ================================================================================ * Copyright 2019 China Mobile + * Copyright (C) 2022 Wipro Limited. *================================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -35,7 +36,8 @@ import javax.servlet.http.HttpServletResponse; import java.io.IOException; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) @@ -68,17 +70,74 @@ public class KafkaControllerTest { when(kafkaService.getKafkaById(kafkaConfig.getId())).thenReturn(null).thenReturn(kafka); when(kafkaRepository.save(kafka)).thenReturn(null); when(kafkaService.fillKafkaConfiguration(kafkaConfig)).thenReturn(kafka); - when(mockBindingResult.hasErrors()).thenReturn(false,true,false,true); + when(mockBindingResult.hasErrors()).thenReturn(false, true, false, true); - kafkaController.createKafka(kafkaConfig,mockBindingResult,httpServletResponse); - kafkaController.createKafka(kafkaConfig,mockBindingResult,httpServletResponse); + kafkaController.createKafka(kafkaConfig, mockBindingResult, httpServletResponse); + kafkaController.createKafka(kafkaConfig, mockBindingResult, httpServletResponse); - kafkaController.updateKafka(kafkaConfig,mockBindingResult,id,httpServletResponse); - kafkaController.updateKafka(kafkaConfig,mockBindingResult,id,httpServletResponse); + kafkaController.updateKafka(kafkaConfig, mockBindingResult, id, httpServletResponse); + kafkaController.updateKafka(kafkaConfig, mockBindingResult, id, httpServletResponse); - kafkaController.deleteKafka(id,httpServletResponse); + kafkaController.deleteKafka(id, httpServletResponse); when(kafkaService.getAllKafka()).thenReturn(null); kafkaController.queryAllKafka(); } -} \ No newline at end of file + + @Test + public void testCreateKafkaNull() throws IOException { + KafkaConfig kafkaConfig = new KafkaConfig(); + kafkaConfig.setId(1); + kafkaConfig.setName("123"); + when(kafkaService.getKafkaById(kafkaConfig.getId())).thenReturn(kafka); + assertEquals(null, kafkaController.createKafka(kafkaConfig, mockBindingResult, httpServletResponse)); + } + + @Test + public void testCreateKafkaException() throws IOException { + KafkaConfig kafkaConfig = new KafkaConfig(); + kafkaConfig.setId(1); + kafkaConfig.setName("123"); + when(kafkaService.getKafkaById(kafkaConfig.getId())).thenReturn(null); + when(kafkaService.fillKafkaConfiguration(kafkaConfig)).thenThrow(NullPointerException.class); + assertEquals(null, kafkaController.createKafka(kafkaConfig, mockBindingResult, httpServletResponse)); + } + + @Test + public void testUpdateKafkaNull() throws IOException { + KafkaConfig kafkaConfig = new KafkaConfig(); + kafkaConfig.setId(1); + kafkaConfig.setName("123"); + when(kafkaService.getKafkaById(kafkaConfig.getId())).thenReturn(null); + assertEquals(null, kafkaController.updateKafka(kafkaConfig, mockBindingResult, 1, httpServletResponse)); + } + + @Test + public void testUpdateKafkaException() throws IOException { + KafkaConfig kafkaConfig = new KafkaConfig(); + kafkaConfig.setId(1); + kafkaConfig.setName("123"); + when(kafkaService.getKafkaById(kafkaConfig.getId())).thenReturn(kafka); + doThrow(NullPointerException.class).when(kafkaService).fillKafkaConfiguration(kafkaConfig, kafka); + assertEquals(null, kafkaController.updateKafka(kafkaConfig, mockBindingResult, 1, httpServletResponse)); + } + + @Test + public void testDeleteKafkaNull() throws IOException { + when(kafkaService.getKafkaById(1)).thenReturn(null); + kafkaController.deleteKafka(1, httpServletResponse); + } + + @Test + public void testGetKafkaDetailNull() throws IOException { + when(kafkaService.getKafkaById(1)).thenReturn(null); + kafkaController.getKafkaDetail(1, httpServletResponse); + } + + @Test + public void testGetKafkaDetail() throws IOException { + when(kafkaService.getKafkaById(1)).thenReturn(kafka); + assertEquals(null, kafkaController.getKafkaDetail(1, httpServletResponse)); + } + +} diff --git a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/TopicControllerTest.java b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/TopicControllerTest.java index 988010ec..17107120 100644 --- a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/TopicControllerTest.java +++ b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/TopicControllerTest.java @@ -3,6 +3,7 @@ * ONAP : DATALAKE * ================================================================================ * Copyright (C) 2018-2019 Huawei. All rights reserved. + * Copyright (C) 2022 Wipro Limited. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,145 +28,185 @@ import org.mockito.Mock; 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.Kafka; import org.onap.datalake.feeder.domain.Topic; import org.onap.datalake.feeder.dto.TopicConfig; +import org.onap.datalake.feeder.repository.KafkaRepository; import org.onap.datalake.feeder.repository.TopicNameRepository; import org.onap.datalake.feeder.repository.TopicRepository; import org.onap.datalake.feeder.service.DbService; import org.onap.datalake.feeder.service.DmaapService; import org.onap.datalake.feeder.service.TopicService; import org.onap.datalake.feeder.util.TestUtil; +import org.springframework.context.ApplicationContext; import org.springframework.validation.BindingResult; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.Optional; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class TopicControllerTest { - static String DEFAULT_TOPIC_NAME = "_DL_DEFAULT_"; + static String DEFAULT_TOPIC_NAME = "_DL_DEFAULT_"; - @Mock - private HttpServletResponse httpServletResponse; + @Mock + private HttpServletResponse httpServletResponse; - @Mock - private BindingResult mockBindingResult; + @Mock + private BindingResult mockBindingResult; - @Mock - private TopicRepository topicRepository; + @Mock + private TopicRepository topicRepository; - @Mock - private TopicService topicService; + @Mock + private TopicService topicService; - @Mock - private TopicNameRepository topicNameRepository; + @Mock + private TopicNameRepository topicNameRepository; + + @Mock + private KafkaRepository kafkaRepository; + + @InjectMocks + TopicController topicController; + + @Mock + private ApplicationConfiguration config; + + @Mock + private ApplicationContext context; + + @Mock + private DbService dbService; + + @Mock + private DmaapService dmaapService; + + @Before + public void setupTest() throws NoSuchFieldException, IllegalAccessException { + // While the default boolean return value for a mock is 'false', + // it's good to be explicit anyway: + when(mockBindingResult.hasErrors()).thenReturn(false); + } + + @Test + public void testListTopic() throws IOException, NoSuchFieldException, IllegalAccessException {} + + @Test + public void testCreateTopic() throws IOException { + Topic a = TestUtil.newTopic("a"); + a.setId(1); + a.setEnabled(true); + + TopicConfig ac = a.getTopicConfig(); + + when(topicService.fillTopicConfiguration(ac)).thenReturn(a); + PostReturnBody < TopicConfig > postTopic = topicController.createTopic(ac, mockBindingResult, httpServletResponse); + assertEquals(postTopic.getStatusCode(), 200); + + when(topicService.fillTopicConfiguration(ac)).thenReturn(a); + a.setTtl(0); + PostReturnBody < TopicConfig > postTopicConfig = topicController.createTopic(ac, mockBindingResult, httpServletResponse); + assertEquals(postTopicConfig.getStatusCode(), 200); + + when(mockBindingResult.hasErrors()).thenReturn(true); + PostReturnBody < TopicConfig > topicConfig = topicController.createTopic(ac, mockBindingResult, httpServletResponse); + assertEquals(null, topicConfig); + } + + @Test + public void testUpdateTopic() throws IOException { + Topic a = TestUtil.newTopic("a"); + a.setId(1); + a.setEnabled(true); + + TopicConfig ac = a.getTopicConfig(); + + when(topicService.getTopic(1)).thenReturn(a); + PostReturnBody < TopicConfig > postConfig1 = topicController.updateTopic(1, ac, mockBindingResult, httpServletResponse); + assertEquals(200, postConfig1.getStatusCode()); + TopicConfig ret = postConfig1.getReturnBody(); + assertEquals("a", ret.getName()); + assertEquals(true, ret.isEnabled()); + + topicController.updateTopic(0, ac, mockBindingResult, httpServletResponse); + + when(topicService.getTopic(1)).thenReturn(null); + topicController.updateTopic(1, ac, mockBindingResult, httpServletResponse); + + when(mockBindingResult.hasErrors()).thenReturn(true); + PostReturnBody < TopicConfig > postConfig2 = topicController.updateTopic(1, ac, mockBindingResult, httpServletResponse); + assertNull(postConfig2); + + } + + @Test + public void testGetTopic() throws IOException { + Topic a = TestUtil.newTopic("a"); + a.setId(1); + a.setEnabled(true); + + when(topicService.getTopic(1)).thenReturn(a); + TopicConfig ac = topicController.getTopic(1, httpServletResponse); + when(topicService.getTopic(1)).thenReturn(null); + ac = topicController.getTopic(1, httpServletResponse); + } + + @Test + public void testDeleteTopic() throws IOException { + Topic a = TestUtil.newTopic("a"); + a.setId(1); + a.setEnabled(true); + + when(topicService.getTopic(1)).thenReturn(a); + topicController.deleteTopic(1, httpServletResponse); + when(topicService.getTopic(1)).thenReturn(null); + topicController.deleteTopic(1, httpServletResponse); + } + + @Test + public void testList() { + ArrayList < Topic > topics = new ArrayList < > (); + topics.add(TestUtil.newTopic("a")); + topics.add(TestUtil.newTopic(DEFAULT_TOPIC_NAME)); + when(topicRepository.findAll()).thenReturn(topics); + + List < Integer > ids = topicController.list(); + for (Integer topic: ids) { + System.out.println(topic); + } + } + + @Test + public void testGetDefaultConfigNull() throws IOException { + Topic topic = null; + when(topicService.getDefaultTopicFromFeeder()).thenReturn(topic); + assertEquals(null, topicController.getDefaultConfig(httpServletResponse)); + } + + @Test + public void testGetDefaultConfig() throws IOException { + Topic topic = TestUtil.newTopic(DEFAULT_TOPIC_NAME); + when(topicService.getDefaultTopicFromFeeder()).thenReturn(topic); + assertEquals(topic.getName(), topicController.getDefaultConfig(httpServletResponse).getName()); + } + + @Test + public void testListDmaapTopics() { + Kafka kafka = TestUtil.newKafka("test"); + when(kafkaRepository.findById(1)).thenReturn(Optional.of(kafka)); + DmaapService dmaapService = mock(DmaapService.class); + when(context.getBean(DmaapService.class, kafka)).thenReturn(dmaapService); + when(dmaapService.getTopics()).thenReturn(null); + assertEquals(null, topicController.listDmaapTopics(1)); + } - @InjectMocks - TopicController topicController; - - @Mock - private ApplicationConfiguration config; - - @Mock - private DbService dbService; - - @Mock - private DmaapService dmaapService; - - @Before - public void setupTest() throws NoSuchFieldException, IllegalAccessException { - // While the default boolean return value for a mock is 'false', - // it's good to be explicit anyway: - when(mockBindingResult.hasErrors()).thenReturn(false); - } - - @Test - public void testListTopic() throws IOException, NoSuchFieldException, IllegalAccessException { - } - - @Test - public void testCreateTopic() throws IOException { - Topic a = TestUtil.newTopic("a"); - a.setId(1); - a.setEnabled(true); - - TopicConfig ac = a.getTopicConfig(); - - when(topicService.fillTopicConfiguration(ac)).thenReturn(a); - PostReturnBody postTopic = topicController.createTopic(ac, mockBindingResult, httpServletResponse); - assertEquals(postTopic.getStatusCode(), 200); - - when(mockBindingResult.hasErrors()).thenReturn(true); - PostReturnBody topicConfig = topicController.createTopic(ac, mockBindingResult, httpServletResponse); - assertEquals(null, topicConfig); - } - - @Test - public void testUpdateTopic() throws IOException { - Topic a = TestUtil.newTopic("a"); - a.setId(1); - a.setEnabled(true); - - TopicConfig ac = a.getTopicConfig(); - - when(topicService.getTopic(1)).thenReturn(a); - PostReturnBody postConfig1 = topicController.updateTopic(1, ac, mockBindingResult, httpServletResponse); - assertEquals(200, postConfig1.getStatusCode()); - TopicConfig ret = postConfig1.getReturnBody(); - assertEquals("a", ret.getName()); - assertEquals(true, ret.isEnabled()); - - topicController.updateTopic(0, ac, mockBindingResult, httpServletResponse); - - when(topicService.getTopic(1)).thenReturn(null); - topicController.updateTopic(1, ac, mockBindingResult, httpServletResponse); - - when(mockBindingResult.hasErrors()).thenReturn(true); - PostReturnBody postConfig2 = topicController.updateTopic(1, ac, mockBindingResult, httpServletResponse); - assertNull(postConfig2); - - } - - @Test - public void testGetTopic() throws IOException { - Topic a = TestUtil.newTopic("a"); - a.setId(1); - a.setEnabled(true); - - when(topicService.getTopic(1)).thenReturn(a); - TopicConfig ac = topicController.getTopic(1, httpServletResponse); - when(topicService.getTopic(1)).thenReturn(null); - ac = topicController.getTopic(1, httpServletResponse); - } - - @Test - public void testDeleteTopic() throws IOException { - Topic a = TestUtil.newTopic("a"); - a.setId(1); - a.setEnabled(true); - - when(topicService.getTopic(1)).thenReturn(a); - topicController.deleteTopic(1, httpServletResponse); - when(topicService.getTopic(1)).thenReturn(null); - topicController.deleteTopic(1, httpServletResponse); - } - - @Test - public void testList() { - ArrayList topics = new ArrayList<>(); - topics.add(TestUtil.newTopic("a")); - topics.add(TestUtil.newTopic(DEFAULT_TOPIC_NAME)); - when(topicRepository.findAll()).thenReturn(topics); - - List ids = topicController.list(); - for (Integer topic : ids) { - System.out.println(topic); - } - } } diff --git a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/TopicNameControllerTest.java b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/TopicNameControllerTest.java new file mode 100644 index 00000000..9ac5f1b3 --- /dev/null +++ b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/TopicNameControllerTest.java @@ -0,0 +1,67 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : DCAE + * ================================================================================ + * Copyright (C) 2022 Wipro Limited. + * ================================================================================= + * 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 static org.junit.Assert.assertEquals; +import static org.mockito.Mockito.when; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.datalake.feeder.domain.Topic; +import org.onap.datalake.feeder.domain.TopicName; +import org.onap.datalake.feeder.repository.TopicNameRepository; + +@RunWith(MockitoJUnitRunner.class) +public class TopicNameControllerTest { + + @Mock + private TopicNameRepository topicNameRepository; + + @InjectMocks + TopicNameController topicNameController; + + @Test + public void testList() throws IOException { + List < TopicName > topicNameList = new ArrayList < > (); + + TopicName topicName = new TopicName(); + topicName.setId("1"); + topicName.setDesigns(null); + Topic topic = new Topic(); + topic.setId(1); + Set < Topic > topics = new HashSet < > (); + topics.add(topic); + topicName.setTopics(topics); + topicNameList.add(topicName); + + when(topicNameRepository.findAll()).thenReturn(topicNameList); + List < String > retString = topicNameController.list(); + assertEquals("1", retString.get(0)); + + } + +} diff --git a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/domain/DbTypeTest.java b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/domain/DbTypeTest.java index 4a75df17..11432e16 100644 --- a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/domain/DbTypeTest.java +++ b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/domain/DbTypeTest.java @@ -3,6 +3,7 @@ * ONAP : DataLake * ================================================================================ * Copyright 2019 China Mobile + * Copyright (C) 2022 Wipro Limited. *================================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,20 +20,23 @@ */ package org.onap.datalake.feeder.domain; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import org.junit.Test; -import static org.junit.Assert.*; - public class DbTypeTest { @Test - public void test(){ - DbType dbType = new DbType("ES","Elasticsearch"); - + public void test() { + DbType dbType = new DbType("ES", "Elasticsearch"); + DbType dbType2 = new DbType("MONGO", "MongoDB"); - dbType.setTool(false); - + dbType.setTool(false); + assertNotNull(dbType.toString()); assertEquals(dbType, dbType); @@ -44,10 +48,10 @@ public class DbTypeTest { assertEquals("MongoDB", dbType2.getName()); dbType2.setName(null); dbType2.setDefaultPort(1); - assertTrue(1==dbType2.getDefaultPort()); + assertTrue(1 == dbType2.getDefaultPort()); dbType2.setDbs(null); assertNull(dbType2.getDbs()); } -} \ No newline at end of file +} diff --git a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/domain/DesignTest.java b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/domain/DesignTest.java index de6fec27..efc49bf9 100644 --- a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/domain/DesignTest.java +++ b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/domain/DesignTest.java @@ -3,6 +3,7 @@ * ONAP : DATALAKE * ================================================================================ * Copyright 2019 China Mobile + * Copyright (C) 2022 Wipro Limited. *================================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,11 +21,12 @@ package org.onap.datalake.feeder.domain; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import org.junit.Test; import org.onap.datalake.feeder.util.TestUtil; -import static org.junit.Assert.*; - public class DesignTest { @Test @@ -53,4 +55,4 @@ public class DesignTest { assertNull(design.getDbs()); } -} \ No newline at end of file +} diff --git a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/domain/DesignTypeTest.java b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/domain/DesignTypeTest.java index e02c2d1c..da1c5ec1 100644 --- a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/domain/DesignTypeTest.java +++ b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/domain/DesignTypeTest.java @@ -3,6 +3,7 @@ * ONAP : DATALAKE * ================================================================================ * Copyright 2019 China Mobile + * Copyright (C) 2022 Wipro Limited. *================================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,10 +21,9 @@ package org.onap.datalake.feeder.domain; +import static org.junit.Assert.assertEquals; import org.junit.Test; -import static org.junit.Assert.*; - public class DesignTypeTest { @Test @@ -40,4 +40,4 @@ public class DesignTypeTest { designType.getDesigns(); designType.getDesignTypeConfig(); } -} \ No newline at end of file +} diff --git a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/dto/DesignConfigTest.java b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/dto/DesignConfigTest.java index 22ebe4f1..6b0b006f 100644 --- a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/dto/DesignConfigTest.java +++ b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/dto/DesignConfigTest.java @@ -3,6 +3,7 @@ * ONAP : DATALAKE * ================================================================================ * Copyright 2019 China Mobile + * Copyright (C) 2022 Wipro Limited. *================================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,13 +21,13 @@ package org.onap.datalake.feeder.dto; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import org.junit.Test; import org.onap.datalake.feeder.domain.Design; import org.onap.datalake.feeder.domain.DesignType; import org.onap.datalake.feeder.domain.TopicName; -import static org.junit.Assert.*; - public class DesignConfigTest { @Test @@ -58,4 +59,4 @@ public class DesignConfigTest { assertEquals(testDesignConfig.getDesignType(), null); } -} \ No newline at end of file +} diff --git a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/dto/DesignTypeConfigTest.java b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/dto/DesignTypeConfigTest.java new file mode 100644 index 00000000..a7253ae8 --- /dev/null +++ b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/dto/DesignTypeConfigTest.java @@ -0,0 +1,37 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : DCAE + * ================================================================================ + * Copyright (C) 2022 Wipro Limited. + * ================================================================================= + * 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.dto; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +public class DesignTypeConfigTest { + + @Test + public void testDesignTypeConfig() { + DesignTypeConfig designTypeConfig = new DesignTypeConfig(); + designTypeConfig.setId("123"); + designTypeConfig.setName("test"); + assertEquals("123", designTypeConfig.getId()); + assertEquals("test", designTypeConfig.getName()); + } + +} diff --git a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/dto/KafkaConfigTest.java b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/dto/KafkaConfigTest.java index b2104177..2d38f532 100644 --- a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/dto/KafkaConfigTest.java +++ b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/dto/KafkaConfigTest.java @@ -3,6 +3,7 @@ * ONAP : DataLake * ================================================================================ * Copyright 2019 China Mobile + * Copyright (C) 2022 Wipro Limited. *================================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,12 +20,12 @@ */ package org.onap.datalake.feeder.dto; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import org.junit.Test; import org.onap.datalake.feeder.domain.Kafka; import org.onap.datalake.feeder.util.TestUtil; -import static org.junit.Assert.*; - /** * Test Kafka * @@ -76,4 +77,4 @@ public class KafkaConfigTest { assertNotEquals(null, testKafkaConfig.getTimeout()); } -} \ No newline at end of file +} diff --git a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/DesignServiceTest.java b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/DesignServiceTest.java index 65b373f5..cdc8c42a 100644 --- a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/DesignServiceTest.java +++ b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/DesignServiceTest.java @@ -3,6 +3,7 @@ * ONAP : DCAE * ================================================================================ * Copyright 2019 China Mobile + * Copyright (C) 2022 Wipro Limited. *================================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,26 +26,54 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import org.onap.datalake.feeder.config.ApplicationConfiguration; +import org.onap.datalake.feeder.domain.Db; import org.onap.datalake.feeder.domain.Design; import org.onap.datalake.feeder.domain.DesignType; +import org.onap.datalake.feeder.domain.TopicName; +import org.onap.datalake.feeder.dto.DesignConfig; +import org.onap.datalake.feeder.repository.DbRepository; +import org.onap.datalake.feeder.repository.DesignRepository; +import org.onap.datalake.feeder.repository.DesignTypeRepository; +import org.onap.datalake.feeder.repository.TopicNameRepository; +import org.onap.datalake.feeder.util.TestUtil; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.mockito.Mockito.when; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Optional; +import java.util.Set; + @RunWith(MockitoJUnitRunner.class) public class DesignServiceTest { @Mock private DesignType designType; + @Mock + private DesignRepository designRepository; + + @Mock + private TopicNameRepository topicNameRepository; + + @Mock + private DbRepository dbRepository; + @Mock private ApplicationConfiguration applicationConfiguration; + @Mock + private DesignTypeRepository designTypeRepository; + @InjectMocks private DesignService designService; @Test(expected = RuntimeException.class) - public void testDeploy() { - when(designType.getId()).thenReturn("KIBANA_DB","ES_MAPPING"); + public void testDeployException() { + when(designType.getId()).thenReturn("KIBANA_DB", "ES_MAPPING"); Design design = new Design(); design.setDesignType(designType); design.setBody("jsonString"); @@ -53,4 +82,96 @@ public class DesignServiceTest { designService.deploy(design); System.out.println(); } -} \ No newline at end of file + + @Test + public void testFillDesignConfigurationNull() { + DesignConfig designConfig = new DesignConfig(); + designConfig.setTopicName("topic"); + designConfig.setDesignType("designType"); + TopicName topicName = new TopicName("test"); + Optional < TopicName > topicNameOptional = Optional.of(topicName); + when(topicNameRepository.findById(designConfig.getTopicName())).thenReturn(topicNameOptional); + when(designTypeRepository.findById(designConfig.getDesignType())).thenReturn(Optional.of(new DesignType())); + assertNull(designService.fillDesignConfiguration(designConfig).getName()); + } + + @Test + public void testFillDesignConfiguration() { + DesignConfig designConfig = new DesignConfig(); + designConfig.setTopicName("topic"); + designConfig.setDesignType("designType"); + + List < Integer > dbs = new ArrayList < > (); + dbs.add(1); + designConfig.setDbs(dbs); + when(topicNameRepository.findById(designConfig.getTopicName())).thenReturn(Optional.of(new TopicName())); + when(designTypeRepository.findById(designConfig.getDesignType())).thenReturn(Optional.of(new DesignType())); + when(dbRepository.findById(designConfig.getDbs().get(0))).thenReturn(Optional.of(new Db())); + designService.fillDesignConfiguration(designConfig).getName(); + } + + @Test(expected = IllegalArgumentException.class) + public void testFillDesign() { + Design design = new Design(); + design.setDesignType(designType); + DesignConfig designConfig = new DesignConfig(); + designService.fillDesignConfiguration(designConfig, design); + } + + @Test + public void testGetDesignNull() { + Optional < Design > testDesign = Optional.ofNullable(null); + when(designRepository.findById(1)).thenReturn(testDesign); + assertNull(designService.getDesign(1)); + } + + @Test + public void testDeploy() { + when(designType.getId()).thenReturn("KIBANA_DB"); + Design design = getDesign(); + assertNotNull(designService.deploy(design)); + } + + @Test + public void testDeployESMappingCase() { + when(designType.getId()).thenReturn("ES_MAPPING"); + Design design = getDesign(); + assertNotNull(designService.deploy(design)); + } + + @Test + public void testDeployDefault() { + when(designType.getId()).thenReturn("KIBANA_SEARCH"); + Design design = getDesign(); + assertNull(designService.deploy(design)); + } + + @Test(expected = NullPointerException.class) + public void testQueryAllDesignNull() { + when(designRepository.findAll()).thenReturn(null); + designService.queryAllDesign(); + } + + @Test + public void testQueryAllDesign() { + List < Design > designList = new ArrayList < > (); + Design design = getDesign(); + designList.add(design); + when(designRepository.findAll()).thenReturn(designList); + designService.queryAllDesign(); + } + + public Design getDesign() { + Design design = new Design(); + design.setDesignType(designType); + design.setBody("jsonString"); + design.setTopicName(new TopicName("1")); + Set < Db > dbs = new HashSet < > (); + Db db = TestUtil.newDb("MongoDB"); + db.setEnabled(true); + dbs.add(db); + design.setDbs(dbs); + return design; + } + +} diff --git a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/DesignTypeServiceTest.java b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/DesignTypeServiceTest.java index 5879deb6..28c84750 100644 --- a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/DesignTypeServiceTest.java +++ b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/DesignTypeServiceTest.java @@ -3,6 +3,7 @@ * ONAP : DataLake * ================================================================================ * Copyright 2019 China Mobile + * Copyright (C) 2022 Wipro Limited. *================================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,13 +26,11 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import org.onap.datalake.feeder.domain.DesignType; -import org.onap.datalake.feeder.dto.DesignTypeConfig; import org.onap.datalake.feeder.repository.DesignTypeRepository; - import java.util.ArrayList; import java.util.List; - -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) @@ -44,16 +43,19 @@ public class DesignTypeServiceTest { private DesignTypeService designTypeService; @Test - public void testDesignTypeService(){ - List designTypeList = new ArrayList<>(); + public void testDesignTypeService() { + List < DesignType > designTypeList = new ArrayList < > (); DesignType designType = new DesignType(); designType.setName("test"); - //DesignTypeConfig designTypeConfig = new DesignTypeConfig(); - //designTypeConfig.setDesignType("test"); - //designTypeConfig.setDisplay("test"); designTypeList.add(designType); when(designTypeRepository.findAll()).thenReturn(designTypeList); assertNotNull(designTypeService.getDesignTypes()); } -} \ No newline at end of file + @Test + public void testDesignTypeServiceNull() { + when(designTypeRepository.findAll()).thenReturn(null); + assertEquals(0, designTypeService.getDesignTypes().size()); + } + +} diff --git a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/KafkaServiceTest.java b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/KafkaServiceTest.java index 0274d309..2cb0740c 100644 --- a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/KafkaServiceTest.java +++ b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/KafkaServiceTest.java @@ -3,6 +3,7 @@ * ONAP : DATALAKE * ================================================================================ * Copyright 2019 China Mobile + * Copyright (C) 2022 Wipro Limited. *================================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,12 +29,11 @@ import org.mockito.junit.MockitoJUnitRunner; import org.onap.datalake.feeder.domain.Kafka; import org.onap.datalake.feeder.dto.KafkaConfig; import org.onap.datalake.feeder.repository.KafkaRepository; - import java.util.ArrayList; import java.util.List; import java.util.Optional; - -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) @@ -67,4 +67,4 @@ public class KafkaServiceTest { kafkaService.fillKafkaConfiguration(kafkaConfig); } -} \ No newline at end of file +} diff --git a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/PullServiceTest.java b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/PullServiceTest.java index d6298b87..f587f358 100644 --- a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/PullServiceTest.java +++ b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/PullServiceTest.java @@ -3,6 +3,7 @@ * ONAP : DATALAKE * ================================================================================ * Copyright 2019 China Mobile + * Copyright (C) 2022 Wipro Limited *================================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,74 +28,72 @@ import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import org.onap.datalake.feeder.config.ApplicationConfiguration; import org.springframework.context.ApplicationContext; - import java.lang.reflect.Field; import java.util.List; import java.util.concurrent.ExecutorService; import java.util.concurrent.locks.ReentrantReadWriteLock; - -import static org.junit.Assert.*; +import static org.junit.Assert.assertFalse; import static org.mockito.Mockito.when; @RunWith(MockitoJUnitRunner.class) public class PullServiceTest { - @InjectMocks - private PullService pullService; + @InjectMocks + private PullService pullService; - @Mock - private ApplicationContext context; + @Mock + private ApplicationContext context; - @Mock - private ApplicationConfiguration config; + @Mock + private ApplicationConfiguration config; - @Mock - private ExecutorService executorService; + @Mock + private ExecutorService executorService; - @Mock - private List consumers; + @Mock + private List < Puller > consumers; - @Test - public void isRunning() { - assertFalse(pullService.isRunning()); - } + @Test + public void isRunning() { + assertFalse(pullService.isRunning()); + } - @Test(expected = NullPointerException.class) - public void start() { - setRunning(false); - pullService.start(); - setRunning(true); - pullService.start(); - } + @Test(expected = NullPointerException.class) + public void start() { + setRunning(false); + pullService.start(); + setRunning(true); + pullService.start(); + } - @Test - public void shutdown() { - when(config.getShutdownLock()).thenReturn(new ReentrantReadWriteLock()); - setRunning(false); - pullService.shutdown(); - setRunning(true); - pullService.shutdown(); - } + @Test + public void shutdown() { + when(config.getShutdownLock()).thenReturn(new ReentrantReadWriteLock()); + setRunning(false); + pullService.shutdown(); + setRunning(true); + pullService.shutdown(); + } - private void setRunning(boolean running) { - Field configField; - try { - configField = PullService.class.getDeclaredField("isRunning"); - configField.setAccessible(true); - configField.set(pullService, running); - } catch (IllegalArgumentException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IllegalAccessException e) { - // TODO Auto-generated catch block - e.printStackTrace(); + private void setRunning(boolean running) { + Field configField; + try { + configField = PullService.class.getDeclaredField("isRunning"); + configField.setAccessible(true); + configField.set(pullService, running); + } catch (IllegalArgumentException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (IllegalAccessException e) { + // TODO Auto-generated catch block + e.printStackTrace(); - } catch (NoSuchFieldException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (SecurityException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } -} \ No newline at end of file + } catch (NoSuchFieldException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (SecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } +} diff --git a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/TopicConfigPollingServiceTest.java b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/TopicConfigPollingServiceTest.java index bd26519b..e30aa7b8 100644 --- a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/TopicConfigPollingServiceTest.java +++ b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/TopicConfigPollingServiceTest.java @@ -3,6 +3,7 @@ * ONAP : DATALAKE * ================================================================================ * Copyright 2019 China Mobile + * Copyright (C) 2022 Wipro Limited. *================================================================================= * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,6 +22,7 @@ package org.onap.datalake.feeder.service; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.when; @@ -30,6 +32,7 @@ import java.lang.reflect.Method; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Map; import java.util.Set; @@ -40,6 +43,7 @@ import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import org.onap.datalake.feeder.config.ApplicationConfiguration; +import org.onap.datalake.feeder.domain.EffectiveTopic; import org.onap.datalake.feeder.domain.Kafka; import org.onap.datalake.feeder.util.TestUtil; @@ -51,73 +55,87 @@ import org.onap.datalake.feeder.util.TestUtil; */ @RunWith(MockitoJUnitRunner.class) public class TopicConfigPollingServiceTest { - @Mock - private ApplicationConfiguration config; - @Mock - private DmaapService dmaapService; + @Mock + private ApplicationConfiguration config; - @InjectMocks - private TopicConfigPollingService topicConfigPollingService = new TopicConfigPollingService(); + @Mock + private DmaapService dmaapService; - static String KAFKA_NAME = "kafka1"; + @Mock + private Map < Integer, Map < String, List < EffectiveTopic >>> effectiveTopicMap = new HashMap < > (); - @Before - public void init() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException { - Method init = topicConfigPollingService.getClass().getDeclaredMethod("init"); - init.setAccessible(true); - init.invoke(topicConfigPollingService); + @InjectMocks + private TopicConfigPollingService topicConfigPollingService = new TopicConfigPollingService(); - Set activeTopics = new HashSet<>(Arrays.asList("test")); - Map> activeTopicMap = new HashMap<>(); - activeTopicMap.put(1, activeTopics); + static String KAFKA_NAME = "kafka1"; - Field activeTopicsField = TopicConfigPollingService.class.getDeclaredField("activeTopicMap"); - activeTopicsField.setAccessible(true); - activeTopicsField.set(topicConfigPollingService, activeTopicMap); + @Before + public void init() + throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException { + Method init = topicConfigPollingService.getClass().getDeclaredMethod("init"); + init.setAccessible(true); + init.invoke(topicConfigPollingService); - Method initMethod = TopicConfigPollingService.class.getDeclaredMethod("init"); - initMethod.setAccessible(true); - initMethod.invoke(topicConfigPollingService); - } + Set < String > activeTopics = new HashSet < > (Arrays.asList("test")); + Map < Integer, Set < String >> activeTopicMap = new HashMap < > (); + activeTopicMap.put(1, activeTopics); - @Test - public void testRun() throws InterruptedException { + Field activeTopicsField = TopicConfigPollingService.class.getDeclaredField("activeTopicMap"); + activeTopicsField.setAccessible(true); + activeTopicsField.set(topicConfigPollingService, activeTopicMap); - when(config.getCheckTopicInterval()).thenReturn(1L); + Method initMethod = TopicConfigPollingService.class.getDeclaredMethod("init"); + initMethod.setAccessible(true); + initMethod.invoke(topicConfigPollingService); + } - Thread thread = new Thread(topicConfigPollingService); - thread.start(); + @Test + public void testRun() throws InterruptedException { - Thread.sleep(50); - topicConfigPollingService.shutdown(); - thread.join(); + when(config.getCheckTopicInterval()).thenReturn(1L); - assertTrue(topicConfigPollingService.isActiveTopicsChanged(new Kafka())); - } + Thread thread = new Thread(topicConfigPollingService); + thread.start(); - @Test - public void testRunNoChange() throws InterruptedException { + Thread.sleep(50); + topicConfigPollingService.shutdown(); + thread.join(); - when(config.getCheckTopicInterval()).thenReturn(1L); + assertTrue(topicConfigPollingService.isActiveTopicsChanged(new Kafka())); + } - Thread thread = new Thread(topicConfigPollingService); - thread.start(); + @Test + public void testRunNoChange() throws InterruptedException { - Thread.sleep(50); - topicConfigPollingService.shutdown(); - thread.join(); + when(config.getCheckTopicInterval()).thenReturn(1L); - assertTrue(topicConfigPollingService.isActiveTopicsChanged(new Kafka())); - } + Thread thread = new Thread(topicConfigPollingService); + thread.start(); - @Test - public void testGet() { - Kafka kafka = TestUtil.newKafka(KAFKA_NAME); - kafka.setId(1); - //assertNull(topicConfigPollingService.getEffectiveTopic (kafka, "test")); - assertNotNull(topicConfigPollingService.getActiveTopics(kafka)); + Thread.sleep(50); + topicConfigPollingService.shutdown(); + thread.join(); - } + assertTrue(topicConfigPollingService.isActiveTopicsChanged(new Kafka())); + } -} \ No newline at end of file + @Test + public void testGet() { + Kafka kafka = TestUtil.newKafka(KAFKA_NAME); + kafka.setId(1); + //assertNull(topicConfigPollingService.getEffectiveTopic (kafka, "test")); + assertNotNull(topicConfigPollingService.getActiveTopics(kafka)); + + } + + @Test + public void testGetEffectiveTopic() { + Kafka kafka = TestUtil.newKafka(KAFKA_NAME); + kafka.setId(1); + Map < String, List < EffectiveTopic >> effectiveTopicMapKafka = new HashMap < > (); + when(effectiveTopicMap.get(kafka.getId())).thenReturn(effectiveTopicMapKafka); + assertNull(topicConfigPollingService.getEffectiveTopic(kafka, "test")); + } + +} diff --git a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/TopicNameServiceTest.java b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/TopicNameServiceTest.java new file mode 100644 index 00000000..f040261c --- /dev/null +++ b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/TopicNameServiceTest.java @@ -0,0 +1,55 @@ +/* + * ============LICENSE_START======================================================= + * ONAP : DCAE + * ================================================================================ + * Copyright (C) 2022 Wipro Limited. + * ================================================================================= + * 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.service; + +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.onap.datalake.feeder.domain.TopicName; +import org.onap.datalake.feeder.repository.TopicNameRepository; + +@RunWith(MockitoJUnitRunner.class) +public class TopicNameServiceTest { + + @Mock + private TopicNameRepository topicNameRepository; + + @InjectMocks + private TopicNameService topicNameService; + + @Test + public void testUpdate() { + List < TopicName > topicNameList = new ArrayList < > (); + topicNameList.add(new TopicName("test")); + Collection < String > allTopicNames = new ArrayList < > (); + allTopicNames.add("MONGODB"); + when(topicNameRepository.findAll()).thenReturn(topicNameList); + topicNameService.update(allTopicNames); + } + +} diff --git a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/TopicServiceTest.java b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/TopicServiceTest.java index eea47501..3c03f14e 100644 --- a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/TopicServiceTest.java +++ b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/TopicServiceTest.java @@ -1,53 +1,51 @@ /* -* ============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========================================================= -*/ + * ============LICENSE_START======================================================= + * ONAP : DATALAKE + * ================================================================================ + * Copyright 2019 China Mobile + * Copyright (C) 2022 Wipro Limited. + *================================================================================= + * 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.service; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.when; import java.io.IOException; -import java.util.*; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Optional; +import java.util.Set; -import org.elasticsearch.client.IndicesClient; -import org.elasticsearch.client.RequestOptions; -import org.elasticsearch.client.RestHighLevelClient; -import org.elasticsearch.client.indices.GetIndexRequest; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.invocation.InvocationOnMock; import org.mockito.junit.MockitoJUnitRunner; -import org.mockito.stubbing.Answer; import org.onap.datalake.feeder.config.ApplicationConfiguration; -import org.onap.datalake.feeder.domain.*; +import org.onap.datalake.feeder.domain.Db; +import org.onap.datalake.feeder.domain.DbType; +import org.onap.datalake.feeder.domain.Kafka; +import org.onap.datalake.feeder.domain.Topic; +import org.onap.datalake.feeder.domain.TopicName; import org.onap.datalake.feeder.dto.TopicConfig; -import org.onap.datalake.feeder.enumeration.DbTypeEnum; import org.onap.datalake.feeder.repository.DbRepository; +import org.onap.datalake.feeder.repository.KafkaRepository; import org.onap.datalake.feeder.repository.TopicNameRepository; import org.onap.datalake.feeder.repository.TopicRepository; import org.onap.datalake.feeder.service.db.ElasticsearchService; @@ -61,127 +59,210 @@ import org.onap.datalake.feeder.service.db.ElasticsearchService; @RunWith(MockitoJUnitRunner.class) public class TopicServiceTest { - static String DEFAULT_TOPIC_NAME = "_DL_DEFAULT_"; - - @Mock - private ApplicationConfiguration config; - - @Mock - private TopicRepository topicRepository; - - @Mock - private ElasticsearchService elasticsearchService; - - @Mock - private DbService dbService; - - @Mock - private DbRepository dbRepository; - - @Mock - private TopicNameRepository topicNameRepository; - - @InjectMocks - private TopicService topicService; - - @Test(expected = NullPointerException.class) - public void testGetTopic() throws IOException{ - List topics = new ArrayList<>(); - Topic topic = new Topic(); - DbType dbType = new DbType(); - Set kafkas = new HashSet<>(); - Set dbs = new HashSet<>(); - Db db = new Db(); - db.setName("Elasticsearch"); - dbs.add(db); - - dbType.setId("ES"); - db.setDbType(dbType); - - Kafka kafka = new Kafka(); - kafka.setName("1234"); - kafkas.add(kafka); - - TopicName topicName = new TopicName(); - topicName.setId("1234"); - - topic.setTopicName(topicName); - topic.setKafkas(kafkas); - topic.setEnabled(true); - topic.setDbs(dbs); - topics.add(topic); - when(topicRepository.findAll()).thenReturn(topics); - when((ElasticsearchService)dbService.findDbStoreService(db)).thenReturn(new ElasticsearchService(db)); - topicService.findTopics(kafka,topicName.getId()); - topicService.getEnabledEffectiveTopic(kafka,topicName.getId(),true); - - } - @Test - public void testGetTopicNull() { - Topic topic = new Topic(); - TopicName topicName = new TopicName(); - topicName.setId("_DL_DEFAULT_"); - topic.setId(1234); - topic.setTopicName(topicName); - Optional optional = Optional.of(topic); - when(topicRepository.findById(0)).thenReturn(optional); - when(config.getDefaultTopicName()).thenReturn("_DL_DEFAULT_"); - assertEquals(topic,topicService.getTopic(0)); - assertTrue(topicService.isDefaultTopic(topic)); - } - - @Test - public void testFillTopic(){ - TopicConfig tConfig = new TopicConfig(); - tConfig.setId(1234); - tConfig.setName("1234"); - tConfig.setLogin("1234"); - tConfig.setPassword("1234"); - tConfig.setEnabled(true); - tConfig.setSaveRaw(true); - tConfig.setDataFormat("1234"); - tConfig.setTtl(1234); - tConfig.setCorrelateClearedMessage(true); - tConfig.setMessageIdPath("1234"); - tConfig.setAggregateArrayPath("1234"); - tConfig.setFlattenArrayPath("1234"); - List sinkdbs = new ArrayList<>(); - sinkdbs.add(1234); - tConfig.setSinkdbs(sinkdbs); - - Db db = new Db(); - db.setId(1234); - - TopicName topicName = new TopicName(); - topicName.setId("1234"); - - Optional optional = Optional.of(topicName); - when(dbRepository.findById(1234)).thenReturn(Optional.of(db)); - when(topicNameRepository.findById(tConfig.getName())).thenReturn(optional); - - topicService.fillTopicConfiguration(tConfig); - } + static String DEFAULT_TOPIC_NAME = "_DL_DEFAULT_"; -/* - @Test - public void testGetEffectiveTopic() throws IOException { - String name = "a"; - Topic topic = new Topic(name); - topic.setEnabled(true); - Set dbSet = new HashSet<>(); - dbSet.add(new Db("Elasticsearch")); - topic.setDbs(dbSet); - - when(config.getDefaultTopicName()).thenReturn(DEFAULT_TOPIC_NAME); - when(topicRepository.findById(DEFAULT_TOPIC_NAME)).thenReturn(Optional.of(topic)); - when(topicRepository.findById(name)).thenReturn(Optional.of(topic)); - when(topicRepository.findById(null)).thenReturn(Optional.empty()); - - assertEquals(topicService.getEffectiveTopic(name), topicService.getEffectiveTopic(name, false)); - - assertNotNull(topicService.getEffectiveTopic(null)); - - topicService.getEffectiveTopic(name, true); - } -*/ + @Mock + private ApplicationConfiguration config; + + @Mock + private TopicRepository topicRepository; + + @Mock + private ElasticsearchService elasticsearchService; + + @Mock + private DbService dbService; + + @Mock + private DbRepository dbRepository; + + @Mock + private TopicNameRepository topicNameRepository; + + @Mock + private KafkaRepository kafkaRepository; + + @InjectMocks + private TopicService topicService; + + @Test(expected = NullPointerException.class) + public void testGetTopicException() throws IOException { + List < Topic > topics = new ArrayList < > (); + Topic topic = new Topic(); + DbType dbType = new DbType(); + Set < Kafka > kafkas = new HashSet < > (); + Set < Db > dbs = new HashSet < > (); + Db db = new Db(); + db.setName("Elasticsearch"); + dbs.add(db); + + dbType.setId("ES"); + db.setDbType(dbType); + + Kafka kafka = new Kafka(); + kafka.setName("1234"); + kafkas.add(kafka); + + TopicName topicName = new TopicName(); + topicName.setId("1234"); + + topic.setTopicName(topicName); + topic.setKafkas(kafkas); + topic.setEnabled(true); + topic.setDbs(dbs); + topics.add(topic); + when(topicRepository.findAll()).thenReturn(topics); + when((ElasticsearchService) dbService.findDbStoreService(db)).thenReturn(new ElasticsearchService(db)); + topicService.findTopics(kafka, topicName.getId()); + topicService.getEnabledEffectiveTopic(kafka, topicName.getId(), true); + + } + + @Test + public void testGetTopic() throws IOException { + ArrayList < Topic > topics = new ArrayList < > (); + Topic topic = new Topic(); + Set < Kafka > kafkas = new HashSet < > (); + Kafka kafka = new Kafka(); + kafka.setName("1234"); + kafkas.add(kafka); + TopicName topicName = new TopicName(); + topicName.setId(DEFAULT_TOPIC_NAME); + topic.setTopicName(topicName); + topic.setKafkas(kafkas); + topics.add(topic); + when(topicRepository.findAll()).thenReturn(topics); + when(config.getDefaultTopicName()).thenReturn("_DL_DEFAULT_"); + topicService.getEnabledEffectiveTopic(new Kafka(), "test", true); + } + + @Test + public void testFindTopics() { + ArrayList < Topic > topics = new ArrayList < > (); + Topic topic = new Topic(); + Set < Kafka > kafkas = new HashSet < > (); + Kafka kafka = new Kafka(); + kafka.setName("1234"); + kafkas.add(kafka); + TopicName topicName = new TopicName(); + topicName.setId(DEFAULT_TOPIC_NAME); + topic.setTopicName(topicName); + topic.setKafkas(kafkas); + topics.add(topic); + when(topicRepository.findAll()).thenReturn(topics); + topicService.findTopics(kafka, topicName.getId()); + } + + @Test + public void testGetTopicNull() { + Topic topic = new Topic(); + TopicName topicName = new TopicName(); + topicName.setId("_DL_DEFAULT_"); + topic.setId(1234); + topic.setTopicName(topicName); + Optional < Topic > optional = Optional.of(topic); + when(topicRepository.findById(0)).thenReturn(optional); + when(config.getDefaultTopicName()).thenReturn("_DL_DEFAULT_"); + assertEquals(topic, topicService.getTopic(0)); + assertTrue(topicService.isDefaultTopic(topic)); + } + + @Test + public void testFillTopic() { + TopicConfig tConfig = new TopicConfig(); + tConfig.setId(1234); + tConfig.setName("1234"); + tConfig.setLogin("1234"); + tConfig.setPassword("1234"); + tConfig.setEnabled(true); + tConfig.setSaveRaw(true); + tConfig.setDataFormat("1234"); + tConfig.setTtl(1234); + tConfig.setCorrelateClearedMessage(true); + tConfig.setMessageIdPath("1234"); + tConfig.setAggregateArrayPath("1234"); + tConfig.setFlattenArrayPath("1234"); + List < Integer > sinkdbs = new ArrayList < > (); + sinkdbs.add(1234); + tConfig.setSinkdbs(sinkdbs); + List < Integer > kafkas = new ArrayList < > (); + kafkas.add(1); + tConfig.setKafkas(kafkas); + + Db db = new Db(); + db.setId(1234); + + TopicName topicName = new TopicName(); + topicName.setId("1234"); + + Optional < TopicName > optional = Optional.of(topicName); + when(dbRepository.findById(1234)).thenReturn(Optional.of(db)); + when(kafkaRepository.findById(1)).thenReturn(Optional.of(new Kafka())); + when(topicNameRepository.findById(tConfig.getName())).thenReturn(optional); + topicService.fillTopicConfiguration(tConfig); + } + + @Test + public void testGetDefaultTopicFromFeeder() { + when(topicRepository.findByTopicName_Id(config.getDefaultTopicName())).thenReturn(new Topic()); + topicService.getDefaultTopicFromFeeder(); + } + + @Test + public void testGetDefaultTopic() { + List < Topic > topics = new ArrayList < > (); + Topic topic = new Topic(); + DbType dbType = new DbType(); + Set < Kafka > kafkas = new HashSet < > (); + Set < Db > dbs = new HashSet < > (); + Db db = new Db(); + db.setName("Elasticsearch"); + dbs.add(db); + + dbType.setId("ES"); + db.setDbType(dbType); + + Kafka kafka = new Kafka(); + kafka.setName("1234"); + kafkas.add(kafka); + + TopicName topicName = new TopicName(); + topicName.setId(DEFAULT_TOPIC_NAME); + topic.setTopicName(topicName); + topic.setKafkas(kafkas); + topic.setEnabled(true); + topic.setDbs(dbs); + topics.add(topic); + when(config.getDefaultTopicName()).thenReturn(DEFAULT_TOPIC_NAME); + when(topicRepository.findAll()).thenReturn(topics); + topicService.getDefaultTopic(kafka); + } + + @Test + public void testIsDefaultTopic() { + assertEquals(false, topicService.isDefaultTopic(null)); + } + + /* + @Test + public void testGetEffectiveTopic() throws IOException { + String name = "a"; + Topic topic = new Topic(name); + topic.setEnabled(true); + Set dbSet = new HashSet<>(); + dbSet.add(new Db("Elasticsearch")); + topic.setDbs(dbSet); + + when(config.getDefaultTopicName()).thenReturn(DEFAULT_TOPIC_NAME); + when(topicRepository.findById(DEFAULT_TOPIC_NAME)).thenReturn(Optional.of(topic)); + when(topicRepository.findById(name)).thenReturn(Optional.of(topic)); + when(topicRepository.findById(null)).thenReturn(Optional.empty()); + + assertEquals(topicService.getEffectiveTopic(name), topicService.getEffectiveTopic(name, false)); + + assertNotNull(topicService.getEffectiveTopic(null)); + + topicService.getEffectiveTopic(name, true); + } + */ } diff --git a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/db/CouchbaseServiceTest.java b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/db/CouchbaseServiceTest.java old mode 100755 new mode 100644 index 2a7745b4..0abee7fc --- a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/db/CouchbaseServiceTest.java +++ b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/db/CouchbaseServiceTest.java @@ -3,6 +3,7 @@ * ONAP : DATALAKE * ================================================================================ * Copyright (C) 2018-2019 Huawei. All rights reserved. + * Copyright (C) 2022 Wipro Limited. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,8 +21,12 @@ package org.onap.datalake.feeder.service.db; +import static org.mockito.Mockito.when; + +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; +import java.util.concurrent.locks.ReentrantReadWriteLock; import org.jetbrains.annotations.NotNull; import org.json.JSONObject; @@ -29,6 +34,8 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; import org.onap.datalake.feeder.config.ApplicationConfiguration; import org.onap.datalake.feeder.domain.Db; @@ -46,111 +53,129 @@ import com.couchbase.mock.client.MockClient; @RunWith(MockitoJUnitRunner.class) public class CouchbaseServiceTest { - protected final BucketConfiguration bucketConfiguration = new BucketConfiguration(); - protected MockClient mockClient; - protected CouchbaseMock couchbaseMock; - protected Cluster cluster; - protected com.couchbase.client.java.Bucket bucket; - protected int carrierPort; - protected int httpPort; - - protected void getPortInfo(String bucket) throws Exception { - httpPort = couchbaseMock.getHttpPort(); - carrierPort = couchbaseMock.getCarrierPort(bucket); - } - - protected void createMock(@NotNull String name, @NotNull String password) throws Exception { - bucketConfiguration.numNodes = 1; - bucketConfiguration.numReplicas = 1; - bucketConfiguration.numVBuckets = 1024; - bucketConfiguration.name = name; - bucketConfiguration.type = Bucket.BucketType.COUCHBASE; - bucketConfiguration.password = password; - ArrayList configList = new ArrayList(); - configList.add(bucketConfiguration); - couchbaseMock = new CouchbaseMock(0, configList); - couchbaseMock.start(); - couchbaseMock.waitForStartup(); - } - - protected void createClient() { - cluster = CouchbaseCluster.create(DefaultCouchbaseEnvironment.builder().bootstrapCarrierDirectPort(carrierPort).bootstrapHttpDirectPort(httpPort).build(), "couchbase://127.0.0.1"); - bucket = cluster.openBucket("default"); - } - - @Before - public void setUp() throws Exception { - createMock("default", ""); - getPortInfo("default"); - createClient(); - } - - @After - public void tearDown() { - if (cluster != null) { - cluster.disconnect(); - } - if (couchbaseMock != null) { - couchbaseMock.stop(); - } - if (mockClient != null) { - mockClient.shutdown(); - } - } - - @Test - public void testSaveJsonsWithTopicId() { - ApplicationConfiguration appConfig = new ApplicationConfiguration(); - appConfig.setTimestampLabel("datalake_ts_"); - - String text = "{ data: { data2 : { value : 'hello'}}}"; - - JSONObject json = new JSONObject(text); - - Topic topic = TestUtil.newTopic("test getMessageId"); - topic.setMessageIdPath("/data/data2/value"); - List jsons = new ArrayList<>(); - json.put(appConfig.getTimestampLabel(), 1234); - jsons.add(json); - CouchbaseService couchbaseService = new CouchbaseService(new Db()); - couchbaseService.bucket = bucket; - couchbaseService.config = appConfig; - - couchbaseService.init(); - EffectiveTopic effectiveTopic = new EffectiveTopic(topic, "test"); - couchbaseService.saveJsons(effectiveTopic, jsons); - - } - - @Test - public void testSaveJsonsWithOutTopicId() { - ApplicationConfiguration appConfig = new ApplicationConfiguration(); - appConfig.setTimestampLabel("datalake_ts_"); - - String text = "{ data: { data2 : { value : 'hello'}}}"; - - JSONObject json = new JSONObject(text); - - Topic topic = TestUtil.newTopic("test getMessageId"); - List jsons = new ArrayList<>(); - json.put(appConfig.getTimestampLabel(), 1234); - jsons.add(json); - CouchbaseService couchbaseService = new CouchbaseService(new Db()); - couchbaseService.bucket = bucket; - couchbaseService.config = appConfig; - - couchbaseService.init(); - EffectiveTopic effectiveTopic = new EffectiveTopic(topic, "test"); - couchbaseService.saveJsons(effectiveTopic, jsons); - } - - @Test - public void testCleanupBucket() { - // CouchbaseService couchbaseService = new CouchbaseService(new Db()); - // couchbaseService.bucket = bucket; - // ApplicationConfiguration appConfig = new ApplicationConfiguration(); - // couchbaseService.config = appConfig; - // couchbaseService.cleanUp(); - } - -} \ No newline at end of file + protected final BucketConfiguration bucketConfiguration = new BucketConfiguration(); + protected MockClient mockClient; + protected CouchbaseMock couchbaseMock; + protected Cluster cluster; + protected com.couchbase.client.java.Bucket bucket; + protected int carrierPort; + protected int httpPort; + + @InjectMocks + private CouchbaseService couchbaseService; + + @Mock + private ApplicationConfiguration config; + + @Before + public void init() throws NoSuchFieldException, IllegalAccessException { + Db db = TestUtil.newDb("Couchbasedb"); + db.setDatabase("database"); + db.setLogin("login"); + couchbaseService = new CouchbaseService(db); + + Field configField = CouchbaseService.class.getDeclaredField("config"); + configField.setAccessible(true); + configField.set(couchbaseService, config); + couchbaseService.bucket = bucket; + couchbaseService.init(); + } + + protected void getPortInfo(String bucket) throws Exception { + httpPort = couchbaseMock.getHttpPort(); + carrierPort = couchbaseMock.getCarrierPort(bucket); + } + + protected void createMock(@NotNull String name, @NotNull String password) throws Exception { + bucketConfiguration.numNodes = 1; + bucketConfiguration.numReplicas = 1; + bucketConfiguration.numVBuckets = 1024; + bucketConfiguration.name = name; + bucketConfiguration.type = Bucket.BucketType.COUCHBASE; + bucketConfiguration.password = password; + ArrayList < BucketConfiguration > configList = new ArrayList < BucketConfiguration > (); + configList.add(bucketConfiguration); + couchbaseMock = new CouchbaseMock(0, configList); + couchbaseMock.start(); + couchbaseMock.waitForStartup(); + } + + protected void createClient() { + cluster = CouchbaseCluster.create(DefaultCouchbaseEnvironment.builder().bootstrapCarrierDirectPort(carrierPort) + .bootstrapHttpDirectPort(httpPort).build(), "couchbase://127.0.0.1"); + bucket = cluster.openBucket("default"); + } + + @Before + public void setUp() throws Exception { + createMock("default", ""); + getPortInfo("default"); + createClient(); + } + + @After + public void tearDown() { + if (cluster != null) { + cluster.disconnect(); + } + if (couchbaseMock != null) { + couchbaseMock.stop(); + } + if (mockClient != null) { + mockClient.shutdown(); + } + } + + @Test + public void testSaveJsonsWithTopicId() { + ApplicationConfiguration appConfig = new ApplicationConfiguration(); + appConfig.setTimestampLabel("datalake_ts_"); + + String text = "{ data: { data2 : { value : 'hello'}}}"; + + JSONObject json = new JSONObject(text); + + Topic topic = TestUtil.newTopic("test getMessageId"); + topic.setMessageIdPath("/data/data2/value"); + List < JSONObject > jsons = new ArrayList < > (); + json.put(appConfig.getTimestampLabel(), 1234); + jsons.add(json); + CouchbaseService couchbaseService = new CouchbaseService(new Db()); + couchbaseService.bucket = bucket; + couchbaseService.config = appConfig; + + couchbaseService.init(); + EffectiveTopic effectiveTopic = new EffectiveTopic(topic, "test"); + couchbaseService.saveJsons(effectiveTopic, jsons); + + } + + @Test + public void testSaveJsonsWithOutTopicId() { + ApplicationConfiguration appConfig = new ApplicationConfiguration(); + appConfig.setTimestampLabel("datalake_ts_"); + + String text = "{ data: { data2 : { value : 'hello'}}}"; + + JSONObject json = new JSONObject(text); + + Topic topic = TestUtil.newTopic("test getMessageId"); + List < JSONObject > jsons = new ArrayList < > (); + json.put(appConfig.getTimestampLabel(), 1234); + jsons.add(json); + CouchbaseService couchbaseService = new CouchbaseService(new Db()); + couchbaseService.bucket = bucket; + couchbaseService.config = appConfig; + + couchbaseService.init(); + EffectiveTopic effectiveTopic = new EffectiveTopic(topic, "test"); + couchbaseService.saveJsons(effectiveTopic, jsons); + } + + @Test + public void testCleanupBucket() { + when(config.getShutdownLock()).thenReturn(new ReentrantReadWriteLock()); + couchbaseService.cleanUp(); + } + +} diff --git a/components/datalake-handler/pom.xml b/components/datalake-handler/pom.xml index 2a45c023..0db38d14 100644 --- a/components/datalake-handler/pom.xml +++ b/components/datalake-handler/pom.xml @@ -1,267 +1,280 @@ + - 4.0.0 - - - org.onap.oparent - oparent - 2.0.0 - - - org.onap.dcaegen2.services.components - datalake-handler - 1.1.1-SNAPSHOT - pom - - dcaegen2-service-datalake-handler - - - feeder - admin - collector - des - - - - UTF-8 - UTF-8 - 11 - - 3.10.1 - 2.1.0.RELEASE - 3.1.2.RELEASE - 2.9.8 - 2.0.0 - 7.1.1 - 3.2.0 - - - ${project.reporting.outputDirectory}/jacoco-ut/jacoco.xml - - - - - - - - org.apache.hadoop - hadoop-client - ${hadoop.version} - - - - org.mariadb.jdbc - mariadb-java-client - 2.4.1 - - - - commons-io - commons-io - 2.6 - - - - com.fasterxml.jackson.core - jackson-core - ${jackson.version} - - - - com.fasterxml.jackson.core - jackson-annotations - ${jackson.version} - - - - com.fasterxml.jackson.core - jackson-databind - ${jackson.version} - - - - com.fasterxml.jackson.dataformat - jackson-dataformat-yaml - ${jackson.version} - - - - com.fasterxml.jackson.dataformat - jackson-dataformat-xml - ${jackson.version} - - - - com.google.code.gson - gson - 2.8.2 - - - - io.druid - tranquility-core_2.11 - 0.8.3 - - - - javax.servlet - javax.servlet-api - 4.0.1 - - - - - org.apache.kafka - kafka-clients - ${kafka.version} - - - - org.apache.velocity - velocity-engine-core - 2.0 - - - - org.json - json - 20180813 - - - - org.mongodb - mongo-java-driver - ${mongojava.version} - - - - org.projectlombok - lombok - 1.18.2 - - - - org.springframework.data - spring-data-couchbase - ${springcouchbase.version} - - - - org.springframework.boot - spring-boot-starter-web - ${springboot.version} - - - - - org.springframework.boot - spring-boot-starter-actuator - ${springboot.version} - - - - - org.springframework.boot - spring-boot-starter-data-jpa - ${springboot.version} - - - - org.springframework.boot - spring-boot-starter-data-couchbase - ${springboot.version} - - - - org.springframework.boot - spring-boot-starter-data-mongodb - ${springboot.version} - - - - org.springframework.boot - spring-boot-starter-data-rest - ${springboot.version} - - - - org.springframework.boot - spring-boot-starter-test - ${springboot.version} - test - - - - - org.springframework.boot - spring-boot-configuration-processor - ${springboot.version} - true - - - - org.elasticsearch.client - elasticsearch-rest-high-level-client - ${elasticsearchjava.version} - - - - - - - - - org.springframework.boot - spring-boot-maven-plugin - ${springboot.version} - - - - repackage - - - - - - - maven-failsafe-plugin - - - - integration-test - verify - - - - - - - org.apache.maven.plugins - maven-eclipse-plugin - - true - true - - - - - org.apache.maven.plugins - maven-deploy-plugin - 2.8 - - true - - - - - org.apache.maven.plugins - maven-compiler-plugin - 3.8.0 - - - - + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + + + org.onap.oparent + oparent + 2.0.0 + + + org.onap.dcaegen2.services.components + datalake-handler + 1.1.2-SNAPSHOT + pom + + dcaegen2-service-datalake-handler + + + feeder + admin + des + + + + UTF-8 + UTF-8 + 11 + + 3.10.1 + 2.1.0.RELEASE + 3.1.2.RELEASE + 2.9.8 + 2.0.0 + 7.1.1 + 3.2.0 + + + ${project.reporting.outputDirectory}/jacoco-ut/jacoco.xml + + + + + + + + org.apache.hadoop + hadoop-client + ${hadoop.version} + + + + org.mariadb.jdbc + mariadb-java-client + 2.4.1 + + + + commons-io + commons-io + 2.6 + + + + com.fasterxml.jackson.core + jackson-core + ${jackson.version} + + + + com.fasterxml.jackson.core + jackson-annotations + ${jackson.version} + + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + + com.fasterxml.jackson.dataformat + jackson-dataformat-yaml + ${jackson.version} + + + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + ${jackson.version} + + + + com.google.code.gson + gson + 2.8.2 + + + + io.druid + tranquility-core_2.11 + 0.8.3 + + + + javax.servlet + javax.servlet-api + 4.0.1 + + + + + org.apache.kafka + kafka-clients + ${kafka.version} + + + + org.apache.velocity + velocity-engine-core + 2.0 + + + + org.json + json + 20180813 + + + + org.mongodb + mongo-java-driver + ${mongojava.version} + + + + org.projectlombok + lombok + 1.18.2 + + + + org.springframework.data + spring-data-couchbase + ${springcouchbase.version} + + + + org.springframework.boot + spring-boot-starter-web + ${springboot.version} + + + + + org.springframework.boot + spring-boot-starter-actuator + ${springboot.version} + + + + + org.springframework.boot + spring-boot-starter-data-jpa + ${springboot.version} + + + + org.springframework.boot + spring-boot-starter-data-couchbase + ${springboot.version} + + + + org.springframework.boot + spring-boot-starter-data-mongodb + ${springboot.version} + + + + org.springframework.boot + spring-boot-starter-data-rest + ${springboot.version} + + + + org.springframework.boot + spring-boot-starter-test + ${springboot.version} + test + + + + + org.springframework.boot + spring-boot-configuration-processor + ${springboot.version} + true + + + + org.elasticsearch.client + elasticsearch-rest-high-level-client + ${elasticsearchjava.version} + + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + ${springboot.version} + + + + repackage + + + + + + + maven-failsafe-plugin + + + + integration-test + verify + + + + + + + org.apache.maven.plugins + maven-eclipse-plugin + + true + true + + + + + org.apache.maven.plugins + maven-deploy-plugin + 2.8 + + true + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.0 + + + + diff --git a/components/datalake-handler/version.properties b/components/datalake-handler/version.properties index 9461aa95..e6eb586c 100644 --- a/components/datalake-handler/version.properties +++ b/components/datalake-handler/version.properties @@ -1,6 +1,6 @@ major=1 minor=1 -patch=1 +patch=2 base_version=${major}.${minor}.${patch} release_version=${base_version} snapshot_version=${base_version}-SNAPSHOT -- cgit 1.2.3-korg