Skip to content
Open

Xml #15

Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
create category and Customer PUT method
  • Loading branch information
lxian-coder committed Nov 22, 2020
commit 8c3a2bbe8bc7bf1dc302a54f8a5db8ef9523f3df
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

/**
* Darcy Xian 22/11/20 5:05 pm spring5-mvc-rest
*/

@Controller
@RequestMapping("/api/v1/customers/")
@EnableWebMvc
@RequestMapping("/api/v1/customers")
@AllArgsConstructor
public class CustomerController {

Expand All @@ -29,10 +30,42 @@ public ResponseEntity<CustomerListDTO> getAllCustomers (){
new CustomerListDTO(customerService.findAllCustomers()), HttpStatus.OK);
}

@GetMapping("{id}")
@GetMapping("/{id}")
public ResponseEntity<CustomerDTO> getCustomerById(@PathVariable Long id){
return new ResponseEntity<CustomerDTO>(
customerService.findCustomerById(id),HttpStatus.OK
);
CustomerDTO customerDTO = customerService.findCustomerById(id);
customerDTO.setCustomerUrl("/api/v1/cusotmers/"+id);


return new ResponseEntity<CustomerDTO>(
customerDTO ,HttpStatus.OK);
}

@PostMapping
// @RequestBody tell Sping mVC to look at the body of the request and parse it and try to create a
// CustomerDTO out of that
public ResponseEntity<CustomerDTO> createNewCustomer(@RequestBody CustomerDTO customerDTO){
return new ResponseEntity<CustomerDTO>(customerService.createNewCustomer(customerDTO),HttpStatus.CREATED);
}

