Update the resource with HTTP Put
- do not crash while trying to update non exisiting resource
This commit is contained in:
parent
d3751dda0f
commit
4287b96320
@ -40,6 +40,6 @@ test {
|
|||||||
showStackTraces true
|
showStackTraces true
|
||||||
|
|
||||||
// Change to `true` for more verbose test output
|
// Change to `true` for more verbose test output
|
||||||
showStandardStreams = false
|
showStandardStreams = true
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -7,6 +7,7 @@ import org.springframework.data.domain.Sort;
|
|||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.PutMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
import org.springframework.web.bind.annotation.PathVariable;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
@ -74,4 +75,17 @@ public class CashCardController {
|
|||||||
|
|
||||||
return ResponseEntity.created(locationOfNewCashCard).build();
|
return ResponseEntity.created(locationOfNewCashCard).build();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
@PutMapping("/{requestedId}")
|
||||||
|
private ResponseEntity<Void> putCashCard(@PathVariable Long requestedId, @RequestBody CashCard cashCardUpdate, Principal principal) {
|
||||||
|
CashCard cashCard = cashCardRepository.findByIdAndOwner(requestedId, principal.getName());
|
||||||
|
|
||||||
|
if (cashCard == null) {
|
||||||
|
return ResponseEntity.notFound().build();
|
||||||
|
}
|
||||||
|
|
||||||
|
CashCard updatedCashCard = new CashCard(cashCard.id(), cashCardUpdate.amount(), principal.getName());
|
||||||
|
cashCardRepository.save(updatedCashCard);
|
||||||
|
return ResponseEntity.noContent().build();
|
||||||
|
}
|
||||||
|
}
|
@ -5,6 +5,8 @@ import org.junit.jupiter.api.Test;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
import org.springframework.boot.test.web.client.TestRestTemplate;
|
import org.springframework.boot.test.web.client.TestRestTemplate;
|
||||||
|
import org.springframework.http.HttpEntity;
|
||||||
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.test.annotation.DirtiesContext;
|
import org.springframework.test.annotation.DirtiesContext;
|
||||||
@ -169,4 +171,37 @@ class CashcardApplicationTests {
|
|||||||
.getForEntity("/cashcards/102", String.class); // <<-- kumar2's data
|
.getForEntity("/cashcards/102", String.class); // <<-- kumar2's data
|
||||||
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
|
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DirtiesContext
|
||||||
|
void shouldUpdateAnExistingCashCard() {
|
||||||
|
CashCard cashCardUpdate = new CashCard(null, 19.99, null);
|
||||||
|
HttpEntity<CashCard> request = new HttpEntity<>(cashCardUpdate);
|
||||||
|
ResponseEntity<Void> response = restTemplate
|
||||||
|
.withBasicAuth("sarah1", "abc123")
|
||||||
|
.exchange("/cashcards/99", HttpMethod.PUT, request, Void.class);
|
||||||
|
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NO_CONTENT);
|
||||||
|
|
||||||
|
// Validte that the resource was updated
|
||||||
|
ResponseEntity<String> getResponse = restTemplate
|
||||||
|
.withBasicAuth("sarah1", "abc123")
|
||||||
|
.getForEntity("/cashcards/99", String.class);
|
||||||
|
assertThat(getResponse.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||||
|
|
||||||
|
DocumentContext documentContext = JsonPath.parse(getResponse.getBody());
|
||||||
|
Number id = documentContext.read("$.id");
|
||||||
|
Double amount = documentContext.read("$.amount");
|
||||||
|
assertThat(id).isEqualTo(99);
|
||||||
|
assertThat(amount).isEqualTo(19.99);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldNotUpdateACashCardThatDoesNotExist() {
|
||||||
|
CashCard unknownCard = new CashCard(null, 19.99, null);
|
||||||
|
HttpEntity<CashCard> request = new HttpEntity<>(unknownCard);
|
||||||
|
ResponseEntity<Void> response = restTemplate
|
||||||
|
.withBasicAuth("sarah1", "abc123")
|
||||||
|
.exchange("/cashcards/99999", HttpMethod.PUT, request, Void.class);
|
||||||
|
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user