package org.onap.policy.clamp.acm.participant.http.main.handler;
-import java.io.Closeable;
-import java.io.IOException;
import java.lang.invoke.MethodHandles;
import java.util.List;
import java.util.Map;
import java.util.UUID;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
import java.util.stream.Collectors;
import javax.validation.Validation;
import javax.ws.rs.core.Response.Status;
import lombok.RequiredArgsConstructor;
-import org.apache.commons.lang3.tuple.Pair;
import org.onap.policy.clamp.acm.participant.http.main.models.ConfigRequest;
import org.onap.policy.clamp.acm.participant.http.main.webclient.AcHttpClient;
import org.onap.policy.clamp.acm.participant.intermediary.api.AutomationCompositionElementListener;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.models.base.PfModelException;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
*/
@Component
@RequiredArgsConstructor
-public class AutomationCompositionElementHandler implements AutomationCompositionElementListener, Closeable {
+public class AutomationCompositionElementHandler implements AutomationCompositionElementListener {
private static final Coder CODER = new StandardCoder();
private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
- private final ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
-
private final ParticipantIntermediaryApi intermediaryApi;
private final AcHttpClient acHttpClient;
throws PfModelException {
try {
var configRequest = getConfigRequest(properties);
- var restResponseMap = invokeHttpClient(configRequest);
+ var restResponseMap = acHttpClient.run(configRequest);
var failedResponseStatus = restResponseMap.values().stream()
.filter(response -> !HttpStatus.valueOf(response.getKey()).is2xxSuccessful())
.collect(Collectors.toList());
}
}
- /**
- * Invoke a runnable thread to execute http requests.
- *
- * @param configRequest ConfigRequest
- */
- private Map<ToscaConceptIdentifier, Pair<Integer, String>> invokeHttpClient(ConfigRequest configRequest)
- throws PfModelException {
- try {
- Map<ToscaConceptIdentifier, Pair<Integer, String>> restResponseMap = new ConcurrentHashMap<>();
- // Invoke runnable thread to execute https requests of all config entities
- var result = executor.submit(() -> acHttpClient.run(configRequest, restResponseMap), restResponseMap);
- if (!result.get().isEmpty()) {
- LOGGER.debug("Http Request Completed: {}", result.isDone());
- }
- return restResponseMap;
- } catch (InterruptedException e) {
- Thread.currentThread().interrupt();
- throw new PfModelException(Status.BAD_REQUEST, "Error invoking ExecutorService ", e);
- } catch (ExecutionException e) {
- throw new PfModelException(Status.BAD_REQUEST, "Error invoking the http request for the config ", e);
- }
- }
-
@Override
public void lock(UUID instanceId, UUID elementId) throws PfModelException {
intermediaryApi.updateAutomationCompositionElementState(instanceId, elementId, null, LockState.LOCKED,
intermediaryApi.updateCompositionState(compositionId, AcTypeState.COMMISSIONED, StateChangeResult.NO_ERROR,
"Deprimed");
}
-
- /**
- * Closes this stream and releases any system resources associated
- * with it. If the stream is already closed then invoking this
- * method has no effect.
- *
- * @throws IOException if an I/O error occurs
- */
- @Override
- public void close() throws IOException {
- executor.shutdown();
- }
}
package org.onap.policy.clamp.acm.participant.http.handler;
import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
-import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
"org.onap.domain.database.Http_PMSHMicroserviceAutomationCompositionElement";
@Test
- void testUndeploy() throws IOException {
+ void testUndeploy() {
var instanceId = commonTestData.getAutomationCompositionId();
var element = commonTestData.getAutomationCompositionElement();
var acElementId = element.getId();
var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
- try (var automationCompositionElementHandler =
- new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class))) {
- automationCompositionElementHandler.undeploy(instanceId, acElementId);
- verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, acElementId,
- DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "");
- }
+ var automationCompositionElementHandler =
+ new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class));
+ automationCompositionElementHandler.undeploy(instanceId, acElementId);
+ verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, acElementId,
+ DeployState.UNDEPLOYED, null, StateChangeResult.NO_ERROR, "");
+
}
@Test
- void testDeployConstraintViolations() throws IOException, PfModelException {
+ void testDeployConstraintViolations() throws PfModelException {
var instanceId = commonTestData.getAutomationCompositionId();
var element = commonTestData.getAutomationCompositionElement();
var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
- try (var automationCompositionElementHandler =
- new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class))) {
+ var automationCompositionElementHandler =
+ new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class));
- Map<String, Object> map = new HashMap<>();
- automationCompositionElementHandler.deploy(instanceId, element, map);
- verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
- DeployState.UNDEPLOYED, null, StateChangeResult.FAILED,
- "Constraint violations in the config request");
- }
+ Map<String, Object> map = new HashMap<>();
+ automationCompositionElementHandler.deploy(instanceId, element, map);
+ verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
+ DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Constraint violations in the config request");
}
@Test
- void testDeployError() throws IOException, PfModelException {
+ void testDeployError() throws PfModelException {
var instanceId = commonTestData.getAutomationCompositionId();
var element = commonTestData.getAutomationCompositionElement();
var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
- try (var automationCompositionElementHandler =
- new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class))) {
+ var automationCompositionElementHandler =
+ new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class));
+
+ Map<String, Object> map = new HashMap<>();
+ map.put("httpHeaders", 1);
+ automationCompositionElementHandler.deploy(instanceId, element, map);
+ verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
+ DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Error extracting ConfigRequest ");
- Map<String, Object> map = new HashMap<>();
- map.put("httpHeaders", 1);
- automationCompositionElementHandler.deploy(instanceId, element, map);
- verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
- DeployState.UNDEPLOYED, null, StateChangeResult.FAILED, "Error extracting ConfigRequest ");
- }
}
@Test
var acHttpClient = mock(AcHttpClient.class);
var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
- try (var automationCompositionElementHandler =
- new AutomationCompositionElementHandler(participantIntermediaryApi, acHttpClient)) {
+ var automationCompositionElementHandler =
+ new AutomationCompositionElementHandler(participantIntermediaryApi, acHttpClient);
- automationCompositionElementHandler.deploy(instanceId, element, map);
- verify(acHttpClient).run(any(ConfigRequest.class), anyMap());
- verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
- DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
- }
+ automationCompositionElementHandler.deploy(instanceId, element, map);
+ verify(acHttpClient).run(any(ConfigRequest.class));
+ verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, element.getId(),
+ DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Deployed");
}
@Test
- void testUpdate() throws Exception {
+ void testUpdate() throws PfModelException {
var instanceId = commonTestData.getAutomationCompositionId();
var element = commonTestData.getAutomationCompositionElement();
var acElementId = element.getId();
var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
- try (var automationCompositionElementHandler =
- new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class))) {
+ var automationCompositionElementHandler =
+ new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class));
- automationCompositionElementHandler.update(instanceId, element, Map.of());
- verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, acElementId,
- DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Update not supported");
- }
+ automationCompositionElementHandler.update(instanceId, element, Map.of());
+ verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, acElementId,
+ DeployState.DEPLOYED, null, StateChangeResult.NO_ERROR, "Update not supported");
}
@Test
- void testLock() throws Exception {
+ void testLock() throws PfModelException {
var instanceId = commonTestData.getAutomationCompositionId();
var acElementId = UUID.randomUUID();
var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
- try (var automationCompositionElementHandler =
- new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class))) {
+ var automationCompositionElementHandler =
+ new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class));
- automationCompositionElementHandler.lock(instanceId, acElementId);
- verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, acElementId, null,
- LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked");
- }
+ automationCompositionElementHandler.lock(instanceId, acElementId);
+ verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, acElementId, null,
+ LockState.LOCKED, StateChangeResult.NO_ERROR, "Locked");
}
@Test
- void testUnlock() throws Exception {
+ void testUnlock() throws PfModelException {
var instanceId = commonTestData.getAutomationCompositionId();
var acElementId = UUID.randomUUID();
var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
- try (var automationCompositionElementHandler =
- new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class))) {
+ var automationCompositionElementHandler =
+ new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class));
- automationCompositionElementHandler.unlock(instanceId, acElementId);
- verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, acElementId, null,
- LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked");
- }
+ automationCompositionElementHandler.unlock(instanceId, acElementId);
+ verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, acElementId, null,
+ LockState.UNLOCKED, StateChangeResult.NO_ERROR, "Unlocked");
}
@Test
- void testDelete() throws Exception {
+ void testDelete() throws PfModelException {
var instanceId = commonTestData.getAutomationCompositionId();
var acElementId = UUID.randomUUID();
var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
- try (var automationCompositionElementHandler =
- new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class))) {
+ var automationCompositionElementHandler =
+ new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class));
- automationCompositionElementHandler.delete(instanceId, acElementId);
- verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, acElementId,
- DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
- }
+ automationCompositionElementHandler.delete(instanceId, acElementId);
+ verify(participantIntermediaryApi).updateAutomationCompositionElementState(instanceId, acElementId,
+ DeployState.DELETED, null, StateChangeResult.NO_ERROR, "Deleted");
}
@Test
- void testPrime() throws Exception {
+ void testPrime() throws PfModelException {
var compositionId = UUID.randomUUID();
var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
- try (var automationCompositionElementHandler =
- new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class))) {
+ var automationCompositionElementHandler =
+ new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class));
- automationCompositionElementHandler.prime(compositionId, List.of());
- verify(participantIntermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED,
- StateChangeResult.NO_ERROR, "Primed");
- }
+ automationCompositionElementHandler.prime(compositionId, List.of());
+ verify(participantIntermediaryApi).updateCompositionState(compositionId, AcTypeState.PRIMED,
+ StateChangeResult.NO_ERROR, "Primed");
}
@Test
- void testDeprime() throws Exception {
+ void testDeprime() throws PfModelException {
var compositionId = UUID.randomUUID();
var participantIntermediaryApi = mock(ParticipantIntermediaryApi.class);
- try (var automationCompositionElementHandler =
- new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class))) {
+ var automationCompositionElementHandler =
+ new AutomationCompositionElementHandler(participantIntermediaryApi, mock(AcHttpClient.class));
- automationCompositionElementHandler.deprime(compositionId);
- verify(participantIntermediaryApi).updateCompositionState(compositionId, AcTypeState.COMMISSIONED,
- StateChangeResult.NO_ERROR, "Deprimed");
- }
+ automationCompositionElementHandler.deprime(compositionId);
+ verify(participantIntermediaryApi).updateCompositionState(compositionId, AcTypeState.COMMISSIONED,
+ StateChangeResult.NO_ERROR, "Deprimed");
}
}
package org.onap.policy.clamp.acm.participant.http.webclient;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import java.io.IOException;
-import java.util.HashMap;
import java.util.List;
-import java.util.Map;
-import org.apache.commons.lang3.tuple.Pair;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.onap.policy.clamp.acm.participant.http.utils.CommonTestData;
import org.onap.policy.clamp.acm.participant.http.utils.MockServerRest;
import org.onap.policy.common.utils.network.NetworkUtil;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.springframework.test.context.junit.jupiter.SpringExtension;
@ExtendWith(SpringExtension.class)
void test_validRequest() {
// Add valid rest requests POST, GET
var configurationEntity = commonTestData.getConfigurationEntity();
- Map<ToscaConceptIdentifier, Pair<Integer, String>> responseMap = new HashMap<>();
var headers = commonTestData.getHeaders();
var configRequest =
new ConfigRequest(MOCK_URL + ":" + mockServerPort, headers, List.of(configurationEntity), 10);
var client = new AcHttpClient();
- assertDoesNotThrow(() -> client.run(configRequest, responseMap));
+ var responseMap = client.run(configRequest);
assertThat(responseMap).hasSize(2).containsKey(commonTestData.restParamsWithGet().getRestRequestId());
var restResponseMap = responseMap.get(commonTestData.restParamsWithGet().getRestRequestId());
void test_invalidRequest() {
// Add rest requests Invalid POST, Valid GET
var configurationEntity = commonTestData.getInvalidConfigurationEntity();
- Map<ToscaConceptIdentifier, Pair<Integer, String>> responseMap = new HashMap<>();
var headers = commonTestData.getHeaders();
var configRequest =
new ConfigRequest(MOCK_URL + ":" + mockServerPort, headers, List.of(configurationEntity), 10);
var client = new AcHttpClient();
- assertDoesNotThrow(() -> client.run(configRequest, responseMap));
+ var responseMap = client.run(configRequest);
assertThat(responseMap).hasSize(2).containsKey(commonTestData.restParamsWithGet().getRestRequestId());
var response = responseMap.get(commonTestData.restParamsWithInvalidPost().getRestRequestId());
assertThat(response.getKey()).isEqualTo(404);
void test_WrongUrl() {
// Add rest requests Invalid URL
var configurationEntity = commonTestData.getInvalidConfigurationEntity();
- Map<ToscaConceptIdentifier, Pair<Integer, String>> responseMap = new HashMap<>();
var headers = commonTestData.getHeaders();
var configRequest =
new ConfigRequest(WRONG_URL + ":" + mockServerPort, headers, List.of(configurationEntity), 10);
var client = new AcHttpClient();
- assertDoesNotThrow(() -> client.run(configRequest, responseMap));
+ var responseMap = client.run(configRequest);
assertThat(responseMap).hasSize(2).containsKey(commonTestData.restParamsWithGet().getRestRequestId());
var response = responseMap.get(commonTestData.restParamsWithInvalidPost().getRestRequestId());
assertThat(response.getKey()).isEqualTo(404);