Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mynecker committed Nov 20, 2024
1 parent 39328ad commit c969f60
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 87 deletions.
4 changes: 2 additions & 2 deletions service/application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,8 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>14</source>
<target>14</target>
<source>${java.version}</source>
<target>${java.version}</target>
<annotationProcessorPaths>
<path>
<groupId>org.projectlombok</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,6 @@ public void execute(Command command, SempApiProvider sempApiProvider) {
}
}


/**
* private void executeSempDeleteCommand(Command command, SempApiProvider sempApiProvider) throws ApiException, JsonProcessingException {
* SempEntityType deletionEntityType = SempEntityType.valueOf((String) command.getParameters().get(SempDeleteCommandConstants.SEMP_DELETE_ENTITY_TYPE));
* switch (deletionEntityType) {
* case solaceAclProfile -> executeDeleteAclProfile(command, sempApiProvider);
* case solaceQueue -> executeDeleteSolaceQueue(command, sempApiProvider);
* case solaceQueueSubscriptionTopic -> executeDeleteSolaceQueueTopicSubscription(command, sempApiProvider);
* case solaceClientUsername -> executeDeleteClientUsername(command, sempApiProvider);
* case solaceAuthorizationGroup -> executeDeleteAuthorizationGroup(command, sempApiProvider);
* case solaceAclSubscribeTopicException -> executeDeleteAclSubscribeTopicException(command, sempApiProvider);
* case solaceAclPublishTopicException -> executeDeleteAclPublishTopicException(command, sempApiProvider);
* default -> throw new UnsupportedOperationException("Unsupported entity type for deletion: " + deletionEntityType);
* }
* }
*/

