Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package org.cloudfoundry.multiapps.controller.web;

import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.cloudfoundry.multiapps.controller.process.variables.Variables;

import static org.cloudfoundry.multiapps.controller.persistence.Constants.VARIABLE_NAME_SERVICE_ID;

public class Constants {

private Constants() {
Expand Down Expand Up @@ -58,4 +63,11 @@ private Endpoints() {

public static final String PURGE = "/purge";
}

public static final Set<String> NAMES_OF_SERVICE_PARAMETERS = Set.of(
VARIABLE_NAME_SERVICE_ID, Variables.USER.getName(),
Variables.USER_GUID.getName(), Variables.SPACE_NAME.getName(), Variables.SPACE_GUID.getName(),
Variables.ORGANIZATION_NAME.getName(), Variables.ORGANIZATION_GUID.getName(), Variables.TIMESTAMP.getName(),
Variables.MTA_NAMESPACE.getName());

}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.server.ResponseStatusException;

import static org.cloudfoundry.multiapps.controller.web.Constants.NAMES_OF_SERVICE_PARAMETERS;

@Named
public class OperationsApiServiceImpl implements OperationsApiService {

Expand Down Expand Up @@ -275,10 +277,27 @@ private Operation addServiceParameters(Operation operation, String spaceGuid, St
}

private Operation addParameterValues(Operation operation, Set<ParameterMetadata> predefinedParameters) {
Map<String, Object> parameters = new HashMap<>(operation.getParameters());
parameters.putAll(ParameterConversion.toFlowableVariables(predefinedParameters, parameters));
Map<String, Object> filteredParameters = filterUnnecessaryParameters(predefinedParameters, operation.getParameters());
filteredParameters.putAll(ParameterConversion.toFlowableVariables(predefinedParameters, filteredParameters));
return ImmutableOperation.copyOf(operation)
.withParameters(parameters);
.withParameters(filteredParameters);
}

private Map<String, Object> filterUnnecessaryParameters(Set<ParameterMetadata> predefinedParameters, Map<String, Object> parameters) {
Set<String> allowedParameters = getAllowedParameters(predefinedParameters);

return parameters.entrySet()
.stream()
.filter(entry -> allowedParameters.contains(entry.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a, b) -> b, HashMap::new));
}

private Set<String> getAllowedParameters(Set<ParameterMetadata> predefinedParameters) {
Set<String> allowedParameters = predefinedParameters.stream()
.map(ParameterMetadata::getId)
.collect(Collectors.toSet());
allowedParameters.addAll(NAMES_OF_SERVICE_PARAMETERS);
return allowedParameters;
}

private void ensureRequiredParametersSet(Operation operation, Set<ParameterMetadata> predefinedParameters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Answers;
import org.mockito.ArgumentMatchers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
Expand Down Expand Up @@ -203,6 +204,40 @@ void testStartOperation() {
.startProcess(Mockito.any(), Mockito.anyMap());
}

@Test
void testStartOperationWithInvalidParametersForTheProcess() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add test annotation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

Map<String, Object> parameters = Map.of(Variables.MTA_ID.getName(), "test", Variables.EXT_DESCRIPTOR_FILE_ID.getName(), "ext_test",
Variables.CTS_PROCESS_ID.getName(), "cts_test", Variables.DEPLOY_URI.getName(),
"deploy_test");
Operation operation = createOperation(null, null, parameters);
Mockito.when(operationsHelper.getProcessDefinitionKey(operation))
.thenReturn("deploy");

operationsApiService.startOperation(mockHttpServletRequest(EXAMPLE_USER), SPACE_GUID, operation);

Mockito.verify(flowableFacade)
.startProcess(ArgumentMatchers.eq("deploy"), ArgumentMatchers.argThat(
map -> map.containsKey(Variables.MTA_ID.getName()) && map.containsKey(Variables.EXT_DESCRIPTOR_FILE_ID.getName())
&& !map.containsKey(Variables.CTS_PROCESS_ID.getName()) && !map.containsKey(Variables.DEPLOY_URI.getName())));
}

@Test
void testStartOperationWithValidParametersForTheProcess() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add test annotation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

Map<String, Object> parameters = Map.of(Variables.MTA_ID.getName(), "test", Variables.EXT_DESCRIPTOR_FILE_ID.getName(), "ext_test",
Variables.NO_START.getName(), false, Variables.MTA_NAMESPACE.getName(),
"namespace_test");
Operation operation = createOperation(null, null, parameters);
Mockito.when(operationsHelper.getProcessDefinitionKey(operation))
.thenReturn("deploy");

operationsApiService.startOperation(mockHttpServletRequest(EXAMPLE_USER), SPACE_GUID, operation);

Mockito.verify(flowableFacade)
.startProcess(ArgumentMatchers.eq("deploy"), ArgumentMatchers.argThat(
map -> map.containsKey(Variables.MTA_ID.getName()) && map.containsKey(Variables.EXT_DESCRIPTOR_FILE_ID.getName())
&& map.containsKey(Variables.NO_START.getName()) && map.containsKey(Variables.MTA_NAMESPACE.getName())));
}

@Test
void testGetOperationLogs() throws Exception {
String processId = FINISHED_PROCESS;
Expand Down
Loading