From 74a80cc8abad12cf58443ea959ee4d5d4621d670 Mon Sep 17 00:00:00 2001 From: tillias Date: Fri, 15 Jan 2021 20:57:39 +0100 Subject: [PATCH] Import from descriptor now supports dependencies #124 --- .../service/custom/ImportService.java | 31 ++++++++----- .../web/rest/custom/ImportResource.java | 28 ++++++++++++ .../service/custom/ImportServiceTest.java | 43 +++++++++++++++++++ 3 files changed, 91 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/github/microcatalog/web/rest/custom/ImportResource.java diff --git a/src/main/java/com/github/microcatalog/service/custom/ImportService.java b/src/main/java/com/github/microcatalog/service/custom/ImportService.java index 67423b5..2a864f7 100644 --- a/src/main/java/com/github/microcatalog/service/custom/ImportService.java +++ b/src/main/java/com/github/microcatalog/service/custom/ImportService.java @@ -1,9 +1,11 @@ package com.github.microcatalog.service.custom; import com.github.microcatalog.config.ApplicationProperties; +import com.github.microcatalog.domain.Dependency; import com.github.microcatalog.domain.Microservice; import com.github.microcatalog.domain.Status; import com.github.microcatalog.domain.Team; +import com.github.microcatalog.repository.DependencyRepository; import com.github.microcatalog.repository.MicroserviceRepository; import com.github.microcatalog.repository.StatusRepository; import com.github.microcatalog.repository.TeamRepository; @@ -17,6 +19,7 @@ import org.springframework.stereotype.Service; import javax.transaction.Transactional; +import java.util.List; import java.util.Optional; @Service @@ -27,6 +30,7 @@ public class ImportService { private final ApplicationProperties.Imports.Defaults defaults; private final MicroserviceRepository microserviceRepository; + private final DependencyRepository dependencyRepository; private final StatusRepository statusRepository; private final TeamRepository teamRepository; @@ -34,11 +38,12 @@ public class ImportService { public ImportService(ApplicationProperties applicationProperties, MicroserviceRepository microserviceRepository, - StatusRepository statusRepository, + DependencyRepository dependencyRepository, StatusRepository statusRepository, TeamRepository teamRepository, FullMicroserviceMapper microserviceMapper) { this.defaults = applicationProperties.getImports().getDefaults(); this.microserviceRepository = microserviceRepository; + this.dependencyRepository = dependencyRepository; this.statusRepository = statusRepository; this.teamRepository = teamRepository; this.microserviceMapper = microserviceMapper; @@ -57,7 +62,7 @@ public Optional importFromDescriptor(final MicroserviceImpo return Optional.empty(); } else { final Microservice microservice = persistMicroservice(descriptorDto); - importDependencies(descriptorDto); + importDependencies(microservice, descriptorDto.getDependencies()); return Optional.of(microserviceMapper.microserviceToDto(microservice)); } @@ -142,15 +147,19 @@ private Team getOrCreateTeam(final String name) { return teamRepository.save(newTeam); } - private boolean importDependencies(final MicroserviceImportDescriptorDto descriptorDto) { - try { - // TODO - return true; - } catch (Exception ex) { - log.error("Error importing dependencies", ex); - } + private void importDependencies(final Microservice source, final List dependencies) { + for (MicroserviceImportDescriptorDto d : dependencies) { + Microservice target = microserviceRepository.findByName(d.getName()); + if (target == null) { + target = persistMicroservice(d); + } - return false; - } + final Dependency dependency = new Dependency() + .name(String.format("%s -> %s", source.getName(), target.getName())) + .source(source) + .target(target); + dependencyRepository.save(dependency); + } + } } diff --git a/src/main/java/com/github/microcatalog/web/rest/custom/ImportResource.java b/src/main/java/com/github/microcatalog/web/rest/custom/ImportResource.java new file mode 100644 index 0000000..03b13e3 --- /dev/null +++ b/src/main/java/com/github/microcatalog/web/rest/custom/ImportResource.java @@ -0,0 +1,28 @@ +package com.github.microcatalog.web.rest.custom; + +import com.github.microcatalog.service.custom.ImportService; +import com.github.microcatalog.service.dto.custom.FullMicroserviceDto; +import com.github.microcatalog.service.dto.custom.MicroserviceImportDescriptorDto; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * REST controller for importing {@link com.github.microcatalog.domain.Microservice} with {@link com.github.microcatalog.domain.Dependency} + */ +@RestController +@RequestMapping("/api") +public class ImportResource { + private final ImportService importService; + + public ImportResource(ImportService importService) { + this.importService = importService; + } + + @PostMapping + public ResponseEntity importMicroservice(@RequestBody MicroserviceImportDescriptorDto descriptor) { + return ResponseEntity.of(importService.importFromDescriptor(descriptor)); + } +} diff --git a/src/test/java/com/github/microcatalog/service/custom/ImportServiceTest.java b/src/test/java/com/github/microcatalog/service/custom/ImportServiceTest.java index 665401c..07a6a01 100644 --- a/src/test/java/com/github/microcatalog/service/custom/ImportServiceTest.java +++ b/src/test/java/com/github/microcatalog/service/custom/ImportServiceTest.java @@ -1,6 +1,10 @@ package com.github.microcatalog.service.custom; import com.github.microcatalog.config.ApplicationProperties; +import com.github.microcatalog.domain.Dependency; +import com.github.microcatalog.domain.Microservice; +import com.github.microcatalog.repository.DependencyRepository; +import com.github.microcatalog.repository.MicroserviceRepository; import com.github.microcatalog.service.custom.exceptions.ImportException; import com.github.microcatalog.service.dto.custom.FullMicroserviceDto; import com.github.microcatalog.service.dto.custom.MicroserviceImportDescriptorDto; @@ -10,9 +14,14 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.transaction.annotation.Transactional; +import java.util.Arrays; +import java.util.List; +import java.util.Objects; import java.util.Optional; +import java.util.stream.Collectors; import static com.github.microcatalog.service.dto.custom.builder.MicroserviceImportDescriptorDtoBuilder.aMicroserviceImportDescriptorDto; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.jupiter.api.Assertions.*; @@ -26,6 +35,12 @@ class ImportServiceTest { @Autowired private ApplicationProperties applicationProperties; + @Autowired + private DependencyRepository dependencyRepository; + + @Autowired + private MicroserviceRepository microserviceRepository; + @Test void importFromDescriptor_missingName_Exception() { final MicroserviceImportDescriptorDto descriptorDto = aMicroserviceImportDescriptorDto() @@ -95,4 +110,32 @@ void importFromDescriptor_overrideDefaultValues_Success() { assertEquals(descriptorDto.getTeam(), team.getName()); } + @Test + void importFromDescriptor_withDependencies_Success() { + final MicroserviceImportDescriptorDto descriptorDto = + aMicroserviceImportDescriptorDto() + .withName("Test Imported Microservice 3") + .withDependencies(Arrays.asList( + aMicroserviceImportDescriptorDto().withName("Test Dependent Service1").build(), + aMicroserviceImportDescriptorDto().withName("Test Dependent Service2").build() + )).build(); + + final Optional maybeDto = sut.importFromDescriptor(descriptorDto); + assertTrue(maybeDto.isPresent()); + + final List dependenciesForNewMicroservice = getDependenciesFor(maybeDto.get().getId()); + List dependentMicroservices = + dependenciesForNewMicroservice.stream().map(Dependency::getTarget).collect(Collectors.toList()); + + assertThat(dependentMicroservices) + .extracting(Microservice::getName) + .containsExactlyInAnyOrder("Test Dependent Service1", "Test Dependent Service2"); + } + + private List getDependenciesFor(final Long microserviceId) { + final List allDependencies = dependencyRepository.findAll(); + return allDependencies.stream() + .filter(d -> Objects.equals(d.getSource().getId(), microserviceId)) + .collect(Collectors.toList()); + } }