private void executeSempDeleteCommand(Command command, SempApiProvider sempApiProvider) throws ApiException, JsonProcessingException {
String entityType = (String) command.getParameters().get(SempDeleteCommandConstants.SEMP_DELETE_ENTITY_TYPE);
SempEntityType deletionEntityType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ public static void setCommandError(Command command, Exception e) {
* @param directory
*/
public static void deleteConfigPath(CommandRequest request, String directory) {
Validate.notNull(request, "request must be provided");
Validate.notEmpty(request.getContext(), "context of request must be provided");
Validate.notEmpty(request.getServiceId(), "serviceId of request must be provided");
Validate.notEmpty(directory, "directory must be provided");
// just in case the delete operation is called concurrently
synchronized (lock) {
Validate.notNull(request, "request must be provided");
Validate.notEmpty(request.getContext(), "context of request must be provided");
Validate.notEmpty(request.getServiceId(), "serviceId of request must be provided");
Validate.notEmpty(directory, "directory must be provided");
Path configPath = Paths.get(directory + File.separator
+ request.getContext()
+ "-"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,64 +71,6 @@ public class TerraformCommandIT {
@Autowired
private ResourceLoader resourceLoader;

private static Command generateCommand(String tfCommand, String body) {
return generateCommand(tfCommand, body, false);
}

private static Command generateCommand(String tfCommand, String body, Boolean ignoreResult) {
return generateCommand(tfCommand, body, ignoreResult,
Map.of("Content-Type", "application/hcl",
"Content-Encoding", "base64"));
}

private static Command generateCommand(String tfCommand, String body, Boolean ignoreResult, Map<String, Object> parameters) {
return Command.builder()
.body(Optional.ofNullable(body)
.map(b -> Base64.getEncoder().encodeToString(b.getBytes(UTF_8)))
.orElse(""))
.command(tfCommand)
.commandType(CommandType.terraform)
.ignoreResult(ignoreResult)
.parameters(parameters)
.build();
}

private static CommandRequest generateCommandRequest(Command commandRequest) {
return generateCommandRequest(List.of(commandRequest), false);
}

private static CommandRequest generateCommandRequest(List<Command> commandRequests, boolean exitOnFailure) {
return CommandRequest.builder()
.commandBundles(List.of(
CommandBundle.builder()
.executionType(ExecutionType.serial)
.exitOnFailure(exitOnFailure)
.commands(commandRequests)
.build()))
.commandCorrelationId("234")
.context("app123")
.serviceId("ms1234")
.build();
}

private static String getResourceAsString(Resource resource) {
try (Reader reader = new InputStreamReader(resource.getInputStream(), UTF_8)) {
return FileCopyUtils.copyToString(reader);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private static List<String> getResourceAsStringArray(Resource resource) {
try (Reader reader = new InputStreamReader(resource.getInputStream(), UTF_8)) {
return Arrays.stream(FileCopyUtils.copyToString(reader).split("\n"))
.filter(StringUtils::isNotEmpty)
.collect(Collectors.toList());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

@BeforeAll
public void setup() {
terraformProperties.setWorkingDirectoryRoot(System.getProperty("java.io.tmpdir"));
Expand Down Expand Up @@ -165,6 +107,7 @@ void testExecutionLogsAreGenerated() throws IOException {
Path writeHclExecutionLogFilePath = terraformManager.execute(generateCommandRequest(writeHclCommand), writeHclCommand, Map.of());
Assertions.assertThat(writeHclExecutionLogFilePath.toFile().getName()).startsWith("write_HCL");


Command applyCommand = generateCommand("apply", newQueueTf);

when(terraformClient.plan(Map.of())).thenReturn(CompletableFuture.supplyAsync(() -> true));
Expand Down Expand Up @@ -529,6 +472,47 @@ public void testDeleteResourceHappyPath() throws IOException {
}
}

private static Command generateCommand(String tfCommand, String body) {
return generateCommand(tfCommand, body, false);
}

private static Command generateCommand(String tfCommand, String body, Boolean ignoreResult) {
return generateCommand(tfCommand, body, ignoreResult,
Map.of("Content-Type", "application/hcl",
"Content-Encoding", "base64"));
}

private static Command generateCommand(String tfCommand, String body, Boolean ignoreResult, Map<String, Object> parameters) {
return Command.builder()
.body(Optional.ofNullable(body)
.map(b -> Base64.getEncoder().encodeToString(b.getBytes(UTF_8)))
.orElse(""))
.command(tfCommand)
.commandType(CommandType.terraform)
.ignoreResult(ignoreResult)
.parameters(parameters)
.build();
}


private static CommandRequest generateCommandRequest(Command commandRequest) {
return generateCommandRequest(List.of(commandRequest), false);
}

private static CommandRequest generateCommandRequest(List<Command> commandRequests, boolean exitOnFailure) {
return CommandRequest.builder()
.commandBundles(List.of(
CommandBundle.builder()
.executionType(ExecutionType.serial)
.exitOnFailure(exitOnFailure)
.commands(commandRequests)
.build()))
.commandCorrelationId("234")
.context("app123")
.serviceId("ms1234")
.build();
}

private void setupLogMock(List<String> logs) {
doAnswer(invocation -> {
Object arg0 = invocation.getArgument(0);
Expand All @@ -538,6 +522,25 @@ private void setupLogMock(List<String> logs) {
}).when(terraformClient).setOutputListener(any());
}


private static String getResourceAsString(Resource resource) {
try (Reader reader = new InputStreamReader(resource.getInputStream(), UTF_8)) {
return FileCopyUtils.copyToString(reader);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private static List<String> getResourceAsStringArray(Resource resource) {
try (Reader reader = new InputStreamReader(resource.getInputStream(), UTF_8)) {
return Arrays.stream(FileCopyUtils.copyToString(reader).split("\n"))
.filter(StringUtils::isNotEmpty)
.collect(Collectors.toList());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private void assertAllLogsContainExpectedFields(List<Map<String, Object>> logs) {
for (Map<String, Object> log : logs) {
assertTrue(log.containsKey("message"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@
import java.nio.file.Files;
import java.nio.file.Path;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

@ActiveProfiles("TEST")
public class TerraformUtilsTest {
class TerraformUtilsTest {

@Test
public void testDeleteConfigPath(@TempDir Path tempDirectory) throws IOException {
void testDeleteConfigPath(@TempDir Path tempDirectory) throws IOException {
CommandRequest commandRequest = CommandRequest
.builder()
.context("someContext")
Expand All @@ -31,8 +30,5 @@ public void testDeleteConfigPath(@TempDir Path tempDirectory) throws IOException
TerraformUtils.deleteConfigPath(commandRequest, tempDirectory.toString());
assertTrue(Files.notExists(subDirectory));
assertTrue(Files.exists(tempDirectory));
//cleanup
Files.deleteIfExists(tempDirectory);
assertFalse(Files.exists(tempDirectory));
}
}

0 comments on commit c969f60

Please sign in to comment.