Merge "Fix restart flag in ACM"
diff --git a/packages/policy-clamp-tarball/src/main/resources/etc/AcRuntimeParameters.yaml b/packages/policy-clamp-tarball/src/main/resources/etc/AcRuntimeParameters.yaml
old mode 100644
new mode 100755
index a8e45ff..ea8ae2e
--- a/packages/policy-clamp-tarball/src/main/resources/etc/AcRuntimeParameters.yaml
+++ b/packages/policy-clamp-tarball/src/main/resources/etc/AcRuntimeParameters.yaml
@@ -40,7 +40,7 @@
 runtime:
   participantParameters:
     heartBeatMs: 20000
-    maxStatusWaitMs: 100000
+    maxStatusWaitMs: 200000
   topicParameterGroup:
     topicSources:
       -
diff --git a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionParticipantHandler.java b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionParticipantHandler.java
old mode 100644
new mode 100755
index e0d11ed..f8bc233
--- a/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionParticipantHandler.java
+++ b/runtime-acm/src/main/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionParticipantHandler.java
@@ -76,10 +76,7 @@
 
         if (participantOpt.isPresent()) {
             var participant = participantOpt.get();
-            if (ParticipantState.OFF_LINE.equals(participant.getParticipantState())) {
-                participant.setParticipantState(ParticipantState.ON_LINE);
-                participantProvider.saveParticipant(participant);
-            }
+            checkOnline(participant);
             handleRestart(participant.getParticipantId());
         } else {
             var participant = createParticipant(participantRegisterMsg.getParticipantId(),
@@ -127,12 +124,21 @@
             var participant = createParticipant(participantStatusMsg.getParticipantId(),
                     listToMap(participantStatusMsg.getParticipantSupportedElementType()));
             participantProvider.saveParticipant(participant);
+        } else {
+            checkOnline(participantOpt.get());
         }
         if (!participantStatusMsg.getAutomationCompositionInfoList().isEmpty()) {
             automationCompositionProvider.upgradeStates(participantStatusMsg.getAutomationCompositionInfoList());
         }
     }
 