@PutMapping({"/{id}"})
// @RequestBody tell Sping mVC to look at the body of the request and parse it and try to create a
// CustomerDTO out of that
public ResponseEntity<CustomerDTO> updateCustomer(
@PathVariable Long id,
@RequestBody CustomerDTO customerDTO){
return new ResponseEntity<CustomerDTO>(
customerService.saveCustomerByDTO(id,customerDTO),HttpStatus.CREATED);

}
}











Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public interface CustomerMapper {
@Mapping(source = "id",target = "id")
CustomerDTO CustomerToCustomerDTO(Customer customer);


@Mapping(source = "id",target = "id")
Customer CustomerDTOToCustomer(CustomerDTO customerDTO);

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

import guru.springfamework.api.v1.model.CustomerDTO;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import java.util.List;

/**
* Darcy Xian 22/11/20 5:13 pm spring5-mvc-rest
*/

@Service
public interface CustomerService {

Expand All @@ -17,4 +19,7 @@ public interface CustomerService {

CustomerDTO createNewCustomer(CustomerDTO customerDTO);

CustomerDTO saveCustomerByDTO(Long id, CustomerDTO customerDTO);


}
73 changes: 73 additions & 0 deletions src/main/java/guru/springfamework/services/CustomerServiceImp.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

import guru.springfamework.api.v1.mapper.CustomerMapper;
import guru.springfamework.api.v1.model.CustomerDTO;
import guru.springfamework.domain.Customer;
import guru.springfamework.repositories.CustomerRepository;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import java.util.List;
import java.util.stream.Collectors;

/**
* Darcy Xian 22/11/20 5:24 pm spring5-mvc-rest
*/

@Service
@AllArgsConstructor
public class CustomerServiceImp implements CustomerService{
Expand Down Expand Up @@ -40,4 +43,74 @@ public CustomerDTO findCustomerById(Long id) {
.map(customerMapper::CustomerToCustomerDTO)
.orElseThrow(RuntimeException::new);
}

@Override
public CustomerDTO createNewCustomer(CustomerDTO customerDTO) {

Customer customer = customerMapper.CustomerDTOToCustomer(customerDTO);

Customer savedCustomer = customerRepository.save(customer);

CustomerDTO returnDTO = customerMapper.CustomerToCustomerDTO(savedCustomer);

returnDTO.setCustomerUrl("/api/v1/customer/" + savedCustomer.getId());

return returnDTO;
}

@Override
public CustomerDTO saveCustomerByDTO(Long id, CustomerDTO customerDTO) {
customerDTO.setId(id);

CustomerDTO customerDTO1 = customerMapper.CustomerToCustomerDTO(
customerRepository.save(
customerMapper.CustomerDTOToCustomer(customerDTO)));
customerDTO1.setCustomerUrl("/api/v1/customer/" + id);
return customerDTO1;
}
}













































Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package guru.springfamework.api.controllers.v1;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
* Darcy Xian 22/11/20 9:07 pm spring5-mvc-rest
*/
public abstract class AbstractRestControllerTest {

public static String asJsonString(final Object ob){
try{
return new ObjectMapper().writeValueAsString(ob);
} catch (Exception e){
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,28 @@
import org.mockito.MockitoAnnotations;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import static guru.springfamework.api.controllers.v1.AbstractRestControllerTest.asJsonString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;

import java.util.Arrays;
import java.util.List;


import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

/**
* Darcy Xian 22/11/20 5:51 pm spring5-mvc-rest
*/

public class CustomerControllerTest {

public static final String FIRSTNAME = "JOE";
Expand Down Expand Up @@ -74,9 +80,57 @@ public void getCustomerById() throws Exception {
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(jsonPath("$.firstname", equalTo(FIRSTNAME)));
}
// @Test
// public void createNewCustomer() throws Exception {
// // given
// CustomerDTO customerDTO = new CustomerDTO();
// customerDTO.setFirstname(FIRSTNAME);
// customerDTO.setLastname(LASTNAME);
//
// CustomerDTO returnDTO = new CustomerDTO();
// returnDTO.setFirstname(customerDTO.getFirstname());
// returnDTO.setLastname(customerDTO.getLastname());
// returnDTO.setCustomerUrl("/api/v1/customers/1");
//
// when(customerService.createNewCustomer(customerDTO)).thenReturn(returnDTO);
//
// mockMvc.perform(post("/api/v1/customers/")
// .contentType(MediaType.APPLICATION_JSON)
// // 把一个对象转化成json 利用jackson的jar包
// .contentType(asJsonString(customerDTO)))
// .andExpect(status().isOk())
// .andExpect(jsonPath("$.firstname",equalTo("Fred")));
// // .andExpect(jsonPath("$.customer_url",equalTo("/api/v1/customers/1")));
//
// }

@Test
public void testUpdateCustomer() throws Exception{
CustomerDTO customerDTO = new CustomerDTO();
customerDTO.setFirstname(FIRSTNAME);
customerDTO.setLastname(LASTNAME);

CustomerDTO returnDTO = new CustomerDTO();
returnDTO.setId(1l);
returnDTO.setFirstname(customerDTO.getFirstname());
returnDTO.setLastname(customerDTO.getLastname());
returnDTO.setCustomerUrl("/api/v1/customers/1");

when(customerService.saveCustomerByDTO(anyLong(),any(CustomerDTO.class))).thenReturn(returnDTO);

// when then
mockMvc.perform(put("/api/v1/customers/1")
.contentType(MediaType.APPLICATION_JSON)
.content(asJsonString(customerDTO)))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.firstname",equalTo(FIRSTNAME)));


}



}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.List;

import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

/**
Expand Down Expand Up @@ -59,7 +60,46 @@ public void findCustomerById() {

// then
assertEquals(Long.valueOf(1L),Long.valueOf(customerDTO.getId()));
}

@Test
public void createNewCustomer() throws Exception{
// given
CustomerDTO customerDTO = new CustomerDTO();
customerDTO.setFirstname("lim");

Customer savedCustomer = new Customer();
savedCustomer.setId(1L);
savedCustomer.setFirstname(customerDTO.getFirstname());

when(customerRepository.save(any((Customer.class)))).thenReturn(savedCustomer);

// when
CustomerDTO savedDto = customerService.createNewCustomer(customerDTO);

//then
assertEquals(customerDTO.getFirstname(), savedDto.getFirstname());
assertEquals("/api/v1/customer/1", savedDto.getCustomerUrl());
}

@Test
public void saveCustomerByDTO() throws Exception{
// given
CustomerDTO customerDTO = new CustomerDTO();
customerDTO.setFirstname("lim");

Customer savedCustomer = new Customer();
savedCustomer.setId(1L);
savedCustomer.setFirstname(customerDTO.getFirstname());

when(customerRepository.save(any((Customer.class)))).thenReturn(savedCustomer);

// when
CustomerDTO savedDto = customerService.saveCustomerByDTO(1L,customerDTO);

//then
assertEquals(customerDTO.getFirstname(), savedDto.getFirstname());
assertEquals("/api/v1/customer/1", savedDto.getCustomerUrl());
}
}

Expand Down