Skip to content

Commit

Permalink
Import from descriptor now supports dependencies #124
Browse files Browse the repository at this point in the history
  • Loading branch information
tillias committed Jan 15, 2021
1 parent d9cbb22 commit 74a80cc
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -17,6 +19,7 @@
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;

@Service
Expand All @@ -27,18 +30,20 @@ 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;

private final FullMicroserviceMapper microserviceMapper;

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;
Expand All @@ -57,7 +62,7 @@ public Optional<FullMicroserviceDto> importFromDescriptor(final MicroserviceImpo
return Optional.empty();
} else {
final Microservice microservice = persistMicroservice(descriptorDto);
importDependencies(descriptorDto);
importDependencies(microservice, descriptorDto.getDependencies());

return Optional.of(microserviceMapper.microserviceToDto(microservice));
}
Expand Down Expand Up @@ -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<MicroserviceImportDescriptorDto> 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<FullMicroserviceDto> importMicroservice(@RequestBody MicroserviceImportDescriptorDto descriptor) {
return ResponseEntity.of(importService.importFromDescriptor(descriptor));
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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.*;

Expand All @@ -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()
Expand Down Expand Up @@ -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<FullMicroserviceDto> maybeDto = sut.importFromDescriptor(descriptorDto);
assertTrue(maybeDto.isPresent());

final List<Dependency> dependenciesForNewMicroservice = getDependenciesFor(maybeDto.get().getId());
List<Microservice> dependentMicroservices =
dependenciesForNewMicroservice.stream().map(Dependency::getTarget).collect(Collectors.toList());

assertThat(dependentMicroservices)
.extracting(Microservice::getName)
.containsExactlyInAnyOrder("Test Dependent Service1", "Test Dependent Service2");
}

private List<Dependency> getDependenciesFor(final Long microserviceId) {
final List<Dependency> allDependencies = dependencyRepository.findAll();
return allDependencies.stream()
.filter(d -> Objects.equals(d.getSource().getId(), microserviceId))
.collect(Collectors.toList());
}
}

0 comments on commit 74a80cc

Please sign in to comment.