diff options
Diffstat (limited to 'components/datalake-handler/feeder/src')
21 files changed, 1288 insertions, 604 deletions
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<Kafka> ret = kafkaRepository.findById(id); + Optional < Kafka > ret = kafkaRepository.findById(id); return ret.isPresent() ? ret.get() : null; } - public List<KafkaConfig> getAllKafka() { + public List < KafkaConfig > getAllKafka() { - List<KafkaConfig> kafkaConfigList = new ArrayList<>(); - Iterable<Kafka> 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; @@ -62,11 +67,20 @@ public class DbControllerTest { 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<DbConfig> 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<DbConfig> 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<Db> dbs = new ArrayList<>(); + List < Db > dbs = new ArrayList < > (); dbs.add(TestUtil.newDb(name)); setAccessPrivateFields(dbController); when(dbRepository.findAll()).thenReturn(dbs); - List<Integer> list = dbController.list(); - for (int id : list) { + 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<Topic> topics = new HashSet<>(); + Set < Topic > topics = new HashSet < > (); topics.add(topic); Db db1 = TestUtil.newDb(dbName); db1.setTopics(topics); setAccessPrivateFields(dbController); - Set<Topic> elecsticsearch = dbController.getDbTopics(dbName, httpServletResponse); + 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<DbConfig> 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<DbConfig> 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<DesignConfig> 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<DesignConfig> 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<Design> 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<String> 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<TopicConfig> postTopic = topicController.createTopic(ac, mockBindingResult, httpServletResponse); - assertEquals(postTopic.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); - } - } } 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,11 +26,27 @@ 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 { @@ -37,14 +54,26 @@ public class DesignServiceTest { 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<DesignType> 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<Puller> 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<String> activeTopics = new HashSet<>(Arrays.asList("test")); - Map<Integer, Set<String>> 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<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 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); - - 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(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<Db> 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<Db> 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 index 2a7745b4..0abee7fc 100755..100644 --- 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<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() { - // 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(); + } + +} |