diff --git a/template/pom.xml b/template/pom.xml index 167a453..365c55c 100644 --- a/template/pom.xml +++ b/template/pom.xml @@ -1,6 +1,6 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.springframework @@ -13,6 +13,10 @@ 1.5.9.RELEASE + + 1.9 + + org.springframework.boot @@ -36,11 +40,23 @@ test + + org.postgresql + postgresql + 9.4-1206-jdbc42 + + + org.springframework.boot + spring-boot-starter-data-jpa + + + javax.xml.bind + jaxb-api + 2.3.0 + - - 1.9 - + diff --git a/template/src/main/java/com/simtlix/techgroups/template/controllers/CustomerController.java b/template/src/main/java/com/simtlix/techgroups/template/controllers/CustomerController.java new file mode 100644 index 0000000..fd089b2 --- /dev/null +++ b/template/src/main/java/com/simtlix/techgroups/template/controllers/CustomerController.java @@ -0,0 +1,75 @@ +package com.simtlix.techgroups.template.controllers; + +import com.simtlix.techgroups.template.model.Customer; +import com.simtlix.techgroups.template.services.CustomerService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import java.util.List; + +/** + * Created by Facundo on 1/29/2018. + */ +@RestController +@RequestMapping("/api/customers") +public class CustomerController { + + private CustomerService customerService; + + @Autowired + public CustomerController(CustomerService customerService){ + this.customerService = customerService; + } + + @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity> getCustomers() { + List customers = customerService.getCustomers(); + return new ResponseEntity<>(customers,HttpStatus.OK); + } + + @RequestMapping(method = RequestMethod.GET, path = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity getCustomerById(@PathVariable Long id) { + ResponseEntity responseEntity; + Customer customer = customerService.getCustomer(id); + if (customer == null) { + responseEntity = new ResponseEntity<>(HttpStatus.NO_CONTENT); + } else{ + responseEntity = new ResponseEntity<>(customer,HttpStatus.OK); + } + return responseEntity; + } + + @RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, + consumes = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity addCustomer(@RequestBody Customer customer) { + Customer customerSaved = customerService.addCustomer(customer); + return new ResponseEntity<>(customerSaved,HttpStatus.CREATED); + } + + @RequestMapping(method = RequestMethod.PUT, path = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE, + consumes = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity updateCustomer(@RequestBody Customer customer,@PathVariable Long id) { + ResponseEntity responseEntity; + Customer customerStored = customerService.getCustomer(id); + if (customerStored == null) { + responseEntity = new ResponseEntity<>(HttpStatus.NOT_FOUND); + } else{ + customer.setId(id); + responseEntity = new ResponseEntity<>(customerService.updateCustomer(customer),HttpStatus.OK); + } + return responseEntity; + } + + @RequestMapping(method = RequestMethod.DELETE, path = "/{id}") + public ResponseEntity deleteCustomer(@PathVariable Long id) { + customerService.deleteCustomer(id); + return new ResponseEntity(HttpStatus.OK); + } + +} diff --git a/template/src/main/java/com/simtlix/techgroups/template/model/Customer.java b/template/src/main/java/com/simtlix/techgroups/template/model/Customer.java new file mode 100644 index 0000000..590cc76 --- /dev/null +++ b/template/src/main/java/com/simtlix/techgroups/template/model/Customer.java @@ -0,0 +1,68 @@ +package com.simtlix.techgroups.template.model; + +import javax.persistence.*; +import java.io.Serializable; + +/** + * Created by Facundo on 1/29/2018. + */ +@Entity +@Table(name = "customer") +public class Customer implements Serializable { + + + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name = "id", nullable = false) + @Id + private Long id; + + @Column(name = "name") + private String name; + + @Column(name = "last_name") + private String lastName; + + @Column(name = "email", unique = true, nullable = false) + private String email; + + public Customer() { + } + + public Customer(String name, String lastName, String email) { + this.name = name; + this.lastName = lastName; + this.email = email; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } +} diff --git a/template/src/main/java/com/simtlix/techgroups/template/repositories/CustomerRepository.java b/template/src/main/java/com/simtlix/techgroups/template/repositories/CustomerRepository.java new file mode 100644 index 0000000..905e842 --- /dev/null +++ b/template/src/main/java/com/simtlix/techgroups/template/repositories/CustomerRepository.java @@ -0,0 +1,11 @@ +package com.simtlix.techgroups.template.repositories; + +import com.simtlix.techgroups.template.model.Customer; +import org.springframework.data.repository.PagingAndSortingRepository; + +/** + * Created by Facundo on 1/29/2018. + */ + +public interface CustomerRepository extends PagingAndSortingRepository { +} diff --git a/template/src/main/java/com/simtlix/techgroups/template/services/CustomerService.java b/template/src/main/java/com/simtlix/techgroups/template/services/CustomerService.java new file mode 100644 index 0000000..94fdd1d --- /dev/null +++ b/template/src/main/java/com/simtlix/techgroups/template/services/CustomerService.java @@ -0,0 +1,21 @@ +package com.simtlix.techgroups.template.services; + +import com.simtlix.techgroups.template.model.Customer; + +import java.util.List; + +/** + * Created by Facundo on 1/29/2018. + */ +public interface CustomerService { + + Customer addCustomer(Customer customer); + + List getCustomers(); + + Customer getCustomer(Long id); + + Customer updateCustomer(Customer customer); + + void deleteCustomer(Long id); +} diff --git a/template/src/main/java/com/simtlix/techgroups/template/services/CustomerServiceImpl.java b/template/src/main/java/com/simtlix/techgroups/template/services/CustomerServiceImpl.java new file mode 100644 index 0000000..a59cffe --- /dev/null +++ b/template/src/main/java/com/simtlix/techgroups/template/services/CustomerServiceImpl.java @@ -0,0 +1,50 @@ +package com.simtlix.techgroups.template.services; + +import com.simtlix.techgroups.template.model.Customer; +import com.simtlix.techgroups.template.repositories.CustomerRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Facundo on 1/29/2018. + */ +@Service +public class CustomerServiceImpl implements CustomerService { + + private final CustomerRepository customerRepository; + + @Autowired + public CustomerServiceImpl(CustomerRepository customerRepository) { + this.customerRepository = customerRepository; + } + + @Override + public List getCustomers() { + List customers = new ArrayList<>(); + customerRepository.findAll().forEach(customers::add); + return customers; + } + + @Override + public Customer getCustomer(Long id) { + return customerRepository.findOne(id); + } + + @Override + public Customer addCustomer(Customer customer) { + return customerRepository.save(customer); + } + + @Override + public Customer updateCustomer(Customer customer) { + return customerRepository.save(customer); + } + + @Override + public void deleteCustomer(Long id) { + customerRepository.delete(id); + } +} diff --git a/template/src/main/resources/application.properties b/template/src/main/resources/application.properties index dabfde7..578c6a5 100644 --- a/template/src/main/resources/application.properties +++ b/template/src/main/resources/application.properties @@ -1,2 +1,7 @@ +spring.datasource.url=jdbc:postgresql://db:5432/template +spring.datasource.driver-class-name=org.postgresql.Driver +spring.datasource.username=postgres +spring.datasource.password=Simtlix01 +spring.jpa.hibernate.ddl-auto=update spring.data.mongodb.host=mongo -spring.data.mongodb.port=27017 \ No newline at end of file +spring.data.mongodb.port=27017 diff --git a/template/src/test/java/com/simtlix/techgroups/template/controllers/CostumerControllerTest.java b/template/src/test/java/com/simtlix/techgroups/template/controllers/CostumerControllerTest.java new file mode 100644 index 0000000..6cc4db1 --- /dev/null +++ b/template/src/test/java/com/simtlix/techgroups/template/controllers/CostumerControllerTest.java @@ -0,0 +1,70 @@ +package com.simtlix.techgroups.template.controllers; + +import com.simtlix.techgroups.template.model.Customer; +import com.simtlix.techgroups.template.repositories.CustomerRepository; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.embedded.LocalServerPort; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.test.context.junit4.SpringRunner; + +import java.net.URL; + +import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.when; + +/** + * Created by Facundo on 2/20/2018. + */ +@RunWith(SpringRunner.class) +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +public class CostumerControllerTest { + + @LocalServerPort + private int port; + + private URL base; + + @Autowired + private TestRestTemplate restTemplate; + + @MockBean + private CustomerRepository customerRepository; + + + @Before + public void setUp() throws Exception { + this.base = new URL("http://localhost:" + port + "/"); + Customer customer = new Customer(); + customer.setId(1l); + customer.setEmail("test"); + customer.setName("test"); + customer.setLastName("test"); + + when(customerRepository.save(any(Customer.class))).thenReturn(customer); + when(customerRepository.findOne(any())).thenReturn(customer); + + } + + @Test + public void testSaveOk() { + Customer customer = new Customer(); + customer.setEmail("test"); + customer.setName("test"); + customer.setLastName("test"); + ResponseEntity response = restTemplate.postForEntity(base.toString() + "api/customers", customer, Customer.class); + assertEquals(response.getStatusCode(), HttpStatus.OK); + } + @Test + public void testFindCustomer() { + ResponseEntity response = restTemplate.getForEntity(base.toString() + "api/customers/123123", Customer.class); + assertEquals(123L, response.getBody().getId().longValue()); + } +} diff --git a/template/src/test/java/com/simtlix/techgroups/template/controllers/UserControllerTest.java b/template/src/test/java/com/simtlix/techgroups/template/controllers/UserControllerTest.java index 1b7be36..89eba5a 100644 --- a/template/src/test/java/com/simtlix/techgroups/template/controllers/UserControllerTest.java +++ b/template/src/test/java/com/simtlix/techgroups/template/controllers/UserControllerTest.java @@ -7,18 +7,16 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.embedded.LocalServerPort; -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.context.junit4.SpringRunner; + import java.net.URL; -import java.util.List; -import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; import static org.mockito.Matchers.any; import static org.mockito.Mockito.when;