Skip to content

Commit

Permalink
Merge 9c268fb into 60fd5a1
Browse files Browse the repository at this point in the history
  • Loading branch information
krzyzy committed Dec 1, 2019
2 parents 60fd5a1 + 9c268fb commit f0240ad
Show file tree
Hide file tree
Showing 18 changed files with 92 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public JudgeD(EnvironmentRepository environmentRepository, ServiceContractsRepos
public <C, E> EnvironmentValidatorResult validateServiceAgainstEnvironments(ServiceContracts validatedService, List<String> environments, InterfaceContractValidator<C, E> validator) {
List<ServiceContracts> environmentContracts = environments.stream()
.flatMap(env -> this.environmentRepository.get(env).getAllServices().stream())
.map(sv -> this.serviceContractsRepository.find(sv.getName(), sv.getVersion()))
.map(sv -> this.serviceContractsRepository.findOne(sv))
.filter(Optional::isPresent)
.map(Optional::get)
.collect(toList());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dev.hltech.dredd.domain;


import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.persistence.Access;
import javax.persistence.AccessType;
import javax.persistence.Embeddable;

import java.io.Serializable;

import static lombok.AccessLevel.PROTECTED;

@Embeddable
@Getter
@EqualsAndHashCode
@AllArgsConstructor
@NoArgsConstructor(access = PROTECTED)
@Access(AccessType.FIELD)
public class ServiceVersion implements Serializable {

private String name;
private String version;

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.hltech.dredd.domain.contracts;

import dev.hltech.dredd.domain.ServiceVersion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

Expand All @@ -25,8 +26,11 @@ public ServiceContracts persist(ServiceContracts serviceContracts) {
}

@Override
public Optional<ServiceContracts> find(String name, String version) {
return ofNullable(entityManager.find(ServiceContracts.class, new ServiceContracts.ServiceContractsId(name, version)));
public Optional<ServiceContracts> findOne(ServiceVersion serviceVersion) {
return ofNullable(entityManager.find(
ServiceContracts.class,
new ServiceVersion(serviceVersion.getName(), serviceVersion.getVersion())
));
}

@Override
Expand All @@ -38,7 +42,7 @@ public String getService(String name) {
}

@Override
public List<ServiceContracts> find(String name) {
public List<ServiceContracts> findAllByServiceName(String name) {
return entityManager
.createQuery("select o from " + ServiceContracts.class.getName() + " o where o.id.name = :name", ServiceContracts.class)
.setParameter("name", name)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.hltech.dredd.domain.contracts;

import dev.hltech.dredd.domain.ServiceVersion;
import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
Expand All @@ -22,7 +23,7 @@
public class ServiceContracts {

@EmbeddedId
private ServiceContractsId id;
private ServiceVersion id;

@ElementCollection(fetch = FetchType.EAGER)
@MapKeyColumn(name = "protocol")
Expand All @@ -43,7 +44,7 @@ protected ServiceContracts() {
}

public ServiceContracts(String name, String version, Map<String, Contract> capabilitiesPerProtocol, Map<String, Map<String, Contract>> expectationsPerProvider) {
this.id = new ServiceContractsId(name, version);
this.id = new ServiceVersion(name, version);
this.capabilitiesPerProtocol = capabilitiesPerProtocol;
this.expectations = newHashMap();
for (Entry<String, Map<String, Contract>> expectationsPerProviderEntry : expectationsPerProvider.entrySet()) {
Expand Down Expand Up @@ -88,16 +89,6 @@ public <E> Map<String, E> getMappedExpectations(String communicationInterface, F
));
}

@Getter
@Embeddable
@AllArgsConstructor
@NoArgsConstructor
@Access(AccessType.FIELD)
static class ServiceContractsId implements Serializable {
private String name;
private String version;
}

@Getter
@Embeddable
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
import java.util.List;
import java.util.Optional;

import dev.hltech.dredd.domain.ServiceVersion;

public interface ServiceContractsRepository {

ServiceContracts persist(ServiceContracts serviceContracts);

Optional<ServiceContracts> find(String name, String version);
Optional<ServiceContracts> findOne(ServiceVersion serviceVersion);

String getService(String name);
List<ServiceContracts> findAllByServiceName(String serviceName);

List<ServiceContracts> find(String name);
String getService(String name);

List<String> getServiceNames();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import lombok.*;
import dev.hltech.dredd.domain.ServiceVersion;

import javax.persistence.*;
import java.util.Set;

import static com.google.common.collect.Sets.newHashSet;
import static java.util.stream.Collectors.toSet;
import static lombok.AccessLevel.PROTECTED;

@Entity
@Table(name = "environments")
Expand Down Expand Up @@ -45,20 +44,6 @@ public static EnvironmentAggregate empty(String environmentName) {
return new EnvironmentAggregate(environmentName, newHashSet());
}

@Embeddable
@Getter
@EqualsAndHashCode
@AllArgsConstructor
@NoArgsConstructor(access = PROTECTED)
@Access(AccessType.FIELD)
@ToString
public static class ServiceVersion {

private String name;
private String version;

}

public static EnvironmentAggregateBuilder builder(String name) {
return new EnvironmentAggregateBuilder(name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.hltech.dredd.domain.contracts.ServiceContracts;
import dev.hltech.dredd.domain.contracts.ServiceContractsRepository;
import dev.hltech.dredd.domain.ServiceVersion;
import dev.hltech.dredd.interfaces.rest.ResourceNotFoundException;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
Expand Down Expand Up @@ -87,7 +88,7 @@ void notFound() { }
@ApiResponse(code = 400, message = "Bad Request"),
@ApiResponse(code = 500, message = "Failure")})
public List<String> getAllServiceVersions(@PathVariable(name = "serviceName") String serviceName) {
return serviceContractsRepository.find(serviceName)
return serviceContractsRepository.findAllByServiceName(serviceName)
.stream()
.map(ServiceContracts::getVersion).sorted()
.collect(toList());
Expand All @@ -100,7 +101,7 @@ public List<String> getAllServiceVersions(@PathVariable(name = "serviceName") St
@ApiResponse(code = 400, message = "Bad Request"),
@ApiResponse(code = 500, message = "Failure")})
public ServiceContractsDto getContracts(@PathVariable(name = "serviceName") String serviceName, @PathVariable(name = "version") String version) {
return mapper.toDto(this.serviceContractsRepository.find(serviceName, version).orElseThrow(ResourceNotFoundException::new));
return mapper.toDto(this.serviceContractsRepository.findOne(new ServiceVersion(serviceName, version)).orElseThrow(ResourceNotFoundException::new));
}

@PostMapping(value = "services/{serviceName}/versions/{version:.+}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
Expand Down Expand Up @@ -128,7 +129,7 @@ public ServiceContractsDto registerContract(@PathVariable(name = "serviceName")
@ApiResponse(code = 400, message = "Bad Request"),
@ApiResponse(code = 500, message = "Failure")})
public List<String> getServiceVersions(@PathVariable(name = "serviceName") String serviceName) {
return serviceContractsRepository.find(serviceName)
return serviceContractsRepository.findAllByServiceName(serviceName)
.stream()
.map(ServiceContracts::getVersion).sorted()
.collect(toList());
Expand All @@ -142,7 +143,7 @@ public List<String> getServiceVersions(@PathVariable(name = "serviceName") Strin
@ApiResponse(code = 400, message = "Bad Request"),
@ApiResponse(code = 500, message = "Failure")})
public ServiceContractsDto get(@PathVariable(name = "provider") String provider, @PathVariable(name = "version") String version) {
return mapper.toDto(this.serviceContractsRepository.find(provider, version).orElseThrow(ResourceNotFoundException::new));
return mapper.toDto(this.serviceContractsRepository.findOne(new ServiceVersion(provider, version)).orElseThrow(ResourceNotFoundException::new));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import dev.hltech.dredd.domain.contracts.ServiceContracts;
import dev.hltech.dredd.domain.contracts.ServiceContractsRepository;
import dev.hltech.dredd.domain.environment.EnvironmentAggregate;
import dev.hltech.dredd.domain.environment.EnvironmentRepository;
import dev.hltech.dredd.domain.ServiceVersion;
import dev.hltech.dredd.interfaces.rest.contracts.ContractsMapper;
import dev.hltech.dredd.interfaces.rest.contracts.ServiceContractsDto;
import io.swagger.annotations.ApiOperation;
Expand Down Expand Up @@ -53,8 +53,8 @@ public InterrelationshipDto getInterrelationship (@PathVariable("environment") S
return new InterrelationshipDto(env, serviceContractsSet);
}

private ServiceContracts getServiceContracts(EnvironmentAggregate.ServiceVersion service) {
return serviceContractsRepository.find(service.getName(), service.getVersion())
private ServiceContracts getServiceContracts(ServiceVersion service) {
return serviceContractsRepository.findOne(service)
.orElseGet(() ->
new ServiceContracts(service.getName(), service.getVersion(), new HashMap<>(), new HashMap<>()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.hltech.dredd.interfaces.rest.validation;

import com.google.common.collect.Ordering;
import dev.hltech.dredd.domain.ServiceVersion;
import dev.hltech.dredd.domain.validation.EnvironmentValidatorResult;

import java.util.Comparator;
Expand All @@ -15,15 +16,15 @@ public class Converters {
private Converters() {
}

public static List<ContractValidationReportDto> toDtos(List<EnvironmentValidatorResult> validationResults, String serviceName, String serviceVersion) {
public static List<ContractValidationReportDto> toDtos(List<EnvironmentValidatorResult> validationResults, ServiceVersion serviceVersion) {
Map<ConsumerAndProviderDto, ContractValidationReportDto> result = newHashMap();
validationResults
.stream()
.forEach(environmentValidatorResult -> {
environmentValidatorResult.getCapabilitiesValidationResults()
.stream()
.forEach(cvr -> {
ConsumerAndProviderDto key = new ConsumerAndProviderDto(cvr.getConsumerName(), cvr.getConsumerVersion(), serviceName, serviceVersion);
ConsumerAndProviderDto key = new ConsumerAndProviderDto(cvr.getConsumerName(), cvr.getConsumerVersion(), serviceVersion.getName(), serviceVersion.getVersion());
if (!result.containsKey(key)) {
result.put(
key,
Expand All @@ -45,7 +46,7 @@ public static List<ContractValidationReportDto> toDtos(List<EnvironmentValidator
environmentValidatorResult.getExpectationValidationResults()
.stream()
.forEach(evr -> {
ConsumerAndProviderDto key = new ConsumerAndProviderDto(serviceName, serviceVersion, evr.getProviderName(), evr.getProviderVersion());
ConsumerAndProviderDto key = new ConsumerAndProviderDto(serviceVersion.getName(), serviceVersion.getVersion(), evr.getProviderName(), evr.getProviderVersion());
if (!result.containsKey(key)) {
result.put(
key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import dev.hltech.dredd.domain.JudgeD;
import dev.hltech.dredd.domain.contracts.ServiceContracts;
import dev.hltech.dredd.domain.contracts.ServiceContractsRepository;
import dev.hltech.dredd.domain.ServiceVersion;
import dev.hltech.dredd.domain.validation.EnvironmentValidatorResult;
import dev.hltech.dredd.domain.validation.InterfaceContractValidator;
import dev.hltech.dredd.interfaces.rest.ResourceNotFoundException;
Expand Down Expand Up @@ -47,11 +48,12 @@ public ValidationController(
@ApiResponse(code = 404, message = "Service not found")
})
public List<ContractValidationReportDto> validateAgainstEnvironments(
@PathVariable("serviceName") String serviceName,
@PathVariable("serviceVersion") String serviceVersion,
@PathVariable("serviceName") String name,
@PathVariable("serviceVersion") String version,
@RequestParam("environment") List<String> environments
) {
ServiceContracts validatedServiceContracts = this.serviceContractsRepository.find(serviceName, serviceVersion)
ServiceVersion serviceVersion = new ServiceVersion(name, version);
ServiceContracts validatedServiceContracts = this.serviceContractsRepository.findOne(serviceVersion)
.orElseThrow(ResourceNotFoundException::new);

List<EnvironmentValidatorResult> collect = this.validators.stream()
Expand All @@ -62,6 +64,6 @@ public List<ContractValidationReportDto> validateAgainstEnvironments(
validator
))
.collect(Collectors.toList());
return toDtos(collect, serviceName, serviceVersion);
return toDtos(collect, serviceVersion);
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,23 @@
package dev.hltech.dredd.domain.contracts

import com.google.common.collect.Maps
import dev.hltech.dredd.domain.environment.EnvironmentAggregate
import dev.hltech.dredd.domain.ServiceVersion

import javax.persistence.EntityNotFoundException
import javax.persistence.NoResultException

class InMemoryServiceContractsRepository implements ServiceContractsRepository {

private Map<EnvironmentAggregate.ServiceVersion, ServiceContracts> storage = Maps.newHashMap()
private Map<ServiceVersion, ServiceContracts> storage = Maps.newHashMap()

@Override
ServiceContracts persist(ServiceContracts service) {
storage.put(new EnvironmentAggregate.ServiceVersion(service.name, service.version), service)
storage.put(new ServiceVersion(service.name, service.version), service)
return service;
}

@Override
Optional<ServiceContracts> find(String name, String version) {
return Optional.ofNullable(storage.get(new EnvironmentAggregate.ServiceVersion(name, version)))
Optional<ServiceContracts> findOne(ServiceVersion serviceVersion) {
return Optional.ofNullable(storage.get(serviceVersion))
}

@Override
Expand All @@ -35,7 +34,7 @@ class InMemoryServiceContractsRepository implements ServiceContractsRepository {
}

@Override
List<ServiceContracts> find(String name) {
List<ServiceContracts> findAllByServiceName(String name) {
return storage.entrySet()
.stream()
.filter { it -> it.getKey().name == name }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.hltech.dredd.domain.contracts

import dev.hltech.dredd.domain.ServiceVersion
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.MediaType
Expand Down Expand Up @@ -31,7 +32,7 @@ class JpaServiceContractsRepositoryIT extends Specification {
)
repository.persist(serviceContracts)
when:
def retrieved = repository.find(serviceContracts.name, serviceContracts.version)
def retrieved = repository.findOne(new ServiceVersion(serviceContracts.name, serviceContracts.version))
then:
retrieved.get().id.name == 'provider'
retrieved.get().id.version == '1.0'
Expand Down Expand Up @@ -59,7 +60,7 @@ class JpaServiceContractsRepositoryIT extends Specification {
def s1 = repository.persist(new ServiceContracts(serviceName, randomAlphabetic(5), [:], [:]))
def s2 = repository.persist(new ServiceContracts(serviceName, randomAlphabetic(5), [:], [:]))
when:
def serviceContracts = repository.find(serviceName)
def serviceContracts = repository.findAllByServiceName(serviceName)
then:
serviceContracts.size() == 2
serviceContracts.contains(s1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package dev.hltech.dredd.domain.environment

import dev.hltech.dredd.domain.ServiceVersion
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.test.context.ActiveProfiles
import spock.lang.Specification

import static dev.hltech.dredd.domain.environment.EnvironmentAggregate.ServiceVersion
import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT

Expand Down
Loading

0 comments on commit f0240ad

Please sign in to comment.