Skip to content

Commit 09b9272

Browse files
completing PUT mapping
1 parent fd1ecef commit 09b9272

File tree

5 files changed

+72
-4
lines changed

5 files changed

+72
-4
lines changed

src/main/java/guru/springfamework/controllers/v1/CustomerController.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,10 @@ public ResponseEntity<CustomerDTO> createNewCustomer(@RequestBody CustomerDTO cu
4040
return new ResponseEntity<CustomerDTO>(customerService.createNewCustomer(customerDTO),
4141
HttpStatus.CREATED);
4242
}
43+
44+
@PutMapping({"/{id}"})
45+
public ResponseEntity<CustomerDTO> updateCustomer(@PathVariable Long id, @RequestBody CustomerDTO customerDTO){
46+
return new ResponseEntity<CustomerDTO>(customerService.saveCustomerByDTO(id, customerDTO),
47+
HttpStatus.OK);
48+
}
4349
}

src/main/java/guru/springfamework/services/CustomerService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ public interface CustomerService {
1414
CustomerDTO getCustomerById(Long id);
1515

1616
CustomerDTO createNewCustomer(CustomerDTO customerDTO);
17+
18+
CustomerDTO saveCustomerByDTO(Long id, CustomerDTO customerDTO);
1719
}

src/main/java/guru/springfamework/services/CustomerServiceImpl.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,21 @@ public CustomerDTO getCustomerById(Long id) {
4141

4242
return customerRepository.findById(id)
4343
.map(customerMapper::customerToCustomerDTO)
44+
.map(customerDTO -> {
45+
//set API URL
46+
customerDTO.setCustomerUrl("/api/v1/customer/" + id);
47+
return customerDTO;
48+
})
4449
.orElseThrow(RuntimeException::new); //todo implement better exception handling
4550
}
4651

4752
@Override
4853
public CustomerDTO createNewCustomer(CustomerDTO customerDTO) {
4954

50-
Customer customer = customerMapper.customerDtoToCustomer(customerDTO);
55+
return saveAndReturnDTO(customerMapper.customerDtoToCustomer(customerDTO));
56+
}
5157

58+
private CustomerDTO saveAndReturnDTO(Customer customer) {
5259
Customer savedCustomer = customerRepository.save(customer);
5360

5461
CustomerDTO returnDto = customerMapper.customerToCustomerDTO(savedCustomer);
@@ -57,4 +64,12 @@ public CustomerDTO createNewCustomer(CustomerDTO customerDTO) {
5764

5865
return returnDto;
5966
}
67+
68+
@Override
69+
public CustomerDTO saveCustomerByDTO(Long id, CustomerDTO customerDTO) {
70+
Customer customer = customerMapper.customerDtoToCustomer(customerDTO);
71+
customer.setId(id);
72+
73+
return saveAndReturnDTO(customer);
74+
}
6075
}

src/test/java/guru/springfamework/controllers/v1/CustomerControllerTest.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515

1616
import static org.hamcrest.Matchers.equalTo;
1717
import static org.hamcrest.Matchers.hasSize;
18+
import static org.mockito.ArgumentMatchers.any;
1819
import static org.mockito.ArgumentMatchers.anyLong;
1920
import static org.mockito.Mockito.when;
20-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
21-
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
21+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
2222
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
2323
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
2424

@@ -101,4 +101,28 @@ public void createNewCustomer() throws Exception {
101101
.andExpect(jsonPath("$.firstname", equalTo("Fred")))
102102
.andExpect(jsonPath("$.customer_url", equalTo("/api/v1/customers/1")));
103103
}
104+
105+
@Test
106+
public void testUpdateCustomer() throws Exception {
107+
//given
108+
CustomerDTO customer = new CustomerDTO();
109+
customer.setFirstname("Fred");
110+
customer.setLastname("Flintstone");
111+
112+
CustomerDTO returnDTO = new CustomerDTO();
113+
returnDTO.setFirstname(customer.getFirstname());
114+
returnDTO.setLastname(customer.getLastname());
115+
returnDTO.setCustomerUrl("/api/v1/customers/1");
116+
117+
when(customerService.saveCustomerByDTO(anyLong(), any(CustomerDTO.class))).thenReturn(returnDTO);
118+
119+
//when/then
120+
mockMvc.perform(put("/api/v1/customers/1")
121+
.contentType(MediaType.APPLICATION_JSON)
122+
.content(asJsonString(customer)))
123+
.andExpect(status().isOk())
124+
.andExpect(jsonPath("$.firstname", equalTo("Fred")))
125+
.andExpect(jsonPath("$.lastname", equalTo("Flintstone")))
126+
.andExpect(jsonPath("$.customer_url", equalTo("/api/v1/customers/1")));
127+
}
104128
}

src/test/java/guru/springfamework/services/CustomerServiceImplTest.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
public class CustomerServiceImplTest {
2121

22-
2322
@Mock
2423
CustomerRepository customerRepository;
2524

@@ -94,4 +93,26 @@ public void createNewCustomer() throws Exception {
9493
assertEquals(customerDTO.getFirstname(), savedDto.getFirstname());
9594
assertEquals("/api/v1/customer/1", savedDto.getCustomerUrl());
9695
}
96+
97+
@Test
98+
public void saveCustomerByDTO() throws Exception {
99+
100+
//given
101+
CustomerDTO customerDTO = new CustomerDTO();
102+
customerDTO.setFirstname("Jim");
103+
104+
Customer savedCustomer = new Customer();
105+
savedCustomer.setFirstname(customerDTO.getFirstname());
106+
savedCustomer.setLastname(customerDTO.getLastname());
107+
savedCustomer.setId(1l);
108+
109+
when(customerRepository.save(any(Customer.class))).thenReturn(savedCustomer);
110+
111+
//when
112+
CustomerDTO savedDto = customerService.saveCustomerByDTO(1L, customerDTO);
113+
114+
//then
115+
assertEquals(customerDTO.getFirstname(), savedDto.getFirstname());
116+
assertEquals("/api/v1/customer/1", savedDto.getCustomerUrl());
117+
}
97118
}

0 commit comments

Comments
 (0)