Use UUID for topic consumer instance 93/122393/2
authorJim Hahn <jrh3@att.com>
Thu, 1 Jul 2021 14:21:23 +0000 (10:21 -0400)
committerJim Hahn <jrh3@att.com>
Thu, 1 Jul 2021 15:44:04 +0000 (11:44 -0400)
Modified the code so that if the consumer group is defined, but the
consumer instance is not, then policy-endpoints will generate a UUID for
the consumer instance.
Made the logic a little more straight-forward in response to a review
comment.

Issue-ID: POLICY-3405
Change-Id: If74440bdb01525bc463a28e5b8a9a2eca89a855a
Signed-off-by: Jim Hahn <jrh3@att.com>
policy-endpoints/src/main/java/org/onap/policy/common/endpoints/event/comm/bus/internal/SingleThreadedBusTopicSource.java
policy-endpoints/src/test/java/org/onap/policy/common/endpoints/event/comm/bus/internal/SingleThreadedBusTopicSourceTest.java

index 3b7851d..f98b481 100644 (file)
@@ -94,15 +94,20 @@ public abstract class SingleThreadedBusTopicSource extends BusTopicBase
 
         super(busTopicParams);
 
-        if (busTopicParams.isConsumerGroupInvalid()) {
+        if (busTopicParams.isConsumerGroupInvalid() && busTopicParams.isConsumerInstanceInvalid()) {
             this.consumerGroup = UUID.randomUUID().toString();
-        } else {
+            this.consumerInstance = NetworkUtil.getHostname();
+
+        } else if (busTopicParams.isConsumerGroupInvalid()) {
+            this.consumerGroup = UUID.randomUUID().toString();
+            this.consumerInstance = busTopicParams.getConsumerInstance();
+
+        } else if (busTopicParams.isConsumerInstanceInvalid()) {
             this.consumerGroup = busTopicParams.getConsumerGroup();
-        }
+            this.consumerInstance = UUID.randomUUID().toString();
 
-        if (busTopicParams.isConsumerInstanceInvalid()) {
-            this.consumerInstance = NetworkUtil.getHostname();
         } else {
+            this.consumerGroup = busTopicParams.getConsumerGroup();
             this.consumerInstance = busTopicParams.getConsumerInstance();
         }
 
index 1a5506d..dbdd881 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * policy-endpoints
  * ================================================================================
- * Copyright (C) 2018-2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2018-2021 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
 
 package org.onap.policy.common.endpoints.event.comm.bus.internal;
 
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.api.Assertions.assertThatCode;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
@@ -44,6 +45,7 @@ import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
 import org.onap.policy.common.endpoints.event.comm.TopicListener;
 import org.onap.policy.common.endpoints.event.comm.bus.TopicTestBase;
 import org.onap.policy.common.utils.gson.GsonTestUtils;
+import org.onap.policy.common.utils.network.NetworkUtil;
 
 public class SingleThreadedBusTopicSourceTest extends TopicTestBase {
     private Thread thread;
@@ -160,12 +162,30 @@ public class SingleThreadedBusTopicSourceTest extends TopicTestBase {
 
     @Test
     public void testSingleThreadedBusTopicSource() {
+        // Note: if the value contains "-", it's probably a UUID
+
         // verify that different wrappers can be built
-        new SingleThreadedBusTopicSourceImpl(makeBuilder().consumerGroup(null).build());
-        new SingleThreadedBusTopicSourceImpl(makeBuilder().consumerInstance(null).build());
-        new SingleThreadedBusTopicSourceImpl(makeBuilder().fetchTimeout(-1).build());
-        assertThatCode(() -> new SingleThreadedBusTopicSourceImpl(makeBuilder().fetchLimit(-1).build()))
-                        .doesNotThrowAnyException();
+        source = new SingleThreadedBusTopicSourceImpl(makeBuilder().build());
+        assertThat(source.getConsumerGroup()).isEqualTo(MY_CONS_GROUP);
+        assertThat(source.getConsumerInstance()).isEqualTo(MY_CONS_INST);
+
+        // group is null => group is UUID, instance is as provided
+        source = new SingleThreadedBusTopicSourceImpl(makeBuilder().consumerGroup(null).build());
+        assertThat(source.getConsumerGroup()).contains("-").isNotEqualTo(NetworkUtil.getHostname());
+        assertThat(source.getConsumerInstance()).isEqualTo(MY_CONS_INST);
+
+        // instance is null => group is as provided, instance is UUID
+        source = new SingleThreadedBusTopicSourceImpl(makeBuilder().consumerInstance(null).build());
+        assertThat(source.getConsumerGroup()).isEqualTo(MY_CONS_GROUP);
+        assertThat(source.getConsumerInstance()).contains("-").isNotEqualTo(NetworkUtil.getHostname());
+
+        // group & instance are null => group is UUID, instance is hostname
+        source = new SingleThreadedBusTopicSourceImpl(makeBuilder().consumerGroup(null).consumerInstance(null).build());
+        assertThat(source.getConsumerGroup()).contains("-").isNotEqualTo(NetworkUtil.getHostname());
+        assertThat(source.getConsumerInstance()).isEqualTo(NetworkUtil.getHostname());
+
+        assertThatCode(() -> new SingleThreadedBusTopicSourceImpl(
+                        makeBuilder().fetchLimit(-1).fetchTimeout(-1).build())).doesNotThrowAnyException();
     }
 
     @Test