Skip to content

Commit

Permalink
API - Step 3 - CONTRACT - Server removes backwards compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
chrissimon-au committed May 22, 2023
1 parent e654f4d commit ba4d4f1
Show file tree
Hide file tree
Showing 6 changed files with 8 additions and 99 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package au.chrissimon.safecontractchangesdemo;

public class AddNameRequest {
private String name;
private FullNameDto fullName;

public FullNameDto getFullName() {
Expand All @@ -10,25 +9,9 @@ public FullNameDto getFullName() {

public void setFullName(FullNameDto fullName) {
this.fullName = fullName;
if (fullName != null) {
this.name = fullName.getFirstName() + " " + fullName.getLastName();
}
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public AddNameRequest() {
super();
}

public AddNameRequest(String name) {
super();
this.name = name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ public Name() {
super();
}

public Name(String name) {
public Name(FullNameDto fullName) {
super();
this.id = UUID.randomUUID();
this.name = name;
this.name = (fullName.getFirstName() + " " + fullName.getLastName()).trim();
}

public NameResponse toResponse() {
return new NameResponse(id, name);
String[] names = name.split("\\s", 2);
String lastName = names.length > 1 ? names[1] : "";
return new NameResponse(id, new FullNameDto(names[0], lastName));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.UUID;

public class NameResponse {
private String name;
private FullNameDto fullName;
public FullNameDto getFullName() {
return fullName;
Expand All @@ -23,24 +22,13 @@ public void setId(UUID id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public NameResponse() {
super();
}

public NameResponse(UUID id, String name) {
public NameResponse(UUID id, FullNameDto fullName) {
super();
this.id = id;
this.name = name;
String[] names = name.split("\\s", 2);
String lastName = names.length > 1 ? names[1] : "";
this.fullName = new FullNameDto(names[0], lastName);
this.fullName = fullName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ public NamesController(NameRepository nameRepository) {

@PostMapping("/names")
public ResponseEntity<NameResponse> addName(@RequestBody AddNameRequest request) {

Name name = new Name(request.getName());
Name name = new Name(request.getFullName());

nameRepository.save(name);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,55 +18,6 @@ public class NameTests {

@Value(value="${local.server.port}")
private int port;

@Test
public void AddPerson() {
WebTestClient client = WebTestClient
.bindToServer()
.baseUrl("http://localhost:" + port)
.build();

String name = UUID.randomUUID().toString();

TestAddNameRequest nameRequest = new TestAddNameRequest(name);

ResponseSpec response = client
.post()
.uri("/names")
.bodyValue(nameRequest)
.exchange();

EntityExchangeResult<TestNameResponse> result = response
.expectBody(TestNameResponse.class)
.returnResult();

TestNameResponse nameResponse = result
.getResponseBody();

URI newNameLocation = result
.getResponseHeaders().getLocation();

response.expectStatus().isCreated();
assertThat(nameResponse.getName()).isEqualTo(name);
assertThat(nameResponse.getId()).isNotNull();
assertThat(nameResponse.getId()).isNotEqualTo(new UUID(0, 0));
assertThat(newNameLocation.toString()).isEqualTo("http://localhost:" + port + "/names/" + nameResponse.getId());

ResponseSpec checkResponse = client
.get()
.uri(newNameLocation)
.exchange();

checkResponse.expectStatus().isOk();

TestNameResponse checkNameResponse = checkResponse
.expectBody(TestNameResponse.class)
.returnResult()
.getResponseBody();

assertThat(checkNameResponse.getId()).isEqualTo(nameResponse.getId());
assertThat(checkNameResponse.getName()).isEqualTo(name);
}

@Test
public void AddPersonWithFullName() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package au.chrissimon.safecontractchangesdemo;

public class TestAddNameRequest {
private String name;
private TestFullNameDto fullName;

public TestFullNameDto getFullName() {
Expand All @@ -12,23 +11,10 @@ public void setFullName(TestFullNameDto fullName) {
this.fullName = fullName;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public TestAddNameRequest() {
super();
}

public TestAddNameRequest(String name) {
super();
this.name = name;
}

public TestAddNameRequest(TestFullNameDto fullName) {
super();
this.fullName = fullName;
Expand Down

0 comments on commit ba4d4f1

Please sign in to comment.