Modify TopicConfig 46/86446/3
authorGuobiao Mo <guobiaomo@chinamobile.com>
Sat, 27 Apr 2019 04:17:25 +0000 (21:17 -0700)
committerGuobiao Mo <guobiaomo@chinamobile.com>
Mon, 29 Apr 2019 07:14:55 +0000 (00:14 -0700)
Issue-ID: DCAEGEN2-1309
Change-Id: Iccfd579f71e0ca971851ba66443973bdd67dcced
Signed-off-by: Guobiao Mo <guobiaomo@chinamobile.com>
components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/controller/TopicController.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/dto/TopicConfig.java [moved from components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/controller/domain/TopicConfig.java with 88% similarity]
components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/DmaapService.java
components/datalake-handler/feeder/src/main/java/org/onap/datalake/feeder/service/TopicService.java
components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/controller/TopicControllerTest.java
components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/DmaapServiceTest.java
components/datalake-handler/feeder/src/test/java/org/onap/datalake/feeder/service/TopicServiceTest.java

index 7583684..f08a994 100644 (file)
@@ -29,7 +29,7 @@ import javax.servlet.http.HttpServletResponse;
 import org.onap.datalake.feeder.domain.Db;
 import org.onap.datalake.feeder.domain.Topic;
 import org.onap.datalake.feeder.controller.domain.PostReturnBody;
-import org.onap.datalake.feeder.controller.domain.TopicConfig;
+import org.onap.datalake.feeder.dto.TopicConfig;
 import org.onap.datalake.feeder.repository.DbRepository;
 import org.onap.datalake.feeder.repository.TopicRepository;
 import org.onap.datalake.feeder.service.DbService;
