diff options
author | Guobiao Mo <guobiaomo@chinamobile.com> | 2019-04-16 23:54:45 -0700 |
---|---|---|
committer | Guobiao Mo <guobiaomo@chinamobile.com> | 2019-04-17 11:17:02 -0700 |
commit | da4ba6e2c81f3dd7157e1cc06a92e91c5a38aa95 (patch) | |
tree | 44e548cdd80ac09f085400e9bdc102bc59f67749 /components | |
parent | 6a104760c87139a7cfac5a23b9b6b67e90822134 (diff) |
Unit test to improve sonar coverage
Issue-ID: DCAEGEN2-1309
Change-Id: Ia3651bb105d92013d6d2a9d60fffe3b6066adf64
Signed-off-by: Guobiao Mo <guobiaomo@chinamobile.com>
Diffstat (limited to 'components')
16 files changed, 286 insertions, 52 deletions
diff --git a/components/datalake-handler/feeder/src/main/java/com/mongodb/internal/validator/CollectibleDocumentFieldNameValidator.java b/components/datalake-handler/feeder/src/main/java/com/mongodb/internal/validator/CollectibleDocumentFieldNameValidator.java index 2bc6faad..e7a8e1b9 100644 --- a/components/datalake-handler/feeder/src/main/java/com/mongodb/internal/validator/CollectibleDocumentFieldNameValidator.java +++ b/components/datalake-handler/feeder/src/main/java/com/mongodb/internal/validator/CollectibleDocumentFieldNameValidator.java @@ -8,7 +8,7 @@ package com.mongodb.internal.validator; * ================================================================================
* Copyright 2018 China Mobile
*=================================================================================
-* Licensed under the Apache License, Version 2.0 (the "License");
+* 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
*
diff --git a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/domain/Topic.java b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/domain/Topic.java index 20ebf94a..65478702 100644 --- a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/domain/Topic.java +++ b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/domain/Topic.java @@ -100,7 +100,7 @@ public class Topic { @Column(name = "correlate_cleared_message") private Boolean correlateClearedMessage; - //the value in the JSON with this path will be used as DB id + //paths to the values in the JSON that are used to composite DB id, comma separated, example: "/event-header/id,/event-header/entity-type,/entity/product-name" @Column(name = "message_id_path") private String messageIdPath; diff --git a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/CouchbaseService.java b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/CouchbaseService.java index a63a927c..01c908ef 100644 --- a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/CouchbaseService.java +++ b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/CouchbaseService.java @@ -70,7 +70,7 @@ public class CouchbaseService { cluster.authenticate(couchbase.getLogin(), couchbase.getPass()); bucket = cluster.openBucket(couchbase.getDatabase()); - log.info("Connect to Couchbase " + couchbase.getHost()); + log.info("Connect to Couchbase {}", couchbase.getHost()); // Create a N1QL Primary Index (but ignore if it exists) bucket.bucketManager().createN1qlPrimaryIndex(true, false); diff --git a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/DbService.java b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/DbService.java index f0d943d3..e859270f 100644 --- a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/DbService.java +++ b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/DbService.java @@ -24,8 +24,6 @@ import java.util.Optional; import org.onap.datalake.feeder.domain.Db; import org.onap.datalake.feeder.repository.DbRepository; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -38,8 +36,6 @@ import org.springframework.stereotype.Service; @Service public class DbService { - private final Logger log = LoggerFactory.getLogger(this.getClass()); - @Autowired private DbRepository dbRepository; diff --git a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/ElasticsearchService.java b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/ElasticsearchService.java index fea07187..2c16b2b8 100644 --- a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/ElasticsearchService.java +++ b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/ElasticsearchService.java @@ -27,7 +27,6 @@ import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import org.apache.http.HttpHost; -import org.elasticsearch.ElasticsearchStatusException; import org.elasticsearch.action.ActionListener; import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.client.indices.CreateIndexResponse; @@ -69,6 +68,9 @@ public class ElasticsearchService { private RestHighLevelClient client; ActionListener<BulkResponse> listener; + +//ES Encrypted communication https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/_encrypted_communication.html#_encrypted_communication +//Basic authentication https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/_basic_authentication.html @PostConstruct private void init() { Db elasticsearch = dbService.getElasticsearch(); @@ -77,7 +79,7 @@ public class ElasticsearchService { // Initialize the Connection client = new RestHighLevelClient(RestClient.builder(new HttpHost(elasticsearchHost, 9200, "http"), new HttpHost(elasticsearchHost, 9201, "http"))); - log.info("Connect to Elasticsearch Host " + elasticsearchHost); + log.info("Connect to Elasticsearch Host {}", elasticsearchHost); listener = new ActionListener<BulkResponse>() { @Override @@ -106,7 +108,7 @@ public class ElasticsearchService { if(!exists){ CreateIndexRequest createIndexRequest = new CreateIndexRequest(topicLower); CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT); - log.info(createIndexResponse.index()+" : created "+createIndexResponse.isAcknowledged()); + log.info("{} : created {}", createIndexResponse.index(), createIndexResponse.isAcknowledged()); } } diff --git a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/PullService.java b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/PullService.java index 4433c8c4..48d167b5 100644 --- a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/PullService.java +++ b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/PullService.java @@ -49,10 +49,10 @@ public class PullService { private boolean isRunning = false; private ExecutorService executorService; private List<PullThread> consumers; - + @Autowired private ApplicationContext context; - + @Autowired private ApplicationConfiguration config; @@ -62,13 +62,13 @@ public class PullService { public boolean isRunning() { return isRunning; } - + /** * start pulling. * * @throws IOException */ - public synchronized void start() throws IOException { + public synchronized void start() { if (isRunning) { return; } @@ -80,15 +80,15 @@ public class PullService { executorService = Executors.newFixedThreadPool(numConsumers); consumers = new ArrayList<>(numConsumers); - for (int i = 0; i < numConsumers; i++) { + for (int i = 0; i < numConsumers; i++) { PullThread puller = context.getBean(PullThread.class, i); consumers.add(puller); executorService.submit(puller); } isRunning = true; - - Runtime.getRuntime().addShutdownHook(new Thread(()->shutdown())) ; + + Runtime.getRuntime().addShutdownHook(new Thread(this::shutdown)); } /** @@ -103,16 +103,16 @@ public class PullService { for (PullThread puller : consumers) { puller.shutdown(); } - + executorService.shutdown(); - + try { executorService.awaitTermination(10L, TimeUnit.SECONDS); } catch (InterruptedException e) { logger.error("executor.awaitTermination", e); Thread.currentThread().interrupt(); } - + isRunning = false; } diff --git a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/TopicService.java b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/TopicService.java index cd5113cc..7e0c7780 100644 --- a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/TopicService.java +++ b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/TopicService.java @@ -23,9 +23,6 @@ package org.onap.datalake.feeder.service; import java.io.IOException; import java.util.Optional; -import javax.annotation.PostConstruct; -import javax.annotation.PreDestroy; - import org.onap.datalake.feeder.domain.Topic; import org.onap.datalake.feeder.repository.TopicRepository; import org.slf4j.Logger; @@ -50,14 +47,6 @@ public class TopicService { @Autowired private ElasticsearchService elasticsearchService; - @PostConstruct - private void init() { - } - - @PreDestroy - public void cleanUp() { - } - public Topic getEffectiveTopic(String topicStr) { try { return getEffectiveTopic(topicStr, false); diff --git a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/util/Util.java b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/util/Util.java index 89e81af1..aada44bc 100644 --- a/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/util/Util.java +++ b/components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/util/Util.java @@ -55,11 +55,4 @@ public class Util { return replaceDotInKey(newJson);// there maybe more to replace } } - - public static void main(String[] args) { - String a = "\"u-y.t.y-t\":\"u.gfh\",\\\"jg.h\\\":\"j_9889\""; - String b = replaceDotInKey(a); - System.out.println(a); - System.out.println(b); - } } diff --git a/components/datalake-handler/feeder/src/test/java/com/mongodb/internal/validator/CollectibleDocumentFieldNameValidatorTest.java b/components/datalake-handler/feeder/src/test/java/com/mongodb/internal/validator/CollectibleDocumentFieldNameValidatorTest.java new file mode 100644 index 00000000..dd55a26f --- /dev/null +++ b/components/datalake-handler/feeder/src/test/java/com/mongodb/internal/validator/CollectibleDocumentFieldNameValidatorTest.java @@ -0,0 +1,52 @@ +package com.mongodb.internal.validator;
+
+/*
+* ============LICENSE_START=======================================================
+* ONAP : DataLake
+* ================================================================================
+* Copyright 2018 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=========================================================
+*/
+import org.junit.Test;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * test CollectibleDocumentFieldNameValidator
+ *
+ * @author Guobiao Mo
+ *
+ */
+public class CollectibleDocumentFieldNameValidatorTest {
+ @Test
+ public void validate() {
+ CollectibleDocumentFieldNameValidator validator = new CollectibleDocumentFieldNameValidator();
+
+ assertTrue(validator.validate("$id"));
+ assertFalse(validator.validate("$abc"));
+ assertTrue(validator.validate("abc.abc"));
+
+ assertTrue(validator == validator.getValidatorForField("any"));
+
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void validateNull() {
+ CollectibleDocumentFieldNameValidator validator = new CollectibleDocumentFieldNameValidator();
+
+ assertTrue(validator.validate(null));
+ }
+}
diff --git a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/ApplicationTest.java b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/ApplicationTest.java index 7513249f..b94211e9 100644 --- a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/ApplicationTest.java +++ b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/ApplicationTest.java @@ -25,8 +25,7 @@ import org.onap.datalake.feeder.service.PullService; public class ApplicationTest { @Test - //only dot(.) in key got replaced - public void replaceDotInKey() { + public void testRunner() { Application application = new Application(); application.commandLineRunner(new PullService()); } diff --git a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/config/ApplicationConfigurationTest.java b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/config/ApplicationConfigurationTest.java index 3f1f37c1..ee3ceb7b 100644 --- a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/config/ApplicationConfigurationTest.java +++ b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/config/ApplicationConfigurationTest.java @@ -60,6 +60,7 @@ public class ApplicationConfigurationTest { assertTrue(config.getKafkaConsumerCount() > 0); assertNotNull(config.isAsync()); + assertNotNull(config.isEnableSSL()); } } diff --git a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/domain/TopicTest.java b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/domain/TopicTest.java index e21773cb..afd4503d 100644 --- a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/domain/TopicTest.java +++ b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/domain/TopicTest.java @@ -27,6 +27,7 @@ import java.util.HashSet; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; /** @@ -61,9 +62,11 @@ public class TopicTest { topic.setMessageIdPath("/data/data2/value,/data/data3"); String value = topic.getMessageId(json); - assertEquals(value, "hello^world"); + topic.setMessageIdPath(""); + assertNull(topic.getMessageId(json)); + Topic defaultTopic = new Topic("_DL_DEFAULT_"); Topic testTopic = new Topic("test"); testTopic.setDefaultTopic(defaultTopic); diff --git a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/DbServiceTest.java b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/DbServiceTest.java new file mode 100644 index 00000000..4948001f --- /dev/null +++ b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/DbServiceTest.java @@ -0,0 +1,94 @@ +/* +* ============LICENSE_START======================================================= +* ONAP : DATALAKE +* ================================================================================ +* Copyright 2019 China Mobile +*================================================================================= +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* ============LICENSE_END========================================================= +*/ + +package org.onap.datalake.feeder.service; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.when; + +import java.util.Optional; + +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.Db; +import org.onap.datalake.feeder.repository.DbRepository; + +/** + * Test Service for Dbs + * + * @author Guobiao Mo + * + */ +@RunWith(MockitoJUnitRunner.class) +public class DbServiceTest { + + @Mock + private DbRepository dbRepository; + + @InjectMocks + private DbService dbService; + + @Test + public void testGetDb() { + String name = "a"; + when(dbRepository.findById(name)).thenReturn(Optional.of(new Db(name))); + assertEquals(dbService.getDb(name), new Db(name)); + } + + @Test + public void testGetDbNull() { + String name = null; + when(dbRepository.findById(name)).thenReturn(Optional.empty()); + assertNull(dbService.getDb(name)); + } + + @Test + public void testGetCouchbase() { + String name = "Couchbase"; + when(dbRepository.findById(name)).thenReturn(Optional.of(new Db(name))); + assertEquals(dbService.getCouchbase(), new Db(name)); + } + + @Test + public void testGetElasticsearch() { + String name = "Elasticsearch"; + when(dbRepository.findById(name)).thenReturn(Optional.of(new Db(name))); + assertEquals(dbService.getElasticsearch(), new Db(name)); + } + + @Test + public void testGetMongoDB() { + String name = "MongoDB"; + when(dbRepository.findById(name)).thenReturn(Optional.of(new Db(name))); + assertEquals(dbService.getMongoDB(), new Db(name)); + } + + @Test + public void testGetDruid() { + String name = "Druid"; + when(dbRepository.findById(name)).thenReturn(Optional.of(new Db(name))); + assertEquals(dbService.getDruid(), new Db(name)); + } + +} 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 new file mode 100644 index 00000000..0e6db836 --- /dev/null +++ b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/TopicServiceTest.java @@ -0,0 +1,101 @@ +/* +* ============LICENSE_START======================================================= +* ONAP : DATALAKE +* ================================================================================ +* Copyright 2019 China Mobile +*================================================================================= +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* ============LICENSE_END========================================================= +*/ + +package org.onap.datalake.feeder.service; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.util.HashSet; +import java.util.Optional; +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.Db; +import org.onap.datalake.feeder.domain.Topic; +import org.onap.datalake.feeder.repository.TopicRepository; + +/** + * Test Service for Topic + * + * @author Guobiao Mo + * + */ +@RunWith(MockitoJUnitRunner.class) +public class TopicServiceTest { + + @Mock + private TopicRepository topicRepository; + + @Mock + private ElasticsearchService elasticsearchService; + + @InjectMocks + private TopicService topicService; + + @Test + public void testGetTopic() { + String name = "a"; + when(topicRepository.findById(name)).thenReturn(Optional.of(new Topic(name))); + assertEquals(topicService.getTopic(name), new Topic(name)); + } + + @Test + public void testGetTopicNull() { + String name = null; + when(topicRepository.findById(name)).thenReturn(Optional.empty()); + assertNull(topicService.getTopic(name)); + } + + @Test + public void testGetDefaultTopic() { + String name = "_DL_DEFAULT_"; + when(topicRepository.findById(name)).thenReturn(Optional.of(new Topic(name))); + assertEquals(topicService.getDefaultTopic(), new Topic(name)); + } + + @Test(expected = IOException.class) + 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(topicRepository.findById(name)).thenReturn(Optional.of(topic)); + when(topicRepository.findById(null)).thenReturn(Optional.empty()); + doThrow(IOException.class).when(elasticsearchService).ensureTableExist(name); + + 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/util/UtilTest.java b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/util/UtilTest.java index dc318ffb..918c0701 100644 --- a/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/util/UtilTest.java +++ b/components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/util/UtilTest.java @@ -33,15 +33,19 @@ import static org.junit.Assert.assertEquals; */ public class UtilTest { - @Test - //only dot(.) in key got replaced - public void replaceDotInKey() { - String a = "\"u-y.t.y-t\":\"u.gfh\",\\\"jg.h\\\":\"j_9889\""; - String b = "\"u-y_t_y-t\":\"u.gfh\",\\\"jg_h\\\":\"j_9889\""; + @Test + //only dot(.) in key got replaced + public void replaceDotInKey() { + new Util(); - assertEquals(Util.replaceDotInKey(a), b); + String a = "\"u-y.t.y-t\":\"u.gfh\",\\\"jg.h\\\":\"j_9889\""; + String b = "\"u-y_t_y-t\":\"u.gfh\",\\\"jg_h\\\":\"j_9889\""; - String[] strArray2 = {"test1", "test2", "test3"}; - Util.main(strArray2); - } + assertEquals(Util.replaceDotInKey(a), b); + } + + @Test(expected = IOException.class) + public void validateNull() throws IOException { + Util.getTextFromFile("no_such_file"); + } } diff --git a/components/datalake-handler/pom.xml b/components/datalake-handler/pom.xml index ee53ab3d..7f0bb147 100644 --- a/components/datalake-handler/pom.xml +++ b/components/datalake-handler/pom.xml @@ -27,9 +27,9 @@ <java.version>1.8</java.version> <mongojava.version>3.10.1</mongojava.version> - <springboot.version>2.1.0.RELEASE</springboot.version> + <springboot.version>2.1.4.RELEASE</springboot.version> <springcouchbase.version>3.1.2.RELEASE</springcouchbase.version> - <jackson.version>2.9.6</jackson.version> + <jackson.version>2.9.8</jackson.version> <kafka.version>2.0.0</kafka.version> <elasticsearchjava.version>7.0.0</elasticsearchjava.version> |