Skip to content

Commit

Permalink
Cleaned the code to solve issues with IMService class tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lgiommi committed Oct 31, 2023
1 parent 5c397b5 commit 6f986e7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@
import org.mitre.openid.connect.client.service.impl.StaticClientConfigurationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
Expand All @@ -105,25 +104,17 @@ public class ImServiceImpl extends AbstractDeploymentProviderService {
/**
* Constructor of the ImServiceImpl class.
*/
@Autowired
public ImServiceImpl(RestTemplateBuilder restTemplateBuilder,
CustomOAuth2TemplateFactory templateFactory,
StaticClientConfigurationService staticClientConfigurationService,
ImClientFactory imClientFactory) {
this.restTemplate = restTemplateBuilder.build();
this.templateFactory = templateFactory;
this.staticClientConfigurationService = staticClientConfigurationService;
this.imClientFactory = imClientFactory;
}

private final RestTemplate restTemplate;
private RestTemplate restTemplate;

@Autowired
private IamService iamService;

private final CustomOAuth2TemplateFactory templateFactory;
@Autowired
private CustomOAuth2TemplateFactory templateFactory;

private final StaticClientConfigurationService staticClientConfigurationService;
@Autowired
private StaticClientConfigurationService staticClientConfigurationService;

@Autowired
private ToscaService toscaService;
Expand All @@ -143,7 +134,8 @@ public ImServiceImpl(RestTemplateBuilder restTemplateBuilder,
@Autowired
private OAuth2TokenService oauth2TokenService;

private final ImClientFactory imClientFactory;
@Autowired
private ImClientFactory imClientFactory;

private static final String VMINFO = "VirtualMachineInfo";
public static final String IAM_TOSCA_NODE_TYPE = "tosca.nodes.indigo.iam.client";
Expand Down Expand Up @@ -188,6 +180,7 @@ protected ArchiveRoot prepareTemplate(String template, Deployment deployment,

@Override
public boolean doDeploy(DeploymentMessage deploymentMessage) {
restTemplate = new RestTemplate();
Deployment deployment = getDeployment(deploymentMessage);
String uuid = deployment.getId();

Expand Down Expand Up @@ -268,9 +261,6 @@ public boolean doDeploy(DeploymentMessage deploymentMessage) {
Map<String, Map<String, String>> iamTemplateInput = null;
Map<String, Map<String, String>> iamTemplateOutput = new HashMap<>();

// Get information about the clients related to the orchestrator
Map<String, RegisteredClient> clients = staticClientConfigurationService.getClients();

// Loop over the deployment resources and create an IAM client for all the
// IAM_TOSCA_NODE_TYPE nodes requested
for (Resource resource : resources.get(false)) {
Expand All @@ -281,10 +271,13 @@ public boolean doDeploy(DeploymentMessage deploymentMessage) {
String issuerNode;
String tokenCredentials = null;
Map<String, String> clientCreated = new HashMap<>();
Map<String, RegisteredClient> orchestratorClients = new HashMap<>();

// Get properties of IAM_TOSCA_NODE_TYPE nodes from the TOSCA template
// Get properties of IAM_TOSCA_NODE_TYPE nodes from the TOSCA template and
// get information about the clients related to the orchestrator
if (iamTemplateInput == null) {
iamTemplateInput = toscaService.getIamProperties(ar);
orchestratorClients = staticClientConfigurationService.getClients();
}

// Set the issuer of the current node
Expand Down Expand Up @@ -352,7 +345,7 @@ public boolean doDeploy(DeploymentMessage deploymentMessage) {
resource.setMetadata(resourceMetadata);
iamTemplateOutput.put(nodeName, resourceMetadata);

if (!clients.containsKey(issuerNode)) {
if (!orchestratorClients.containsKey(issuerNode)) {
String errorMessage = String.format("There is no orchestrator client belonging to the "
+ "identity provider: %s. Impossible to set the ownership of the client with "
+ "client_id %s", issuerNode, clientCreated.get(CLIENT_ID));
Expand All @@ -361,7 +354,7 @@ public boolean doDeploy(DeploymentMessage deploymentMessage) {
try {
// Get the orchestrator client related to the issuer of the node and extract
// client_id and client_secret
RegisteredClient orchestratorClient = clients.get(issuerNode);
RegisteredClient orchestratorClient = orchestratorClients.get(issuerNode);
String orchestratorClientId = orchestratorClient.getClientId();
String orchestratorClientSecret = orchestratorClient.getClientSecret();

Expand Down Expand Up @@ -763,6 +756,7 @@ public void cleanFailedUpdate(DeploymentMessage deploymentMessage) {

@Override
public boolean doUndeploy(DeploymentMessage deploymentMessage) {
restTemplate = new RestTemplate();
Deployment deployment = getDeployment(deploymentMessage);

Map<Boolean, Set<Resource>> resources =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@

import alien4cloud.tosca.model.ArchiveRoot;
import alien4cloud.tosca.parser.ParsingException;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import es.upv.i3m.grycap.im.InfrastructureManager;
import es.upv.i3m.grycap.im.States;
import es.upv.i3m.grycap.im.exceptions.ImClientErrorException;
Expand All @@ -46,9 +44,9 @@
import it.reply.orchestrator.dal.repository.DeploymentRepository;
import it.reply.orchestrator.dal.repository.ResourceRepository;
import it.reply.orchestrator.dto.CloudProviderEndpoint;
import it.reply.orchestrator.dto.cmdb.ComputeService;
import it.reply.orchestrator.dto.cmdb.CloudService;
import it.reply.orchestrator.dto.cmdb.CloudServiceType;
import it.reply.orchestrator.dto.cmdb.ComputeService;
import it.reply.orchestrator.dto.deployment.DeploymentMessage;
import it.reply.orchestrator.dto.workflow.CloudServiceWf;
import it.reply.orchestrator.dto.workflow.CloudServicesOrderedIterator;
Expand All @@ -61,10 +59,10 @@
import it.reply.orchestrator.exception.service.DeploymentException;
import it.reply.orchestrator.exception.service.ToscaException;
import it.reply.orchestrator.function.ThrowingFunction;
import it.reply.orchestrator.service.IamService;
import it.reply.orchestrator.service.ToscaServiceImpl;
import it.reply.orchestrator.service.deployment.providers.factory.ImClientFactory;
import it.reply.orchestrator.util.TestUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -74,7 +72,6 @@
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import org.alien4cloud.tosca.model.templates.NodeTemplate;
import org.alien4cloud.tosca.model.templates.Topology;
import org.assertj.core.api.AbstractThrowableAssert;
Expand All @@ -87,16 +84,16 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;

import junitparams.JUnitParamsRunner;
import junitparams.Parameters;

import static it.reply.orchestrator.dto.cmdb.CloudService.ONEPROVIDER_STORAGE_SERVICE;
import static it.reply.orchestrator.dto.cmdb.CloudService.OPENSTACK_COMPUTE_SERVICE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static it.reply.orchestrator.dto.cmdb.CloudService.OPENSTACK_COMPUTE_SERVICE;
import static it.reply.orchestrator.dto.cmdb.CloudService.ONEPROVIDER_STORAGE_SERVICE;
import static org.mockito.Mockito.*;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import junitparams.JUnitParamsRunner;
import junitparams.Parameters;

@RunWith(JUnitParamsRunner.class)
public class ImServiceTest extends ToscaParserAwareTest {
Expand All @@ -107,6 +104,9 @@ public class ImServiceTest extends ToscaParserAwareTest {
@MockBean
private ImClientFactory imClientFactory;

@MockBean
private IamService iamService;

@MockBean
private DeploymentRepository deploymentRepository;

Expand Down

0 comments on commit 6f986e7

Please sign in to comment.