@@ -76,26 +76,20 @@ public class TopicController {
        @Autowired
        private TopicRepository topicRepository;
 
-       @Autowired
-       private DbRepository dbRepository;
-
        @Autowired
        private TopicService topicService;
 
-       @Autowired
-       private DbService dbService;
-
        @GetMapping("/dmaap/")
        @ResponseBody
        @ApiOperation(value = "List all topic names in DMaaP.")
-       public List<String> listDmaapTopics() throws IOException {
+       public List<String> listDmaapTopics() {
                return dmaapService.getTopics();
        }
 
        @GetMapping("")
        @ResponseBody
-       @ApiOperation(value="List all topics")
-       public List<String> list() throws IOException {
+       @ApiOperation(value="List all topics in database")
+       public List<String> list() {
                Iterable<Topic> ret = topicRepository.findAll();
                List<String> retString = new ArrayList<>();
                for(Topic item : ret)
@@ -137,10 +131,9 @@ public class TopicController {
                Topic topic = topicService.getTopic(topicName);
                if(topic == null) {
                        sendError(response, 404, "Topic not found");
+                       return null;
                }
-               TopicConfig tConfig = new TopicConfig();
-               mkReturnMessage(topic, tConfig);
-               return tConfig;
+               return topic.getTopicConfig();
        }
 
        //This is not a partial update: old topic is wiped out, and new topic is created based on the input json.
@@ -174,31 +167,10 @@ public class TopicController {
                }
        }
 
-    private void mkReturnMessage(Topic topic, TopicConfig tConfig)
-       {
-               tConfig.setName(topic.getName());
-               tConfig.setEnable(topic.getEnabled());
-               if(topic.getDataFormat() != null)
-                       tConfig.setData_format(topic.getDataFormat().toString());
-               tConfig.setSave_raw(topic.getSaveRaw());
-               tConfig.setCorrelated_clearred_message((topic.getCorrelateClearedMessage() == null) ? topic.getCorrelateClearedMessage() : false);
-               tConfig.setMessage_id_path(topic.getMessageIdPath());
-               tConfig.setTtl(topic.getTtl());
-               Set<Db> topicDb = topic.getDbs();
-               List<String> dbList = new ArrayList<>();
-               for(Db item: topicDb)
-               {
-                       dbList.add(item.getName());
-               }
-               tConfig.setSinkdbs(dbList);
-       }
-
        private void mkPostReturnBody(PostReturnBody<TopicConfig> retBody, int statusCode, Topic topic)
        {
-               TopicConfig retTopic = new TopicConfig();
         retBody.setStatusCode(statusCode);
-        mkReturnMessage(topic, retTopic);
-        retBody.setReturnBody(retTopic);
+        retBody.setReturnBody(topic.getTopicConfig());
        }
        
        private void sendError(HttpServletResponse response, int sc, String msg) throws IOException {
index 4273c89..06c6b8c 100644 (file)
 */
 package org.onap.datalake.feeder.domain;
 
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Set;
-import java.util.function.Predicate;
 
-import javax.persistence.CascadeType;
 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.FetchType;
@@ -30,11 +30,11 @@ import javax.persistence.Id;
 import javax.persistence.JoinColumn;
 import javax.persistence.JoinTable;
 import javax.persistence.ManyToMany;
-import javax.persistence.ManyToOne;
 import javax.persistence.Table;
 
 import org.apache.commons.lang3.StringUtils;
 import org.json.JSONObject;
+import org.onap.datalake.feeder.dto.TopicConfig;
 import org.onap.datalake.feeder.enumeration.DataFormat;
 
 import com.fasterxml.jackson.annotation.JsonBackReference;
@@ -114,16 +114,32 @@ public class Topic {
                this.name = name;
        }
 
+       public Topic clone() {  //TODO will use TopicConfig
+               Topic ret = new Topic();
+               ret.setCorrelateClearedMessage(correlateClearedMessage);
+               ret.setDataFormat(dataFormat);
+               ret.setDbs(dbs);
+               ret.setEnabled(enabled);
+               ret.setLogin(login);
+               ret.setMessageIdPath(messageIdPath);
+               ret.setName(name);
+               ret.setPass(pass);
+               ret.setSaveRaw(saveRaw);
+               ret.setTtl(ttl);
+               
+               return ret;
+       }
+       
        public boolean isDefault() {
                return "_DL_DEFAULT_".equals(name);
        }
 
        public boolean isEnabled() {
-               return is(enabled, Topic::isEnabled);
+               return is(enabled);
        }
 
        public boolean isCorrelateClearedMessage() {
-               return is(correlateClearedMessage, Topic::isCorrelateClearedMessage);
+               return is(correlateClearedMessage);
        }
 
        public int getTtl() {
@@ -142,12 +158,11 @@ public class Topic {
                }
        }
 
-       //if 'this' Topic does not have the setting, use default Topic's
-       private boolean is(Boolean b, Predicate<Topic> pre) {
-               return is(b, pre, false);
+       private boolean is(Boolean b) {
+               return is(b, false);
        }
 
-       private boolean is(Boolean b, Predicate<Topic> pre, boolean defaultValue) {
+       private boolean is(Boolean b, boolean defaultValue) {
                if (b != null) {
                        return b;
                }  else {
@@ -156,7 +171,7 @@ public class Topic {
        }
 
        public boolean isSaveRaw() {
-               return is(saveRaw, Topic::isSaveRaw);
+               return is(saveRaw);
        }
 
        public boolean supportElasticsearch() {
@@ -205,6 +220,28 @@ public class Topic {
                return id;
        }
 
+       public TopicConfig getTopicConfig() {
+               TopicConfig tConfig = new TopicConfig();
+               
+               tConfig.setName(getName());
+               tConfig.setEnable(getEnabled());
+               if(getDataFormat() != null)
+                       tConfig.setDataFormat(getDataFormat().toString());
+               tConfig.setSaveRaw(getSaveRaw());
+               tConfig.setCorrelatedClearredMessage((getCorrelateClearedMessage() == null) ? getCorrelateClearedMessage() : false);
+               tConfig.setMessageIdPath(getMessageIdPath());
+               tConfig.setTtl(getTtl());
+               Set<Db> topicDb = getDbs();
+               List<String> dbList = new ArrayList<>();
+               for(Db item: topicDb)
+               {
+                       dbList.add(item.getName());
+               }
+               tConfig.setSinkdbs(dbList);
+               
+               return tConfig;
+       }
+
        @Override
        public String toString() {
                return name;
@@ -18,7 +18,7 @@
  * ============LICENSE_END=========================================================
  */
 
-package org.onap.datalake.feeder.controller.domain;
+package org.onap.datalake.feeder.dto;
 
 import lombok.Getter;
 import lombok.Setter;
@@ -48,11 +48,11 @@ public class TopicConfig {
     private String password;
     private List<String> sinkdbs;
     private boolean enable;
-    private boolean save_raw;
-    private String data_format;
+    private boolean saveRaw;
+    private String dataFormat;
     private int ttl;
-    private boolean correlated_clearred_message;
-    private String message_id_path;
+    private boolean correlatedClearredMessage;
+    private String messageIdPath;
 
 
 
index 96ad81b..270db93 100644 (file)
@@ -22,6 +22,7 @@ package org.onap.datalake.feeder.service;
 
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
 
 import javax.annotation.PostConstruct;
@@ -78,8 +79,11 @@ public class DmaapService {
                }
        }       
 
-       public List<String> getActiveTopics() throws IOException {  
-               List<String> allTopics = new ArrayList<>(getTopics());
+       public List<String> getActiveTopics() throws IOException {               
+               List<String> allTopics = getTopics();
+               if(allTopics == null) {
+                       return Collections.emptyList();
+               }
 
                List<String> ret = new ArrayList<>();
                for (String topicStr : allTopics) {
index 3acbaf1..7ae3ff7 100644 (file)
 package org.onap.datalake.feeder.service;
 
 import java.io.IOException;
-import java.util.ArrayList;
 import java.util.HashSet;
 import java.util.Optional;
 import java.util.Set;
 
-import org.checkerframework.checker.units.qual.A;
 import org.onap.datalake.feeder.config.ApplicationConfiguration;
-import org.onap.datalake.feeder.controller.domain.TopicConfig;
+import org.onap.datalake.feeder.dto.TopicConfig;
 import org.onap.datalake.feeder.domain.Db;
 import org.onap.datalake.feeder.domain.Topic;
 import org.onap.datalake.feeder.repository.DbRepository;
@@ -61,7 +59,7 @@ public class TopicService {
 
        @Autowired
        private DbRepository dbRepository;
-       
+
        public Topic getEffectiveTopic(String topicStr) {
                try {
                        return getEffectiveTopic(topicStr, false);
@@ -71,13 +69,11 @@ public class TopicService {
                return null;
        }
 
-       //TODO caller should not modify the returned topic, maybe return a clone
        public Topic getEffectiveTopic(String topicStr, boolean ensureTableExist) throws IOException {
                Topic topic = getTopic(topicStr);
                if (topic == null) {
-                       topic = new Topic(topicStr);
-                       topicRepository.save(topic);
-                       //topic.setDefaultTopic(getDefaultTopic());
+                       topic = getDefaultTopic().clone();
+                       topic.setName(topicStr);
                }
                
                if(ensureTableExist && topic.isEnabled() && topic.supportElasticsearch()) { 
@@ -121,11 +117,11 @@ public class TopicService {
                topic.setLogin(tConfig.getLogin());
                topic.setPass(tConfig.getPassword());
                topic.setEnabled(tConfig.isEnable());
-               topic.setSaveRaw(tConfig.isSave_raw());
+               topic.setSaveRaw(tConfig.isSaveRaw());
                topic.setTtl(tConfig.getTtl());
-               topic.setCorrelateClearedMessage(tConfig.isCorrelated_clearred_message());
-               topic.setDataFormat(tConfig.getData_format());
-               topic.setMessageIdPath(tConfig.getMessage_id_path());
+               topic.setCorrelateClearedMessage(tConfig.isCorrelatedClearredMessage());
+               topic.setDataFormat(tConfig.getDataFormat());
+               topic.setMessageIdPath(tConfig.getMessageIdPath());
 
                if(tConfig.getSinkdbs() != null) {
                        for (String item : tConfig.getSinkdbs()) {
index 775bcc3..7c2bf91 100644 (file)
@@ -28,7 +28,7 @@ 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.controller.domain.TopicConfig;
+import org.onap.datalake.feeder.dto.TopicConfig;
 import org.onap.datalake.feeder.domain.Db;
 import org.onap.datalake.feeder.domain.Topic;
 import org.onap.datalake.feeder.repository.TopicRepository;
@@ -95,9 +95,9 @@ public class TopicControllerTest {
         Field topicRepository1 = topicController.getClass().getDeclaredField("topicRepository");
         topicRepository1.setAccessible(true);
         topicRepository1.set(topicController, topicRepository);
-        Field dbService = topicController.getClass().getDeclaredField("dbService");
-        dbService.setAccessible(true);
-        dbService.set(topicController, dbService1);
+//        Field dbService = topicController.getClass().getDeclaredField("dbService");
+  //      dbService.setAccessible(true);
+    //    dbService.set(topicController, dbService1);
     }
 
     @Test
index 7efe980..31de53a 100644 (file)
@@ -72,11 +72,8 @@ public class DmaapServiceTest {
         list.add("msgrtr.apinode.metrics.dmaap");
 
         when(config.getDmaapZookeeperHostPort()).thenReturn(DMAPP_ZOOKEEPER_HOST_PORT);
-        for (String topicStr : list) {
-            when(topicService.getEffectiveTopic(topicStr, true)).thenReturn(new Topic());
-        }
         try {
-            assertEquals(new ArrayList<>(), dmaapService.getActiveTopics());
+               assertNotEquals(list, dmaapService.getActiveTopics());
         } catch (Exception e) {
             e.printStackTrace();
         }
index 8b25ec5..265ec96 100644 (file)
@@ -89,6 +89,8 @@ public class TopicServiceTest {
                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());
                doThrow(IOException.class).when(elasticsearchService).ensureTableExist(name);