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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# SFG Beer Works - Brewery Client
# SFG Beer Works - Brewery Client

Source code in this repository is to support my on line courses:
* [Spring Boot Microservices with Spring Cloud](https://www.udemy.com/spring-boot-microservices-with-spring-cloud-beginner-to-guru/?couponCode=GIT_HUB2)
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.0.M2</version>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>guru.springframework</groupId>
Expand Down Expand Up @@ -70,7 +70,7 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
</plugin>
<!-- require maven 3.60 or higher, require Java 11 or higher-->
<!-- require maven 3.6.0 or higher, require Java 11 or higher-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package guru.springframework.msscbreweryclient.web.client;

import guru.springframework.msscbreweryclient.web.model.BeerDto;
import guru.springframework.msscbreweryclient.web.model.CustomerDto;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.UUID;

/**
* Created by jt on 2019-04-23.
*/
@ConfigurationProperties(prefix = "sfg.brewery", ignoreUnknownFields = false)
@Component
public class BreweryClient {

public final String BEER_PATH_V1 = "/api/v1/beer/";
public final String CUSTOMER_PATH_V1 = "/api/v1/customer/";
private String apihost;

private final RestTemplate restTemplate;

public BreweryClient(RestTemplateBuilder restTemplateBuilder) {
this.restTemplate = restTemplateBuilder.build();
}

public BeerDto getBeerById(UUID uuid){
return restTemplate.getForObject(apihost + BEER_PATH_V1 + uuid.toString(), BeerDto.class);
}

public URI saveNewBeer(BeerDto beerDto){
return restTemplate.postForLocation(apihost + BEER_PATH_V1, beerDto);
}

public void updateBeer(UUID uuid, BeerDto beerDto){
restTemplate.put(apihost + BEER_PATH_V1 + uuid, beerDto);
}

public void deleteBeer(UUID uuid){
restTemplate.delete(apihost + BEER_PATH_V1 + uuid );
}

public void setApihost(String apihost) {
this.apihost = apihost;
}

public CustomerDto getCustomerById(UUID customerId) {
return restTemplate.getForObject(apihost+ CUSTOMER_PATH_V1 + customerId.toString(), CustomerDto.class);
}

public URI saveNewCustomer(CustomerDto customerDto) {
return restTemplate.postForLocation(apihost + CUSTOMER_PATH_V1, customerDto);
}

public void updateCustomer(UUID customerId, CustomerDto customerDto) {
restTemplate.put(apihost + CUSTOMER_PATH_V1 + customerId, customerDto);
}

public void deleteCustomer(UUID customerId) {
restTemplate.delete(apihost + CUSTOMER_PATH_V1 + customerId);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@

sfg.brewery.apihost=http://localhost:8080
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package guru.springframework.msscbreweryclient.web.client;

import guru.springframework.msscbreweryclient.web.model.BeerDto;
import guru.springframework.msscbreweryclient.web.model.CustomerDto;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.net.URI;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertNotNull;

@SpringBootTest
class BreweryClientTest {

@Autowired
BreweryClient client;

@Test
void getBeerById() {
BeerDto dto = client.getBeerById(UUID.randomUUID());

assertNotNull(dto);

}

@Test
void testSaveNewBeer() {
//given
BeerDto beerDto = BeerDto.builder().beerName("New Beer").build();

URI uri = client.saveNewBeer(beerDto);

assertNotNull(uri);

System.out.println(uri.toString());

}

@Test
void testUpdateBeer() {
//given
BeerDto beerDto = BeerDto.builder().beerName("New Beer").build();

client.updateBeer(UUID.randomUUID(), beerDto);

}

@Test
void testDeleteBeer() {
client.deleteBeer(UUID.randomUUID());
}

@Test
void getCustomerById() {
CustomerDto dto = client.getCustomerById(UUID.randomUUID());

assertNotNull(dto);

}

@Test
void testSaveNewCustomer() {
//given
CustomerDto customerDto = CustomerDto.builder().name("Joe").build();

URI uri = client.saveNewCustomer(customerDto);

assertNotNull(uri);

System.out.println(uri.toString());

}

@Test
void testUpdateCustomer() {
//given
CustomerDto customerDto = CustomerDto.builder().name("Jim").build();

client.updateCustomer(UUID.randomUUID(), customerDto);

}

@Test
void testDeleteCustomer() {
client.deleteCustomer(UUID.randomUUID());
}
}