diff --git a/.jhipster/Microservice.json b/.jhipster/Microservice.json index 334c236..381f646 100644 --- a/.jhipster/Microservice.json +++ b/.jhipster/Microservice.json @@ -5,7 +5,8 @@ "fieldName": "name", "fieldType": "String", "fieldValidateRules": [ - "required" + "required", + "unique" ] }, { diff --git a/.jhipster/Status.json b/.jhipster/Status.json index f185a86..b1aff6b 100644 --- a/.jhipster/Status.json +++ b/.jhipster/Status.json @@ -5,7 +5,8 @@ "fieldName": "name", "fieldType": "String", "fieldValidateRules": [ - "required" + "required", + "unique" ] }, { diff --git a/.jhipster/Team.json b/.jhipster/Team.json index bde61bc..768cb13 100644 --- a/.jhipster/Team.json +++ b/.jhipster/Team.json @@ -5,7 +5,8 @@ "fieldName": "name", "fieldType": "String", "fieldValidateRules": [ - "required" + "required", + "unique" ] }, { diff --git a/microcatalog.jdl b/microcatalog.jdl index eacc5fe..93d35b3 100644 --- a/microcatalog.jdl +++ b/microcatalog.jdl @@ -1,18 +1,18 @@ // Core entities entity Team { - name String required, + name String required unique, teamLead String required, productOwner String required } entity Status { - name String required, + name String required unique, description String } entity Microservice { - name String required, + name String required unique, description TextBlob required, imageUrl String required, swaggerUrl String required, diff --git a/src/main/java/com/github/microcatalog/config/ApplicationProperties.java b/src/main/java/com/github/microcatalog/config/ApplicationProperties.java index 593f04c..a433c85 100644 --- a/src/main/java/com/github/microcatalog/config/ApplicationProperties.java +++ b/src/main/java/com/github/microcatalog/config/ApplicationProperties.java @@ -11,11 +11,16 @@ @ConfigurationProperties(prefix = "application", ignoreUnknownFields = false) public class ApplicationProperties { private final ApplicationProperties.IntegrationTests integrationTests = new IntegrationTests(); + private final ApplicationProperties.Imports imports = new Imports(); public IntegrationTests getIntegrationTests() { return integrationTests; } + public Imports getImports() { + return imports; + } + public static class IntegrationTests { private final IntegrationTests.Jenkins jenkins = new Jenkins(); @@ -63,4 +68,105 @@ public void setCrumbIssuer(String crumbIssuer) { } } } + + public static class Imports { + private final Imports.Defaults defaults = new Defaults(); + + public Defaults getDefaults() { + return defaults; + } + + public static class Defaults { + private String description; + private String imageUrl; + private String apiUrl; + private String gitUrl; + private String ciUrl; + private String status; + private final Defaults.Team team = new Team(); + + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getImageUrl() { + return imageUrl; + } + + public void setImageUrl(String imageUrl) { + this.imageUrl = imageUrl; + } + + public String getApiUrl() { + return apiUrl; + } + + public void setApiUrl(String apiUrl) { + this.apiUrl = apiUrl; + } + + public String getGitUrl() { + return gitUrl; + } + + public void setGitUrl(String gitUrl) { + this.gitUrl = gitUrl; + } + + public String getCiUrl() { + return ciUrl; + } + + public void setCiUrl(String ciUrl) { + this.ciUrl = ciUrl; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public Team getTeam() { + return team; + } + + public static class Team { + private String name; + private String po; + private String tl; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPo() { + return po; + } + + public void setPo(String po) { + this.po = po; + } + + public String getTl() { + return tl; + } + + public void setTl(String tl) { + this.tl = tl; + } + } + } + } } diff --git a/src/main/java/com/github/microcatalog/domain/Microservice.java b/src/main/java/com/github/microcatalog/domain/Microservice.java index 2444509..30b184f 100644 --- a/src/main/java/com/github/microcatalog/domain/Microservice.java +++ b/src/main/java/com/github/microcatalog/domain/Microservice.java @@ -26,10 +26,10 @@ public class Microservice implements Serializable { private Long id; @NotNull - @Column(name = "name", nullable = false) + @Column(name = "name", nullable = false, unique = true) private String name; - + @Lob @Type(type = "org.hibernate.type.TextType") @Column(name = "description", nullable = false) @@ -188,11 +188,7 @@ public boolean equals(Object o) { @Override public int hashCode() { - if (id == null) { - return 31; - } - - return id.intValue(); + return 31; } // prettier-ignore diff --git a/src/main/java/com/github/microcatalog/domain/Status.java b/src/main/java/com/github/microcatalog/domain/Status.java index 71166dc..b49c668 100644 --- a/src/main/java/com/github/microcatalog/domain/Status.java +++ b/src/main/java/com/github/microcatalog/domain/Status.java @@ -24,7 +24,7 @@ public class Status implements Serializable { private Long id; @NotNull - @Column(name = "name", nullable = false) + @Column(name = "name", nullable = false, unique = true) private String name; @Column(name = "description") diff --git a/src/main/java/com/github/microcatalog/domain/Team.java b/src/main/java/com/github/microcatalog/domain/Team.java index d49b8d1..132ae52 100644 --- a/src/main/java/com/github/microcatalog/domain/Team.java +++ b/src/main/java/com/github/microcatalog/domain/Team.java @@ -24,7 +24,7 @@ public class Team implements Serializable { private Long id; @NotNull - @Column(name = "name", nullable = false) + @Column(name = "name", nullable = false, unique = true) private String name; @NotNull diff --git a/src/main/java/com/github/microcatalog/repository/MicroserviceRepository.java b/src/main/java/com/github/microcatalog/repository/MicroserviceRepository.java index 9a0225e..aac411e 100644 --- a/src/main/java/com/github/microcatalog/repository/MicroserviceRepository.java +++ b/src/main/java/com/github/microcatalog/repository/MicroserviceRepository.java @@ -4,8 +4,6 @@ import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; -import java.util.List; - /** * Spring Data repository for the Microservice entity. */ @@ -14,5 +12,5 @@ public interface MicroserviceRepository extends JpaRepository { Long deleteByName(String name); - List findByName(String name); + Microservice findByName(String name); } diff --git a/src/main/java/com/github/microcatalog/repository/StatusRepository.java b/src/main/java/com/github/microcatalog/repository/StatusRepository.java index 6ab8a82..c2a6cf3 100644 --- a/src/main/java/com/github/microcatalog/repository/StatusRepository.java +++ b/src/main/java/com/github/microcatalog/repository/StatusRepository.java @@ -1,14 +1,18 @@ package com.github.microcatalog.repository; +import com.github.microcatalog.domain.Microservice; import com.github.microcatalog.domain.Status; import org.springframework.data.jpa.repository.*; import org.springframework.stereotype.Repository; +import java.util.List; + /** * Spring Data repository for the Status entity. */ @SuppressWarnings("unused") @Repository public interface StatusRepository extends JpaRepository { + Status findByName(String name); } diff --git a/src/main/java/com/github/microcatalog/repository/TeamRepository.java b/src/main/java/com/github/microcatalog/repository/TeamRepository.java index a39e5e6..fad9a52 100644 --- a/src/main/java/com/github/microcatalog/repository/TeamRepository.java +++ b/src/main/java/com/github/microcatalog/repository/TeamRepository.java @@ -1,8 +1,7 @@ package com.github.microcatalog.repository; import com.github.microcatalog.domain.Team; - -import org.springframework.data.jpa.repository.*; +import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; /** @@ -11,4 +10,5 @@ @SuppressWarnings("unused") @Repository public interface TeamRepository extends JpaRepository { + Team findByName(String name); } diff --git a/src/main/java/com/github/microcatalog/service/custom/DependencyService.java b/src/main/java/com/github/microcatalog/service/custom/DependencyService.java index 4330976..41f56af 100644 --- a/src/main/java/com/github/microcatalog/service/custom/DependencyService.java +++ b/src/main/java/com/github/microcatalog/service/custom/DependencyService.java @@ -64,20 +64,20 @@ public Dependency create(final Dependency dependency) { * @return created dependency */ public DependencyDto create(final String dependencyName, final String sourceName, final String targetName) { - final Optional maybeSource = microserviceRepository.findByName(sourceName).stream().findFirst(); - final Optional maybeTarget = microserviceRepository.findByName(targetName).stream().findFirst(); + final Microservice source = microserviceRepository.findByName(sourceName); + final Microservice target = microserviceRepository.findByName(targetName); - if (!maybeSource.isPresent()) { + if (source == null) { throw new IllegalArgumentException(String.format("Source microservice with name [%s] doesn't exist", sourceName)); } - if (!maybeTarget.isPresent()) { + if (target == null) { throw new IllegalArgumentException(String.format("Target microservice with name [%s] doesn't exist", targetName)); } final Dependency dependency = new Dependency().name(dependencyName) - .source(maybeSource.get()) - .target(maybeTarget.get()); + .source(source) + .target(target); return dependencyDto(create(dependency)); } diff --git a/src/main/java/com/github/microcatalog/service/custom/ImportService.java b/src/main/java/com/github/microcatalog/service/custom/ImportService.java new file mode 100644 index 0000000..67423b5 --- /dev/null +++ b/src/main/java/com/github/microcatalog/service/custom/ImportService.java @@ -0,0 +1,156 @@ +package com.github.microcatalog.service.custom; + +import com.github.microcatalog.config.ApplicationProperties; +import com.github.microcatalog.domain.Microservice; +import com.github.microcatalog.domain.Status; +import com.github.microcatalog.domain.Team; +import com.github.microcatalog.repository.MicroserviceRepository; +import com.github.microcatalog.repository.StatusRepository; +import com.github.microcatalog.repository.TeamRepository; +import com.github.microcatalog.service.custom.exceptions.ImportException; +import com.github.microcatalog.service.dto.custom.FullMicroserviceDto; +import com.github.microcatalog.service.dto.custom.MicroserviceImportDescriptorDto; +import com.github.microcatalog.service.mapper.FullMicroserviceMapper; +import org.apache.commons.lang3.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + +import javax.transaction.Transactional; +import java.util.Optional; + +@Service +@Transactional +public class ImportService { + + private final Logger log = LoggerFactory.getLogger(ImportService.class); + + private final ApplicationProperties.Imports.Defaults defaults; + private final MicroserviceRepository microserviceRepository; + private final StatusRepository statusRepository; + private final TeamRepository teamRepository; + + private final FullMicroserviceMapper microserviceMapper; + + public ImportService(ApplicationProperties applicationProperties, + MicroserviceRepository microserviceRepository, + StatusRepository statusRepository, + TeamRepository teamRepository, + FullMicroserviceMapper microserviceMapper) { + this.defaults = applicationProperties.getImports().getDefaults(); + this.microserviceRepository = microserviceRepository; + this.statusRepository = statusRepository; + this.teamRepository = teamRepository; + this.microserviceMapper = microserviceMapper; + } + + public Optional importFromDescriptor(final MicroserviceImportDescriptorDto descriptorDto) { + final String name = descriptorDto.getName(); + if (StringUtils.isBlank(name)) { + throw new ImportException("Microservice name can't be null"); + } + + + final Microservice persistent = microserviceRepository.findByName(name); + if (persistent != null) { + log.warn("Microservice with name {} already exists. Stopping import", name); + return Optional.empty(); + } else { + final Microservice microservice = persistMicroservice(descriptorDto); + importDependencies(descriptorDto); + + return Optional.of(microserviceMapper.microserviceToDto(microservice)); + } + } + + private Microservice persistMicroservice(final MicroserviceImportDescriptorDto descriptorDto) { + final Microservice microservice = new Microservice(); + microservice.setName(descriptorDto.getName()); + + final String description = descriptorDto.getDescription(); + if (StringUtils.isBlank(description)) { + microservice.setDescription(defaults.getDescription()); + } else { + microservice.setDescription(description); + } + + final String imageUrl = descriptorDto.getImageUrl(); + if (StringUtils.isBlank(imageUrl)) { + microservice.setImageUrl(defaults.getImageUrl()); + } else { + microservice.setImageUrl(imageUrl); + } + + final String apiUrl = descriptorDto.getApiUrl(); + if (StringUtils.isBlank(apiUrl)) { + microservice.setSwaggerUrl(defaults.getApiUrl()); + } else { + microservice.setSwaggerUrl(apiUrl); + } + + final String gitUrl = descriptorDto.getGitUrl(); + if (StringUtils.isBlank(gitUrl)) { + microservice.setGitUrl(defaults.getGitUrl()); + } else { + microservice.setGitUrl(gitUrl); + } + + final String ciUrl = descriptorDto.getCiUrl(); + if (StringUtils.isBlank(ciUrl)) { + microservice.setCiUrl(defaults.getCiUrl()); + } else { + microservice.setCiUrl(ciUrl); + } + + String statusName = descriptorDto.getStatus(); + if (StringUtils.isBlank(statusName)) { + statusName = defaults.getStatus(); + } + final Status status = getOrCreateStatus(statusName); + microservice.setStatus(status); + + String teamName = descriptorDto.getTeam(); + if (StringUtils.isBlank(teamName)) { + teamName = defaults.getTeam().getName(); + } + final Team team = getOrCreateTeam(teamName); + microservice.setTeam(team); + + return microserviceRepository.save(microservice); + } + + private Status getOrCreateStatus(final String name) { + final Status persistent = statusRepository.findByName(name); + if (persistent != null) { + return persistent; + } + + final Status newStatus = new Status().name(name); + return statusRepository.save(newStatus); + } + + private Team getOrCreateTeam(final String name) { + final Team persistent = teamRepository.findByName(name); + if (persistent != null) { + return persistent; + } + + final Team newTeam = new Team() + .name(name) + .teamLead(defaults.getTeam().getTl()) + .productOwner(defaults.getTeam().getPo()); + return teamRepository.save(newTeam); + } + + private boolean importDependencies(final MicroserviceImportDescriptorDto descriptorDto) { + try { + // TODO + return true; + } catch (Exception ex) { + log.error("Error importing dependencies", ex); + } + + return false; + } + +} diff --git a/src/main/java/com/github/microcatalog/service/custom/exceptions/ImportException.java b/src/main/java/com/github/microcatalog/service/custom/exceptions/ImportException.java new file mode 100644 index 0000000..a569759 --- /dev/null +++ b/src/main/java/com/github/microcatalog/service/custom/exceptions/ImportException.java @@ -0,0 +1,12 @@ +package com.github.microcatalog.service.custom.exceptions; + +/** + * Used by {@link com.github.microcatalog.service.custom.ImportService} + */ +public class ImportException extends RuntimeException { + private static final long serialVersionUID = -5092030802613208627L; + + public ImportException(String message) { + super(message); + } +} diff --git a/src/main/java/com/github/microcatalog/service/dto/custom/FullMicroserviceDto.java b/src/main/java/com/github/microcatalog/service/dto/custom/FullMicroserviceDto.java new file mode 100644 index 0000000..b459d23 --- /dev/null +++ b/src/main/java/com/github/microcatalog/service/dto/custom/FullMicroserviceDto.java @@ -0,0 +1,75 @@ +package com.github.microcatalog.service.dto.custom; + +import java.io.Serializable; + +public final class FullMicroserviceDto extends BaseDto implements Serializable { + private static final long serialVersionUID = -7994184124965371580L; + + private String description; + private String imageUrl; + private String swaggerUrl; + private String gitUrl; + private String ciUrl; + private TeamDto team; + private StatusDto status; + + public String getCiUrl() { + return ciUrl; + } + + public static long getSerialVersionUID() { + return serialVersionUID; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getImageUrl() { + return imageUrl; + } + + public void setImageUrl(String imageUrl) { + this.imageUrl = imageUrl; + } + + public String getSwaggerUrl() { + return swaggerUrl; + } + + public void setSwaggerUrl(String swaggerUrl) { + this.swaggerUrl = swaggerUrl; + } + + public String getGitUrl() { + return gitUrl; + } + + public void setGitUrl(String gitUrl) { + this.gitUrl = gitUrl; + } + + public void setCiUrl(String ciUrl) { + this.ciUrl = ciUrl; + } + + public TeamDto getTeam() { + return team; + } + + public void setTeam(TeamDto team) { + this.team = team; + } + + public StatusDto getStatus() { + return status; + } + + public void setStatus(StatusDto status) { + this.status = status; + } +} diff --git a/src/main/java/com/github/microcatalog/service/dto/custom/MicroserviceDto.java b/src/main/java/com/github/microcatalog/service/dto/custom/MicroserviceDto.java index a1dc579..a4a5bae 100644 --- a/src/main/java/com/github/microcatalog/service/dto/custom/MicroserviceDto.java +++ b/src/main/java/com/github/microcatalog/service/dto/custom/MicroserviceDto.java @@ -3,6 +3,7 @@ import java.io.Serializable; public final class MicroserviceDto extends BaseDto implements Serializable { + private static final long serialVersionUID = 4624953286552443974L; private String ciUrl; diff --git a/src/main/java/com/github/microcatalog/service/dto/custom/MicroserviceImportDescriptorDto.java b/src/main/java/com/github/microcatalog/service/dto/custom/MicroserviceImportDescriptorDto.java new file mode 100644 index 0000000..6c9ca14 --- /dev/null +++ b/src/main/java/com/github/microcatalog/service/dto/custom/MicroserviceImportDescriptorDto.java @@ -0,0 +1,87 @@ +package com.github.microcatalog.service.dto.custom; + +import java.util.List; + +public final class MicroserviceImportDescriptorDto { + private String name; + private String description; + private String imageUrl; + private String apiUrl; + private String ciUrl; + private String gitUrl; + private String team; + private String status; + private List dependencies; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getImageUrl() { + return imageUrl; + } + + public void setImageUrl(String imageUrl) { + this.imageUrl = imageUrl; + } + + public String getApiUrl() { + return apiUrl; + } + + public void setApiUrl(String apiUrl) { + this.apiUrl = apiUrl; + } + + public String getCiUrl() { + return ciUrl; + } + + public void setCiUrl(String ciUrl) { + this.ciUrl = ciUrl; + } + + public String getGitUrl() { + return gitUrl; + } + + public void setGitUrl(String gitUrl) { + this.gitUrl = gitUrl; + } + + public String getTeam() { + return team; + } + + public void setTeam(String team) { + this.team = team; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public List getDependencies() { + return dependencies; + } + + public void setDependencies(List dependencies) { + this.dependencies = dependencies; + } +} diff --git a/src/main/java/com/github/microcatalog/service/dto/custom/StatusDto.java b/src/main/java/com/github/microcatalog/service/dto/custom/StatusDto.java new file mode 100644 index 0000000..4e694ba --- /dev/null +++ b/src/main/java/com/github/microcatalog/service/dto/custom/StatusDto.java @@ -0,0 +1,6 @@ +package com.github.microcatalog.service.dto.custom; + +import java.io.Serializable; + +public class StatusDto extends BaseDto implements Serializable { +} diff --git a/src/main/java/com/github/microcatalog/service/dto/custom/TeamDto.java b/src/main/java/com/github/microcatalog/service/dto/custom/TeamDto.java new file mode 100644 index 0000000..65a7efd --- /dev/null +++ b/src/main/java/com/github/microcatalog/service/dto/custom/TeamDto.java @@ -0,0 +1,7 @@ +package com.github.microcatalog.service.dto.custom; + +import java.io.Serializable; + +public final class TeamDto extends BaseDto implements Serializable { + private static final long serialVersionUID = 7604902444444032842L; +} diff --git a/src/main/java/com/github/microcatalog/service/dto/custom/builder/MicroserviceImportDescriptorDtoBuilder.java b/src/main/java/com/github/microcatalog/service/dto/custom/builder/MicroserviceImportDescriptorDtoBuilder.java new file mode 100644 index 0000000..208ff54 --- /dev/null +++ b/src/main/java/com/github/microcatalog/service/dto/custom/builder/MicroserviceImportDescriptorDtoBuilder.java @@ -0,0 +1,83 @@ +package com.github.microcatalog.service.dto.custom.builder; + +import com.github.microcatalog.service.dto.custom.MicroserviceImportDescriptorDto; + +import java.util.List; + +public final class MicroserviceImportDescriptorDtoBuilder { + private String name; + private String description; + private String imageUrl; + private String apiUrl; + private String ciUrl; + private String gitUrl; + private String team; + private String status; + private List dependencies; + + private MicroserviceImportDescriptorDtoBuilder() { + } + + public static MicroserviceImportDescriptorDtoBuilder aMicroserviceImportDescriptorDto() { + return new MicroserviceImportDescriptorDtoBuilder(); + } + + public MicroserviceImportDescriptorDtoBuilder withName(String name) { + this.name = name; + return this; + } + + public MicroserviceImportDescriptorDtoBuilder withDescription(String description) { + this.description = description; + return this; + } + + public MicroserviceImportDescriptorDtoBuilder withImageUrl(String imageUrl) { + this.imageUrl = imageUrl; + return this; + } + + public MicroserviceImportDescriptorDtoBuilder withApiUrl(String apiUrl) { + this.apiUrl = apiUrl; + return this; + } + + public MicroserviceImportDescriptorDtoBuilder withCiUrl(String ciUrl) { + this.ciUrl = ciUrl; + return this; + } + + public MicroserviceImportDescriptorDtoBuilder withGitUrl(String gitUrl) { + this.gitUrl = gitUrl; + return this; + } + + public MicroserviceImportDescriptorDtoBuilder withTeam(String team) { + this.team = team; + return this; + } + + public MicroserviceImportDescriptorDtoBuilder withStatus(String status) { + this.status = status; + return this; + } + + public MicroserviceImportDescriptorDtoBuilder withDependencies(List dependencies) { + this.dependencies = dependencies; + return this; + } + + public MicroserviceImportDescriptorDto build() { + MicroserviceImportDescriptorDto microserviceImportDescriptorDto = new MicroserviceImportDescriptorDto(); + microserviceImportDescriptorDto.setName(name); + microserviceImportDescriptorDto.setDescription(description); + microserviceImportDescriptorDto.setImageUrl(imageUrl); + microserviceImportDescriptorDto.setApiUrl(apiUrl); + microserviceImportDescriptorDto.setCiUrl(ciUrl); + microserviceImportDescriptorDto.setGitUrl(gitUrl); + microserviceImportDescriptorDto.setTeam(team); + microserviceImportDescriptorDto.setStatus(status); + microserviceImportDescriptorDto.setDependencies(dependencies); + return microserviceImportDescriptorDto; + } +} diff --git a/src/main/java/com/github/microcatalog/service/mapper/FullMicroserviceMapper.java b/src/main/java/com/github/microcatalog/service/mapper/FullMicroserviceMapper.java new file mode 100644 index 0000000..eda7ed5 --- /dev/null +++ b/src/main/java/com/github/microcatalog/service/mapper/FullMicroserviceMapper.java @@ -0,0 +1,10 @@ +package com.github.microcatalog.service.mapper; + +import com.github.microcatalog.domain.Microservice; +import com.github.microcatalog.service.dto.custom.FullMicroserviceDto; +import org.mapstruct.Mapper; + +@Mapper(componentModel = "spring") +public interface FullMicroserviceMapper { + FullMicroserviceDto microserviceToDto(Microservice microservice); +} diff --git a/src/main/java/com/github/microcatalog/service/mapper/StatusMapper.java b/src/main/java/com/github/microcatalog/service/mapper/StatusMapper.java new file mode 100644 index 0000000..1749ff3 --- /dev/null +++ b/src/main/java/com/github/microcatalog/service/mapper/StatusMapper.java @@ -0,0 +1,10 @@ +package com.github.microcatalog.service.mapper; + +import com.github.microcatalog.domain.Status; +import com.github.microcatalog.service.dto.custom.StatusDto; +import org.mapstruct.Mapper; + +@Mapper(componentModel = "spring") +public interface StatusMapper { + StatusDto statusToDto(Status status); +} diff --git a/src/main/java/com/github/microcatalog/service/mapper/TeamMapper.java b/src/main/java/com/github/microcatalog/service/mapper/TeamMapper.java new file mode 100644 index 0000000..3d6b7a8 --- /dev/null +++ b/src/main/java/com/github/microcatalog/service/mapper/TeamMapper.java @@ -0,0 +1,10 @@ +package com.github.microcatalog.service.mapper; + +import com.github.microcatalog.domain.Team; +import com.github.microcatalog.service.dto.custom.TeamDto; +import org.mapstruct.Mapper; + +@Mapper(componentModel = "spring") +public interface TeamMapper { + TeamDto teamToDto(Team team); +} diff --git a/src/main/resources/config/application.yml b/src/main/resources/config/application.yml index 6115219..b459c39 100644 --- a/src/main/resources/config/application.yml +++ b/src/main/resources/config/application.yml @@ -162,4 +162,16 @@ jhipster: # https://www.jhipster.tech/common-application-properties/ # =================================================================== -# application: +application: + imports: + defaults: + description: Default description because nothing was provided during import + imageUrl: IMPORT_IMAGE + apiUrl: IMPORT_API_URL + gitUrl: IMPORT_GIT_URL + ciUrl: IMPORT_CI_URL + status: IMPORT_STATUS + team: + name: IMPORT_TEAM + po: IMPORT_PO + tl: IMPORT_TL diff --git a/src/main/resources/config/liquibase/changelog/20201001051657_added_entity_Microservice.xml b/src/main/resources/config/liquibase/changelog/20201001051657_added_entity_Microservice.xml index 5cea077..9cf1941 100644 --- a/src/main/resources/config/liquibase/changelog/20201001051657_added_entity_Microservice.xml +++ b/src/main/resources/config/liquibase/changelog/20201001051657_added_entity_Microservice.xml @@ -16,7 +16,7 @@ - + diff --git a/src/main/resources/config/liquibase/changelog/20201001051757_added_entity_Team.xml b/src/main/resources/config/liquibase/changelog/20201001051757_added_entity_Team.xml index d40ca2b..ad50e60 100644 --- a/src/main/resources/config/liquibase/changelog/20201001051757_added_entity_Team.xml +++ b/src/main/resources/config/liquibase/changelog/20201001051757_added_entity_Team.xml @@ -16,7 +16,7 @@ - + diff --git a/src/main/resources/config/liquibase/changelog/20201003191729_added_entity_Status.xml b/src/main/resources/config/liquibase/changelog/20201003191729_added_entity_Status.xml index d8c0426..1b84851 100644 --- a/src/main/resources/config/liquibase/changelog/20201003191729_added_entity_Status.xml +++ b/src/main/resources/config/liquibase/changelog/20201003191729_added_entity_Status.xml @@ -16,7 +16,7 @@ - + diff --git a/src/test/java/com/github/microcatalog/service/custom/DependencyServiceTest.java b/src/test/java/com/github/microcatalog/service/custom/DependencyServiceTest.java index d20dc20..1404982 100644 --- a/src/test/java/com/github/microcatalog/service/custom/DependencyServiceTest.java +++ b/src/test/java/com/github/microcatalog/service/custom/DependencyServiceTest.java @@ -218,7 +218,7 @@ void create_Success() { void create_SourceNotExists_Exception() { final String sourceName = "TestService1"; - given(microserviceRepository.findByName(sourceName)).willReturn(Collections.emptyList()); + given(microserviceRepository.findByName(sourceName)).willReturn(null); assertThatIllegalArgumentException() .isThrownBy(() -> service.create("New Dependency", sourceName, "some target")) @@ -230,8 +230,8 @@ void create_TargetNotExists_Exception() { final String sourceName = "TestService1"; final String targetName = "TestService2"; - given(microserviceRepository.findByName(sourceName)).willReturn(Collections.singletonList(microservice(1))); - given(microserviceRepository.findByName(targetName)).willReturn(Collections.emptyList()); + given(microserviceRepository.findByName(sourceName)).willReturn(microservice(1)); + given(microserviceRepository.findByName(targetName)).willReturn(null); assertThatIllegalArgumentException() .isThrownBy(() -> service.create("New Dependency", sourceName, targetName)) @@ -253,8 +253,8 @@ void create_ValidData_Success() { ) ) ); - given(microserviceRepository.findByName(sourceName)).willReturn(Collections.singletonList(microservice(1))); - given(microserviceRepository.findByName(targetName)).willReturn(Collections.singletonList(microservice(2))); + given(microserviceRepository.findByName(sourceName)).willReturn(microservice(1)); + given(microserviceRepository.findByName(targetName)).willReturn(microservice(2)); given(repository.save(any())).willReturn(dependency(1, 1, 2)); final DependencyDto actual = service.create(dependencyName, sourceName, targetName); diff --git a/src/test/java/com/github/microcatalog/service/custom/ImportServiceBorderTest.java b/src/test/java/com/github/microcatalog/service/custom/ImportServiceBorderTest.java new file mode 100644 index 0000000..2770ff6 --- /dev/null +++ b/src/test/java/com/github/microcatalog/service/custom/ImportServiceBorderTest.java @@ -0,0 +1,46 @@ +package com.github.microcatalog.service.custom; + +import com.github.microcatalog.domain.Microservice; +import com.github.microcatalog.repository.MicroserviceRepository; +import com.github.microcatalog.service.dto.custom.FullMicroserviceDto; +import com.github.microcatalog.service.dto.custom.MicroserviceImportDescriptorDto; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; + +import java.util.Optional; + +import static com.github.microcatalog.service.dto.custom.builder.MicroserviceImportDescriptorDtoBuilder.aMicroserviceImportDescriptorDto; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.BDDMockito.given; + +/** + * Workaround for https://github.com/spring-projects/spring-boot/issues/7033 so we can't use @SpyBean in {@link ImportServiceTest} + */ +@SpringBootTest +class ImportServiceBorderTest { + + @MockBean + private MicroserviceRepository repository; + + @Autowired + private ImportService sut; + + + @Test + void importFromDescriptor_microserviceWithNameAlreadyExists_NoException() { + final String name = "Existing microservice"; + given(repository.findByName(name)).willReturn(new Microservice()); + + final MicroserviceImportDescriptorDto descriptorDto = + aMicroserviceImportDescriptorDto() + .withName(name) + .build(); + + final Optional maybeDto = sut.importFromDescriptor(descriptorDto); + assertFalse(maybeDto.isPresent()); + } + +} diff --git a/src/test/java/com/github/microcatalog/service/custom/ImportServiceTest.java b/src/test/java/com/github/microcatalog/service/custom/ImportServiceTest.java new file mode 100644 index 0000000..665401c --- /dev/null +++ b/src/test/java/com/github/microcatalog/service/custom/ImportServiceTest.java @@ -0,0 +1,98 @@ +package com.github.microcatalog.service.custom; + +import com.github.microcatalog.config.ApplicationProperties; +import com.github.microcatalog.service.custom.exceptions.ImportException; +import com.github.microcatalog.service.dto.custom.FullMicroserviceDto; +import com.github.microcatalog.service.dto.custom.MicroserviceImportDescriptorDto; +import com.github.microcatalog.service.dto.custom.TeamDto; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.transaction.annotation.Transactional; + +import java.util.Optional; + +import static com.github.microcatalog.service.dto.custom.builder.MicroserviceImportDescriptorDtoBuilder.aMicroserviceImportDescriptorDto; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest +@Transactional +class ImportServiceTest { + + @Autowired + private ImportService sut; + + @Autowired + private ApplicationProperties applicationProperties; + + @Test + void importFromDescriptor_missingName_Exception() { + final MicroserviceImportDescriptorDto descriptorDto = aMicroserviceImportDescriptorDto() + .withName(null) + .build(); + + assertThatThrownBy(() -> sut.importFromDescriptor(descriptorDto)).isInstanceOf(ImportException.class) + .hasMessageStartingWith("Microservice name can't be null"); + } + + @Test + void importFromDescriptor_noDependencies_Success() { + final MicroserviceImportDescriptorDto descriptorDto = + aMicroserviceImportDescriptorDto() + .withName("Test Imported Microservice") + .build(); + + final Optional maybeDto = sut.importFromDescriptor(descriptorDto); + assertTrue(maybeDto.isPresent()); + + final FullMicroserviceDto dto = maybeDto.get(); + assertNotNull(dto); + assertEquals(descriptorDto.getName(), dto.getName()); + + ApplicationProperties.Imports.Defaults defaults = applicationProperties.getImports().getDefaults(); + assertEquals(defaults.getDescription(), dto.getDescription()); + assertEquals(defaults.getImageUrl(), dto.getImageUrl()); + assertEquals(defaults.getApiUrl(), dto.getSwaggerUrl()); + assertEquals(defaults.getGitUrl(), dto.getGitUrl()); + assertEquals(defaults.getCiUrl(), dto.getCiUrl()); + assertNotNull(dto.getStatus()); + assertEquals(defaults.getStatus(), dto.getStatus().getName()); + assertNotNull(dto.getTeam()); + + final TeamDto team = dto.getTeam(); + assertEquals(defaults.getTeam().getName(), team.getName()); + } + + @Test + void importFromDescriptor_overrideDefaultValues_Success() { + final MicroserviceImportDescriptorDto descriptorDto = + aMicroserviceImportDescriptorDto() + .withName("Test Imported Microservice 2") + .withDescription("Test description") + .withImageUrl("Test image url") + .withApiUrl("Test api url") + .withGitUrl("Test git url") + .withCiUrl("Test ci url") + .withTeam("Test team") + .withStatus("Test status") + .build(); + + final Optional maybeDto = sut.importFromDescriptor(descriptorDto); + assertTrue(maybeDto.isPresent()); + + final FullMicroserviceDto dto = maybeDto.get(); + assertEquals(descriptorDto.getDescription(), dto.getDescription()); + assertEquals(descriptorDto.getImageUrl(), dto.getImageUrl()); + assertEquals(descriptorDto.getApiUrl(), dto.getSwaggerUrl()); + assertEquals(descriptorDto.getGitUrl(), dto.getGitUrl()); + assertEquals(descriptorDto.getCiUrl(), dto.getCiUrl()); + assertNotNull(dto.getStatus()); + assertEquals(descriptorDto.getStatus(), dto.getStatus().getName()); + assertNotNull(dto.getTeam()); + + final TeamDto team = dto.getTeam(); + assertEquals(descriptorDto.getTeam(), team.getName()); + } + +} diff --git a/src/test/java/com/github/microcatalog/web/rest/DependencyResourceIT.java b/src/test/java/com/github/microcatalog/web/rest/DependencyResourceIT.java index 53e35e9..5bae0eb 100644 --- a/src/test/java/com/github/microcatalog/web/rest/DependencyResourceIT.java +++ b/src/test/java/com/github/microcatalog/web/rest/DependencyResourceIT.java @@ -61,8 +61,8 @@ public static Dependency createEntity(EntityManager em) { .description(DEFAULT_DESCRIPTION); // Add required entity - Microservice source = MicroserviceResourceIT.createEntity(em); - Microservice target = MicroserviceResourceIT.createEntity(em); + Microservice source = MicroserviceResourceIT.createEntity(em, "FIRST"); + Microservice target = MicroserviceResourceIT.createEntity(em, "SECOND"); em.persist(source); em.persist(target); em.flush(); diff --git a/src/test/java/com/github/microcatalog/web/rest/MicroserviceResourceIT.java b/src/test/java/com/github/microcatalog/web/rest/MicroserviceResourceIT.java index 49bc56c..842c81c 100644 --- a/src/test/java/com/github/microcatalog/web/rest/MicroserviceResourceIT.java +++ b/src/test/java/com/github/microcatalog/web/rest/MicroserviceResourceIT.java @@ -61,15 +61,9 @@ public class MicroserviceResourceIT { private Microservice microservice; - /** - * Create an entity for this test. - * - * This is a static method, as tests for other entities might also need it, - * if they test an entity which requires the current entity. - */ - public static Microservice createEntity(EntityManager em) { + public static Microservice createEntity(EntityManager em, String name){ Microservice microservice = new Microservice() - .name(DEFAULT_NAME) + .name(name) .description(DEFAULT_DESCRIPTION) .imageUrl(DEFAULT_IMAGE_URL) .swaggerUrl(DEFAULT_SWAGGER_URL) @@ -97,6 +91,16 @@ public static Microservice createEntity(EntityManager em) { microservice.setStatus(status); return microservice; } + + /** + * Create an entity for this test. + * + * This is a static method, as tests for other entities might also need it, + * if they test an entity which requires the current entity. + */ + public static Microservice createEntity(EntityManager em) { + return createEntity(em, DEFAULT_NAME); + } /** * Create an updated entity for this test. * @@ -294,7 +298,7 @@ public void getAllMicroservices() throws Exception { .andExpect(jsonPath("$.[*].gitUrl").value(hasItem(DEFAULT_GIT_URL))) .andExpect(jsonPath("$.[*].ciUrl").value(hasItem(DEFAULT_CI_URL))); } - + @Test @Transactional public void getMicroservice() throws Exception { diff --git a/src/test/java/com/github/microcatalog/web/rest/TeamResourceIT.java b/src/test/java/com/github/microcatalog/web/rest/TeamResourceIT.java index 7d37ab5..e59e85f 100644 --- a/src/test/java/com/github/microcatalog/web/rest/TeamResourceIT.java +++ b/src/test/java/com/github/microcatalog/web/rest/TeamResourceIT.java @@ -4,6 +4,7 @@ import com.github.microcatalog.domain.Team; import com.github.microcatalog.repository.TeamRepository; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -13,6 +14,7 @@ import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.web.servlet.MockMvc; import org.springframework.transaction.annotation.Transactional; + import javax.persistence.EntityManager; import java.util.List; @@ -49,9 +51,14 @@ public class TeamResourceIT { private Team team; + @BeforeAll + static void init() { + + } + /** * Create an entity for this test. - * + *

* This is a static method, as tests for other entities might also need it, * if they test an entity which requires the current entity. */ @@ -62,9 +69,10 @@ public static Team createEntity(EntityManager em) { .productOwner(DEFAULT_PRODUCT_OWNER); return team; } + /** * Create an updated entity for this test. - * + *

* This is a static method, as tests for other entities might also need it, * if they test an entity which requires the current entity. */ @@ -192,7 +200,7 @@ public void getAllTeams() throws Exception { .andExpect(jsonPath("$.[*].teamLead").value(hasItem(DEFAULT_TEAM_LEAD))) .andExpect(jsonPath("$.[*].productOwner").value(hasItem(DEFAULT_PRODUCT_OWNER))); } - + @Test @Transactional public void getTeam() throws Exception { @@ -208,6 +216,7 @@ public void getTeam() throws Exception { .andExpect(jsonPath("$.teamLead").value(DEFAULT_TEAM_LEAD)) .andExpect(jsonPath("$.productOwner").value(DEFAULT_PRODUCT_OWNER)); } + @Test @Transactional public void getNonExistingTeam() throws Exception { diff --git a/src/test/resources/config/application.yml b/src/test/resources/config/application.yml index ca5f7ec..e7038cd 100644 --- a/src/test/resources/config/application.yml +++ b/src/test/resources/config/application.yml @@ -15,8 +15,8 @@ spring: profiles: - # Uncomment the following line to enable tests against production database type rather than H2, using Testcontainers - #active: testcontainers + # Uncomment the following line to enable tests against production database type rather than H2, using Testcontainers + #active: testcontainers application: name: microcatalog datasource: @@ -121,3 +121,15 @@ application: token: 1118eceabc3598beaabe090b2a4128f840 disableSSL: false crumbIssuer: /crumbIssuer/api/json + imports: + defaults: + description: Default description because nothing was provided during import + imageUrl: IMPORT_IMAGE + apiUrl: IMPORT_API_URL + gitUrl: IMPORT_GIT_URL + ciUrl: IMPORT_CI_URL + status: IMPORT_STATUS + team: + name: IMPORT_TEAM + po: IMPORT_PO + tl: IMPORT_TL