Add modified project modules in spring cloud module

This commit is contained in:
Sasa M
2020-06-09 13:10:48 +02:00
parent 28af7f1e3d
commit c2a7a971a5
23 changed files with 644 additions and 0 deletions
@@ -0,0 +1,14 @@
package com.baeldung.customerservice;
import lombok.AllArgsConstructor;
import lombok.Data;
@Data
@AllArgsConstructor
public class Customer {
private int id;
private String firstName;
private String lastName;
}
@@ -0,0 +1,25 @@
package com.baeldung.customerservice;
import com.baeldung.orderservice.client.OrderClient;
import com.baeldung.orderservice.client.OrderClientImpl;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
/**
* Spring Boot application starter class
*/
@SpringBootApplication
public class CustomerApplication {
public static void main(String[] args) {
SpringApplication.run(CustomerApplication.class, args);
}
@Bean
public OrderClient getOrderClient() {
return new OrderClientImpl(new RestTemplateBuilder());
}
}
@@ -0,0 +1,56 @@
package com.baeldung.customerservice;
import com.baeldung.orderservice.client.OrderClient;
import com.baeldung.orderservice.client.OrderDTO;
import com.baeldung.orderservice.client.OrderResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@RestController
public class CustomerService {
@Autowired
private OrderClient orderClient;
private List<Customer> customers = Arrays.asList(
new Customer(1, "John", "Smith"),
new Customer(2, "Deny", "Dominic"));
@GetMapping
public List<Customer> getAllCustomers() {
return customers;
}
@GetMapping("/{id}")
public Customer getCustomerById(@PathVariable int id) {
return customers.stream()
.filter(customer -> customer.getId() == id)
.findFirst()
.orElseThrow(IllegalArgumentException::new);
}
@PostMapping(value = "/order")
public String sendOrder(@RequestBody Map<String, Object> body) {
OrderDTO dto = new OrderDTO();
dto.setCustomerId((Integer) body.get("customerId"));
dto.setItemId((String) body.get("itemId"));
OrderResponse response = orderClient.order(dto);
return response.getStatus();
}
}
@@ -0,0 +1,4 @@
#Spring Boot server configuration
server.servlet.context-path=/customer-service
server.port=8001
@@ -0,0 +1,33 @@
package com.baeldung.customerservice;
import com.baeldung.orderservice.client.OrderClient;
import com.baeldung.orderservice.client.OrderDTO;
import com.baeldung.orderservice.client.OrderResponse;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = CustomerApplication.class)
public class CustomerServiceUnitTest {
@Autowired
private OrderClient orderClient;
@Test
public void testAddOrderSuccess(){
OrderDTO dto = new OrderDTO(2,"A152");
OrderResponse response = orderClient.order(dto);
Assert.assertNotNull("Order Id not generated", response.getOrderId());
Assert.assertEquals("A152", response.getProductId());
Assert.assertEquals("CREATED", response.getStatus());
}
}
@@ -0,0 +1,2 @@
local.server.port=8001
server.servlet.context-path=/customer-service