Unit test to improve sonar coverage 34/85534/4
authorGuobiao Mo <guobiaomo@chinamobile.com>
Wed, 17 Apr 2019 06:54:45 +0000 (23:54 -0700)
committerGuobiao Mo <guobiaomo@chinamobile.com>
Wed, 17 Apr 2019 18:17:02 +0000 (11:17 -0700)
Issue-ID: DCAEGEN2-1309
Change-Id: Ia3651bb105d92013d6d2a9d60fffe3b6066adf64
Signed-off-by: Guobiao Mo <guobiaomo@chinamobile.com>
16 files changed:
components/datalake-handler/feeder/src/main/java/com/mongodb/internal/validator/CollectibleDocumentFieldNameValidator.java
components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/domain/Topic.java
components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/CouchbaseService.java
components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/DbService.java
components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/ElasticsearchService.java
components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/PullService.java
components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/TopicService.java
components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/util/Util.java
components/datalake-handler/feeder/src/test/java/com/mongodb/internal/validator/CollectibleDocumentFieldNameValidatorTest.java [new file with mode: 0644]
components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/ApplicationTest.java
components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/config/ApplicationConfigurationTest.java
components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/domain/TopicTest.java
components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/DbServiceTest.java [new file with mode: 0644]
components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/TopicServiceTest.java [new file with mode: 0644]
components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/util/UtilTest.java
components/datalake-handler/pom.xml

index 2bc6faa..e7a8e1b 100644 (file)
@@ -8,7 +8,7 @@ package com.mongodb.internal.validator;
 * ================================================================================\r
 * Copyright 2018 China Mobile\r
 *=================================================================================\r
-* Licensed under the Apache License, Version 2.0 (the "License");\r
+* Licensed under the Apache License, Version 2.0 (the "License")\r
 * you may not use this file except in compliance with the License.\r
 * You may obtain a copy of the License at\r
 *\r
index 20ebf94..6547870 100644 (file)
@@ -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;
 
index a63a927..01c908e 100644 (file)
@@ -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);                 
index f0d943d..e859270 100644 (file)
@@ -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;
        
index fea0718..2c16b2b 100644 (file)
@@ -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());
                }
        }
        
index 4433c8c..48d167b 100644 (file)
@@ -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;
        }
 
index cd5113c..7e0c778 100644 (file)
@@ -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);
index 89e81af..aada44b 100644 (file)
@@ -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 (file)
index 0000000..dd55a26
--- /dev/null
@@ -0,0 +1,52 @@
+package com.mongodb.internal.validator;\r
+\r
+/*\r
+* ============LICENSE_START=======================================================\r
+* ONAP : DataLake\r
+* ================================================================================\r
+* Copyright 2018 China Mobile\r
+*=================================================================================\r
+* Licensed under the Apache License, Version 2.0 (the "License")\r
+* you may not use this file except in compliance with the License.\r
+* You may obtain a copy of the License at\r
+*\r
+*     http://www.apache.org/licenses/LICENSE-2.0\r
+*\r
+* Unless required by applicable law or agreed to in writing, software\r
+* distributed under the License is distributed on an "AS IS" BASIS,\r
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+* See the License for the specific language governing permissions and\r
+* limitations under the License.\r
+* ============LICENSE_END=========================================================\r
+*/\r
+import org.junit.Test;\r
+\r
+import static org.junit.Assert.assertFalse;\r
+import static org.junit.Assert.assertTrue;\r
+\r
+/**\r
+ * test CollectibleDocumentFieldNameValidator\r
+ * \r
+ * @author Guobiao Mo\r
+ *\r
+ */\r
+public class CollectibleDocumentFieldNameValidatorTest {\r
+       @Test\r
+       public void validate() {\r
+               CollectibleDocumentFieldNameValidator validator = new CollectibleDocumentFieldNameValidator();\r
+\r
+               assertTrue(validator.validate("$id"));\r
+               assertFalse(validator.validate("$abc"));\r
+               assertTrue(validator.validate("abc.abc"));\r
+\r
+               assertTrue(validator == validator.getValidatorForField("any"));\r
+\r
+       }\r
+\r
+       @Test(expected = IllegalArgumentException.class)\r
+       public void validateNull() {\r
+               CollectibleDocumentFieldNameValidator validator = new CollectibleDocumentFieldNameValidator();\r
+\r
+               assertTrue(validator.validate(null));\r
+       }\r
+}\r
index 7513249..b94211e 100644 (file)
@@ -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());
     }
index e21773c..afd4503 100644 (file)
@@ -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 (file)
index 0000000..4948001
--- /dev/null
@@ -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 (file)
index 0000000..0e6db83
--- /dev/null
@@ -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);
+       }
+}
index dc318ff..918c070 100644 (file)
@@ -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");
+       }
 }
index ee53ab3..7f0bb14 100644 (file)
@@ -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>