Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 20 additions & 4 deletions template/pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.springframework</groupId>
Expand All @@ -13,6 +13,10 @@
<version>1.5.9.RELEASE</version>
</parent>

<properties>
<java.version>1.9</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand All @@ -36,11 +40,23 @@
<scope>test</scope>
</dependency>
<!-- end::tests[] -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1206-jdbc42</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>

<properties>
<java.version>1.9</java.version>
</properties>


<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<List<Customer>> getCustomers() {
List<Customer> customers = customerService.getCustomers();
return new ResponseEntity<>(customers,HttpStatus.OK);
}

@RequestMapping(method = RequestMethod.GET, path = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Customer> getCustomerById(@PathVariable Long id) {
ResponseEntity<Customer> 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<Customer> 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<Customer> updateCustomer(@RequestBody Customer customer,@PathVariable Long id) {
ResponseEntity<Customer> 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);
}

}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<Customer,Long> {
}
Original file line number Diff line number Diff line change
@@ -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<Customer> getCustomers();

Customer getCustomer(Long id);

Customer updateCustomer(Customer customer);

void deleteCustomer(Long id);
}
Original file line number Diff line number Diff line change
@@ -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<Customer> getCustomers() {
List<Customer> 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);
}
}
7 changes: 6 additions & 1 deletion template/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -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
spring.data.mongodb.port=27017
Original file line number Diff line number Diff line change
@@ -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<Customer> response = restTemplate.postForEntity(base.toString() + "api/customers", customer, Customer.class);
assertEquals(response.getStatusCode(), HttpStatus.OK);
}
@Test
public void testFindCustomer() {
ResponseEntity<Customer> response = restTemplate.getForEntity(base.toString() + "api/customers/123123", Customer.class);
assertEquals(123L, response.getBody().getId().longValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down