Skip to content

Commit

Permalink
Removed old model while retaining old and new endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Felipe444 committed Dec 27, 2018
1 parent 296ba7b commit c872512
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ public ServiceContractsDto create(@PathVariable(name = "provider") String provid
new ServiceContracts(
provider,
version,
map(form.getCapabilities()),
mapToEntity(form.getCapabilities()),
form.getExpectations().entrySet().stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
entry -> map(entry.getValue())
entry -> mapToEntity(entry.getValue())
)
)
)
Expand All @@ -58,8 +58,8 @@ public ServiceContractsDto create(@PathVariable(name = "provider") String provid
@ApiResponse(code = 200, message = "Success", response = ServiceContractsDto.class),
@ApiResponse(code = 400, message = "Bad Request"),
@ApiResponse(code = 500, message = "Failure")})
public NewServiceContractsDto newCreate(@PathVariable(name = "provider") String provider, @PathVariable(name = "version") String version, @RequestBody NewServiceContractsForm form) {
return newToDto(this.serviceContractsRepository.persist(
public ServiceContractsDto newCreate(@PathVariable(name = "provider") String provider, @PathVariable(name = "version") String version, @RequestBody ServiceContractsForm form) {
return toDto(this.serviceContractsRepository.persist(
new ServiceContracts(
provider,
version,
Expand Down Expand Up @@ -113,58 +113,41 @@ public ServiceContractsDto get(@PathVariable(name = "provider") String provider,
@ApiResponse(code = 200, message = "Success", response = ServiceContractsDto.class),
@ApiResponse(code = 400, message = "Bad Request"),
@ApiResponse(code = 500, message = "Failure")})
public NewServiceContractsDto newGet(@PathVariable(name = "provider") String provider, @PathVariable(name = "version") String version) {
return newToDto(this.serviceContractsRepository.find(provider, version).orElseThrow(() -> new ResourceNotFoundException()));
}

private Map<String, ServiceContracts.Contract> map(Map<String, String> protocolToContractStrings) {
return protocolToContractStrings.entrySet().stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
entry -> new ServiceContracts.Contract(entry.getValue(), MediaType.APPLICATION_JSON_VALUE)
));
public ServiceContractsDto newGet(@PathVariable(name = "provider") String provider, @PathVariable(name = "version") String version) {
return toDto(this.serviceContractsRepository.find(provider, version).orElseThrow(() -> new ResourceNotFoundException()));
}

private Map<String, ServiceContracts.Contract> mapToEntity(Map<String, NewServiceContractsForm.ContractForm> protocolToContractForms) {
private Map<String, ServiceContracts.Contract> mapToEntity(Map<String, ServiceContractsForm.ContractForm> protocolToContractForms) {
return protocolToContractForms.entrySet().stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
entry -> new ServiceContracts.Contract(entry.getValue().getValue(), entry.getValue().getMimeType())
));
}

private Map<String, NewServiceContractsDto.ContractDto> mapCapabilitiesToDto(Map<String, ServiceContracts.Contract> capabilities) {
private Map<String, ServiceContractsDto.ContractDto> mapCapabilitiesToDto(Map<String, ServiceContracts.Contract> capabilities) {
return capabilities.entrySet().stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
entry -> new NewServiceContractsDto.ContractDto(entry.getValue().getValue(), entry.getValue().getMimeType())
entry -> new ServiceContractsDto.ContractDto(entry.getValue().getValue(), entry.getValue().getMimeType())
));
}

private Map<String, Map<String, NewServiceContractsDto.ContractDto>> mapExpectationsToDto(Map<ServiceContracts.ProviderProtocol, ServiceContracts.Contract> expectations) {
HashMap<String, Map<String, NewServiceContractsDto.ContractDto>> result = newHashMap();
private Map<String, Map<String, ServiceContractsDto.ContractDto>> mapExpectationsToDto(Map<ServiceContracts.ProviderProtocol, ServiceContracts.Contract> expectations) {
HashMap<String, Map<String, ServiceContractsDto.ContractDto>> result = newHashMap();
for (Map.Entry<ServiceContracts.ProviderProtocol, ServiceContracts.Contract> e : expectations.entrySet()) {
ServiceContracts.ProviderProtocol pp = e.getKey();
ServiceContracts.Contract contract = e.getValue();
if (!result.containsKey(pp.getProvider())) {
result.put(pp.getProvider(), newHashMap());
}
result.get(pp.getProvider()).put(pp.getProtocol(), new NewServiceContractsDto.ContractDto(contract.getValue(), contract.getMimeType()));
result.get(pp.getProvider()).put(pp.getProtocol(), new ServiceContractsDto.ContractDto(contract.getValue(), contract.getMimeType()));
}
return result;
}

private ServiceContractsDto toDto(ServiceContracts serviceContracts) {
return new ServiceContractsDto(
serviceContracts.getName(),
serviceContracts.getVersion(),
serviceContracts.getMappedCapabilities(),
serviceContracts.getMappedExpectations()
);
}

private NewServiceContractsDto newToDto(ServiceContracts serviceContracts) {
return new NewServiceContractsDto(
serviceContracts.getName(),
serviceContracts.getVersion(),
mapCapabilitiesToDto(serviceContracts.getCapabilitiesPerProtocol()),
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.Data;

import java.io.Serializable;
import java.util.Map;

@Data
Expand All @@ -10,7 +11,12 @@ public class ServiceContractsDto {
private final String name;
private final String version;

private final Map<String, String> capabilities;
private final Map<String, Map<String, String>> expectations;
private final Map<String, ContractDto> capabilities;
private final Map<String, Map<String, ContractDto>> expectations;

@Data
public static class ContractDto implements Serializable {
private final String value;
private final String mimeType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,18 @@

import lombok.Data;

import java.io.Serializable;
import java.util.Map;

@Data
public class ServiceContractsForm {

private final Map<String, String> capabilities;
private final Map<String, Map<String, String>> expectations;
private final Map<String, ContractForm> capabilities;
private final Map<String, Map<String, ContractForm>> expectations;

@Data
public static class ContractForm implements Serializable {
private final String value;
private final String mimeType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ class ContractsControllerIT extends Specification {
def response = mockMvc.perform(
post('/new/contracts/' + serviceName + '/' + version)
.contentType("application/json")
.content(objectMapper.writeValueAsString(randomNewServiceContractFormWithExpectationsAndCapabilities()))
.content(objectMapper.writeValueAsString(randomServiceContractFormWithExpectationsAndCapabilities()))
).andReturn().getResponse()
then: 'controller returns dto response in json'
response.getStatus() == 200
response.getContentType().contains("application/json")
objectMapper.readValue(response.getContentAsString(), new TypeReference<NewServiceContractsDto>() {})
objectMapper.readValue(response.getContentAsString(), new TypeReference<ServiceContractsDto>() {})
}

def 'should return 200 and json when get previously saved service contracts'() {
Expand All @@ -82,7 +82,7 @@ class ContractsControllerIT extends Specification {
).andReturn().getResponse()
when:
def response = mockMvc.perform(
get('/contracts/' + serviceName + '/' + version)
get('/new/contracts/' + serviceName + '/' + version)
.contentType("application/json")
).andReturn().getResponse()
then: 'controller returns dto response in json'
Expand All @@ -95,7 +95,7 @@ class ContractsControllerIT extends Specification {
given: 'rest validatePacts url is hit'
def serviceName = randomAlphabetic(10)
def version = '1.0'
def serviceContractsForm = randomNewServiceContractFormWithExpectationsAndCapabilities()
def serviceContractsForm = randomServiceContractFormWithExpectationsAndCapabilities()
mockMvc.perform(
post('/new/contracts/' + serviceName + '/' + version)
.contentType("application/json")
Expand All @@ -109,7 +109,7 @@ class ContractsControllerIT extends Specification {
then: 'controller returns dto response in json'
response.getStatus() == 200
response.getContentType().contains("application/json")
objectMapper.readValue(response.getContentAsString(), new TypeReference<NewServiceContractsDto>() {})
objectMapper.readValue(response.getContentAsString(), new TypeReference<ServiceContractsDto>() {})
}

def 'should successfully retrieve list of services'() {
Expand Down Expand Up @@ -143,18 +143,10 @@ class ContractsControllerIT extends Specification {
objectMapper.readValue(response.getContentAsString(), new TypeReference<List<String>>() {})
}


ServiceContractsForm randomServiceContractFormWithExpectationsAndCapabilities() {
return new ServiceContractsForm(
['protocol': 'capabilities'],
['some-other-provider': ['protocol': 'expectations']]
)
}

NewServiceContractsForm randomNewServiceContractFormWithExpectationsAndCapabilities() {
return new NewServiceContractsForm(
['protocol': new NewServiceContractsForm.ContractForm( 'capabilities', MediaType.APPLICATION_JSON_VALUE)],
['some-other-provider': ['protocol': new NewServiceContractsForm.ContractForm( 'expectations', MediaType.APPLICATION_JSON_VALUE)]]
['protocol': new ServiceContractsForm.ContractForm( 'capabilities', MediaType.APPLICATION_JSON_VALUE)],
['some-other-provider': ['protocol': new ServiceContractsForm.ContractForm( 'expectations', MediaType.APPLICATION_JSON_VALUE)]]
)
}

Expand Down

0 comments on commit c872512

Please sign in to comment.