+    private void checkOnline(Participant participant) {
+        if (ParticipantState.OFF_LINE.equals(participant.getParticipantState())) {
+            participant.setParticipantState(ParticipantState.ON_LINE);
+            participantProvider.saveParticipant(participant);
+        }
+    }
+
     private void handleRestart(UUID participantId) {
         var compositionIds = participantProvider.getCompositionIds(participantId);
         for (var compositionId : compositionIds) {
diff --git a/runtime-acm/src/main/resources/application.yaml b/runtime-acm/src/main/resources/application.yaml
old mode 100644
new mode 100755
index faef54a..8dd4b57
--- a/runtime-acm/src/main/resources/application.yaml
+++ b/runtime-acm/src/main/resources/application.yaml
@@ -42,7 +42,7 @@
 runtime:
   participantParameters:
     heartBeatMs: 20000
-    maxStatusWaitMs: 100000
+    maxStatusWaitMs: 200000
   topicParameterGroup:
     topicSources:
       -
diff --git a/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionParticipantHandlerTest.java b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionParticipantHandlerTest.java
old mode 100644
new mode 100755
index ed8a856..49aa030
--- a/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionParticipantHandlerTest.java
+++ b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/supervision/SupervisionParticipantHandlerTest.java
@@ -82,9 +82,7 @@
         var participantRegisterMessage = new ParticipantRegister();
         participantRegisterMessage.setMessageId(UUID.randomUUID());
         participantRegisterMessage.setParticipantId(CommonTestData.getParticipantId());
-        var supportedElementType = new ParticipantSupportedElementType();
-        supportedElementType.setTypeName("Type");
-        supportedElementType.setTypeVersion("1.0.0");
+        var supportedElementType = CommonTestData.createParticipantSupportedElementType();
         participantRegisterMessage.setParticipantSupportedElementType(List.of(supportedElementType));
 
         var participantProvider = mock(ParticipantProvider.class);
@@ -105,9 +103,7 @@
         participantRegisterMessage.setMessageId(UUID.randomUUID());
         var participantId = CommonTestData.getParticipantId();
         participantRegisterMessage.setParticipantId(participantId);
-        var supportedElementType = new ParticipantSupportedElementType();
-        supportedElementType.setTypeName("Type");
-        supportedElementType.setTypeVersion("1.0.0");
+        var supportedElementType = CommonTestData.createParticipantSupportedElementType();
         participantRegisterMessage.setParticipantSupportedElementType(List.of(supportedElementType));
 
         var participant = new Participant();
@@ -156,9 +152,7 @@
         var participantStatusMessage = new ParticipantStatus();
         participantStatusMessage.setParticipantId(CommonTestData.getParticipantId());
         participantStatusMessage.setState(ParticipantState.ON_LINE);
-        var supportedElementType = new ParticipantSupportedElementType();
-        supportedElementType.setTypeName("Type");
-        supportedElementType.setTypeVersion("1.0.0");
+        var supportedElementType = CommonTestData.createParticipantSupportedElementType();
         participantStatusMessage.setParticipantSupportedElementType(List.of(supportedElementType));
         participantStatusMessage.setAutomationCompositionInfoList(List.of(new AutomationCompositionInfo()));
 
@@ -175,4 +169,50 @@
 
         verify(automationCompositionProvider).upgradeStates(any());
     }
+
+    @Test
+    void testHandleParticipantStatusNotRegisterd() {
+        var participantStatusMessage = new ParticipantStatus();
+        participantStatusMessage.setParticipantId(CommonTestData.getParticipantId());
+        participantStatusMessage.setState(ParticipantState.ON_LINE);
+        var supportedElementType = CommonTestData.createParticipantSupportedElementType();
+        participantStatusMessage.setParticipantSupportedElementType(List.of(supportedElementType));
+        participantStatusMessage.setAutomationCompositionInfoList(List.of(new AutomationCompositionInfo()));
+
+        var participantProvider = mock(ParticipantProvider.class);
+        var automationCompositionProvider = mock(AutomationCompositionProvider.class);
+        var handler =
+                new SupervisionParticipantHandler(participantProvider, mock(ParticipantRegisterAckPublisher.class),
+                        mock(ParticipantDeregisterAckPublisher.class), automationCompositionProvider,
+                        mock(AcDefinitionProvider.class), mock(ParticipantRestartPublisher.class));
+        handler.handleParticipantMessage(participantStatusMessage);
+
+        verify(participantProvider).saveParticipant(any());
+        verify(automationCompositionProvider).upgradeStates(any());
+    }
+
+    @Test
+    void testHandleParticipantStatusCheckOnline() {
+        var participantStatusMessage = new ParticipantStatus();
+        participantStatusMessage.setParticipantId(CommonTestData.getParticipantId());
+        participantStatusMessage.setState(ParticipantState.ON_LINE);
+        var supportedElementType = CommonTestData.createParticipantSupportedElementType();
+        participantStatusMessage.setParticipantSupportedElementType(List.of(supportedElementType));
+        participantStatusMessage.setAutomationCompositionInfoList(List.of(new AutomationCompositionInfo()));
+
+        var participantProvider = mock(ParticipantProvider.class);
+        var automationCompositionProvider = mock(AutomationCompositionProvider.class);
+        var handler =
+                new SupervisionParticipantHandler(participantProvider, mock(ParticipantRegisterAckPublisher.class),
+                        mock(ParticipantDeregisterAckPublisher.class), automationCompositionProvider,
+                        mock(AcDefinitionProvider.class), mock(ParticipantRestartPublisher.class));
+        var participant = CommonTestData.createParticipant(CommonTestData.getParticipantId());
+        participant.setParticipantState(ParticipantState.OFF_LINE);
+        when(participantProvider.findParticipant(CommonTestData.getParticipantId()))
+                .thenReturn(Optional.of(participant));
+        handler.handleParticipantMessage(participantStatusMessage);
+
+        verify(participantProvider).saveParticipant(any());
+        verify(automationCompositionProvider).upgradeStates(any());
+    }
 }
diff --git a/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/util/CommonTestData.java b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/util/CommonTestData.java
old mode 100644
new mode 100755
index 5c82713..270f1e0
--- a/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/util/CommonTestData.java
+++ b/runtime-acm/src/test/java/org/onap/policy/clamp/acm/runtime/util/CommonTestData.java
@@ -29,6 +29,7 @@
 import org.onap.policy.clamp.models.acm.concepts.AutomationCompositionDefinition;
 import org.onap.policy.clamp.models.acm.concepts.Participant;
 import org.onap.policy.clamp.models.acm.concepts.ParticipantState;
+import org.onap.policy.clamp.models.acm.concepts.ParticipantSupportedElementType;
 import org.onap.policy.clamp.models.acm.utils.AcmUtils;
 import org.onap.policy.common.utils.coder.Coder;
 import org.onap.policy.common.utils.coder.CoderException;
@@ -97,6 +98,18 @@
         return participant;
     }
 
+    /**
+     * Create a new ParticipantSupportedElementType.
+     *
+     * @return a new ParticipantSupportedElementType
+     */
+    public static ParticipantSupportedElementType createParticipantSupportedElementType() {
+        var supportedElementType = new ParticipantSupportedElementType();
+        supportedElementType.setTypeName("Type");
+        supportedElementType.setTypeVersion("1.0.0");
+        return supportedElementType;
+    }
+
     public static UUID getParticipantId() {
         return UUID.fromString("101c62b3-8918-41b9-a747-d21eb79c6c03");
     }