Commit f7a1a2ad authored by Ashok Kumar K's avatar Ashok Kumar K

changed urls in test and added a new test

parent a1954127
......@@ -4,7 +4,6 @@ import com.nisum.ecomcustomer.model.Customer;
import com.nisum.ecomcustomer.model.CustomerType;
import com.nisum.ecomcustomer.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
......@@ -18,7 +17,7 @@ import javax.validation.constraints.Pattern;
import java.time.LocalDate;
import java.util.List;
@RefreshScope
@RestController
@Validated
public class CustomerController {
......
spring:
data:
mongodb:
host: localhost
port: 27017
database: ecom-test
cloud:
config:
discovery:
enabled: false
# config:
# enabled: true
......@@ -13,6 +13,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
......@@ -24,8 +25,7 @@ import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.TestInstance.Lifecycle;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
......@@ -52,7 +52,7 @@ public class CustomerControllerIntegrationTest {
public void setUp() {
initializeDb();
url = "http://localhost:" + port + "/customer";
url = "http://localhost:" + port;
}
@Test
......@@ -167,6 +167,31 @@ public class CustomerControllerIntegrationTest {
assertEquals(HttpStatus.NOT_FOUND, customerResponseEntity.getStatusCode());
}
@Test
public void getCustomerByCustomerType_returnsCustomers_200() {
int loyalValue = CustomerType.LOYAL.getValue();
ResponseEntity<List<Customer>> customersListResponseEntity = testRestTemplate.exchange("/type/" + loyalValue, HttpMethod.GET, null, new ParameterizedTypeReference<List<Customer>>() {
});
assertEquals(HttpStatus.OK, customersListResponseEntity.getStatusCode());
List<Customer> loyalCustomers = customersListResponseEntity.getBody();
assertNotNull(loyalCustomers);
assertNotEquals(0, loyalCustomers.size());
loyalCustomers.forEach(customer -> assertEquals(loyalValue, customer.getCustomerType().getValue()));
}
@Test
public void getRegisteredCustomerOnDate_200() {
LocalDate today = LocalDate.now();
ResponseEntity<List<Customer>> customersRegisteredResponseEntity = testRestTemplate.exchange("/registered/on?onDate={onDate}", HttpMethod.GET, null, new ParameterizedTypeReference<List<Customer>>() {
}, today);
List<Customer> customersRegistered = customersRegisteredResponseEntity.getBody();
assertNotNull(customersRegistered);
assertNotEquals(0, customersRegistered.size());
customersRegistered.forEach(customer -> assertEquals(today, customer.getCreatedDate()));
}
private void initializeDb() {
customerRepository.deleteAll();
......
......@@ -48,7 +48,7 @@ class CustomerControllerTest {
Customer customerRequestObj = getCustomerRequestObject();
Customer customerResponseObj = getCustomerResponseObject();
when(customerService.register(customerRequestObj)).thenReturn(customerResponseObj);
MvcResult result = mockMvc.perform(post("/customer/register")
MvcResult result = mockMvc.perform(post("/register")
.content(objectMapper.writeValueAsString(customerRequestObj))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isCreated())
......@@ -64,7 +64,7 @@ class CustomerControllerTest {
void registerCustomer_throwsEmailAlreadyRegisteredException_409() throws Exception {
Customer customerRequestObject = getCustomerRequestObject();
when(customerService.register(customerRequestObject)).thenThrow(new EmailAlreadyRegisteredException());
mockMvc.perform(post("/customer/register")
mockMvc.perform(post("/register")
.content(objectMapper.writeValueAsString(customerRequestObject))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isConflict())
......@@ -76,7 +76,7 @@ class CustomerControllerTest {
List<Customer> customersRequestObj = getCustomerRequestObjectsList();
List<Customer> customersResponseObj = getCustomerResponseObjectsList();
when(customerService.registerAll(customersRequestObj)).thenReturn(customersResponseObj);
mockMvc.perform(post("/customer/registerAll")
mockMvc.perform(post("/registerAll")
.content(objectMapper.writeValueAsString(customersRequestObj))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isCreated())
......@@ -88,7 +88,7 @@ class CustomerControllerTest {
void registerCustomers_throwsEmailAlreadyRegisteredException_409() throws Exception {
List<Customer> customersRequestObj = getCustomerRequestObjectsList();
when(customerService.registerAll(customersRequestObj)).thenThrow(new EmailAlreadyRegisteredException());
mockMvc.perform(post("/customer/registerAll")
mockMvc.perform(post("/registerAll")
.content(objectMapper.writeValueAsString(customersRequestObj))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isConflict())
......@@ -104,7 +104,7 @@ class CustomerControllerTest {
customerRequestObj.setModifiedDate(LocalDateTime.now());
Customer customerResponseObj = getCustomerResponseObject();
when(customerService.update(customerRequestObj)).thenReturn(customerResponseObj);
MvcResult result = mockMvc.perform(put("/customer/update")
MvcResult result = mockMvc.perform(put("/update")
.content(objectMapper.writeValueAsString(customerRequestObj))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isCreated())
......@@ -122,7 +122,7 @@ class CustomerControllerTest {
Customer customerRequestObj = getCustomerRequestObject();
customerRequestObj.setId(null);
when(customerService.update(customerRequestObj)).thenThrow(new CustomerNotFoundException());
mockMvc.perform(put("/customer/update")
mockMvc.perform(put("/update")
.content(objectMapper.writeValueAsString(customerRequestObj))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isNotFound())
......@@ -132,7 +132,7 @@ class CustomerControllerTest {
@Test
void getAllCustomers_returnsCustomers_200() throws Exception {
when(customerService.getAllCustomers()).thenReturn(getCustomerResponseObjectsList());
mockMvc.perform(get("/customer/"))
mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(3)))
.andReturn();
......@@ -141,7 +141,7 @@ class CustomerControllerTest {
@Test
void getCustomerById_returnsCustomer_200() throws Exception {
when(customerService.getCustomerById(1L)).thenReturn(getCustomerResponseObject());
mockMvc.perform(get("/customer/id/" + 1))
mockMvc.perform(get("/id/" + 1))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id", is(1)))
.andReturn();
......@@ -150,7 +150,7 @@ class CustomerControllerTest {
@Test
void getCustomerById_throwsCustomerNotFoundException_404() throws Exception {
when(customerService.getCustomerById(1L)).thenThrow(new CustomerNotFoundException());
mockMvc.perform(get("/customer/id/" + 1))
mockMvc.perform(get("/id/" + 1))
.andExpect(status().isNotFound())
.andReturn();
}
......@@ -159,7 +159,7 @@ class CustomerControllerTest {
void getCustomerByEmail_returnsCustomer_200() throws Exception {
String email = "ashokk@nisum.com";
when(customerService.getCustomerByEmail(email)).thenReturn(getCustomerResponseObjectsList().stream().filter(customer -> customer.getEmail().equals(email)).findFirst().get());
mockMvc.perform(get("/customer/email/" + email))
mockMvc.perform(get("/email/" + email))
.andExpect(status().isOk())
.andExpect(jsonPath("$.email", is(email)))
.andReturn();
......@@ -169,20 +169,21 @@ class CustomerControllerTest {
void getCustomerByEmail_throwsCustomerNotFoundException_404() throws Exception {
String email = "ashokk@nisum.com";
when(customerService.getCustomerByEmail(email)).thenThrow(new CustomerNotFoundException());
mockMvc.perform(get("/customer/email/" + email))
mockMvc.perform(get("/email/" + email))
.andExpect(status().isNotFound())
.andReturn();
}
@Test
void getCustomersByFirstName() {
}
@Test
void getCustomersByLastName() {
void getCustomersByFirstName() throws Exception {
String fname = "Ashok";
when(customerService.getCustomerByFirstName(fname)).thenReturn(getCustomerResponseObjectsList().stream().filter(customer -> !customer.getFirstName().equals(fname)).collect(Collectors.toList()));
mockMvc.perform(get("/fname/" + fname))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(2)))
.andReturn();
}
@Test
void getCustomersByCustomerType_returnsCustomers_200() throws Exception {
int loyalValue = CustomerType.LOYAL.getValue();
......@@ -190,7 +191,7 @@ class CustomerControllerTest {
thenReturn(getCustomerResponseObjectsList().stream()
.filter(customer -> customer.getCustomerType().getValue() == loyalValue)
.collect(Collectors.toList()));
mockMvc.perform(get("/customer/type/" + loyalValue))
mockMvc.perform(get("/type/" + loyalValue))
.andExpect(status().isOk())
.andExpect(jsonPath("$", hasSize(2)))
.andExpect(jsonPath("$[0].customerType", is(loyalValue)))
......@@ -201,30 +202,22 @@ class CustomerControllerTest {
@Test
void getCustomersByCustomerType_throwsIllegalArgumentException_400() throws Exception {
int typeValue = 3;
mockMvc.perform(get("/customer/type/" + typeValue))
mockMvc.perform(get("/type/" + typeValue))
.andExpect(status().isBadRequest())
.andReturn();
}
@Test
void getRegisteredCustomersOnDate() {
}
@Test
void getRegisteredCustomersBetweenDates() {
}
@Test
void deleteById_returnsNoContent_204() throws Exception {
doNothing().when(customerService).deleteById(1L);
mockMvc.perform(delete("/customer/id/" + 1))
mockMvc.perform(delete("/id/" + 1))
.andExpect(status().isNoContent());
}
@Test
void deleteById_throwsCustomerNotFoundException_404() throws Exception {
doThrow(new CustomerNotFoundException()).when(customerService).deleteById(1L);
mockMvc.perform(delete("/customer/id/" + 1))
mockMvc.perform(delete("/id/" + 1))
.andExpect(status().isNotFound());
}
......@@ -232,7 +225,7 @@ class CustomerControllerTest {
Customer customer = new Customer();
customer.setId(null);
customer.setFirstName("Ashok");
customer.setLastName("Kumar");
customer.setLastName("Katam");
customer.setEmail("ashokk@nisum.com");
customer.setDayPhone("8989898989");
customer.setCustomerType(CustomerType.LOYAL);
......@@ -285,11 +278,18 @@ class CustomerControllerTest {
private List<Customer> getCustomerRequestObjectsList() {
Customer customer1 = getCustomerRequestObject();
Customer customer2 = getCustomerRequestObject();
customer2.setEmail("anandk@nisum.com");
customer2.setFirstName("Anand");
customer2.setLastName("Kumar");
Customer customer3 = getCustomerRequestObject();
customer3.setEmail("anandk1@nisum.com");
customer3.setCustomerType(CustomerType.REGULAR);
customer3.setFirstName("Anand");
customer3.setLastName("Katam");
return Arrays.asList(customer1, customer2, customer3);
}
......
package com.nisum.ecomcustomer.service.impl;
import com.nisum.ecomcustomer.exceptions.CustomerNotFoundException;
import com.nisum.ecomcustomer.exceptions.EmailAlreadyRegisteredException;
import com.nisum.ecomcustomer.model.Address;
import com.nisum.ecomcustomer.model.AddressType;
import com.nisum.ecomcustomer.model.Customer;
import com.nisum.ecomcustomer.model.CustomerType;
import com.nisum.ecomcustomer.repository.CustomerRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ActiveProfiles;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
@SpringBootTest
@ActiveProfiles("test")
class CustomerServiceImplTest {
@Autowired
CustomerServiceImpl customerService;
@MockBean
CustomerRepository customerRepository;
@Test
void register_throwsEmailAlreadyRegisteredException() {
String email = "ashokk@nisum.com";
Customer customerRequestToRegister = getCustomerRequestObject();
when(customerRepository.existsByEmail(email)).thenReturn(true);
assertThrows(EmailAlreadyRegisteredException.class, () -> customerService.register(customerRequestToRegister));
}
@Test
void register_success() {
String email = "ashokk@nisum.com";
Customer customerRequestToRegister = getCustomerRequestObject();
Customer customerResponseFromRepo = getCustomerResponseObject();
when(customerRepository.existsByEmail(email)).thenReturn(false);
when(customerRepository.save(customerRequestToRegister)).thenReturn(customerResponseFromRepo);
Customer customerResponse = customerService.register(customerRequestToRegister);
assertNull(customerRequestToRegister.getId());
assertNotNull(customerResponse.getId());
}
@Test
void registerAll() {
List<Customer> customersToSave = getCustomerRequestObjectsList();
List<Customer> customersSavedResponse = getCustomerResponseObjectsList();
when(customerRepository.existsByEmail(anyString())).thenReturn(false);
when(customerRepository.saveAll(customersToSave)).thenReturn(customersSavedResponse);
List<Customer> customersSaved = customerService.registerAll(customersToSave);
assertNotNull(customersSaved);
customersSaved.forEach(customer -> assertNotNull(customer.getId()));
}
@Test
void registerAll_throwsEmailAlreadyExistsException() {
List<Customer> customersToSave = getCustomerRequestObjectsList();
when(customerRepository.existsByEmailIn(anyList())).thenReturn(true);
assertThrows(EmailAlreadyRegisteredException.class, () -> customerService.registerAll(customersToSave));
}
@Test
void getCustomerById_success() {
Customer responseObject = getCustomerResponseObject();
when(customerRepository.findById(1L)).thenReturn(Optional.of(responseObject));
Customer customer = customerService.getCustomerById(1L);
assertEquals(responseObject.getId(), customer.getId());
}
@Test
void getCustomerById_throwsCustomerNotFoundException() {
when(customerRepository.findById(1L)).thenReturn(Optional.empty());
assertThrows(CustomerNotFoundException.class, () -> customerService.getCustomerById(1L));
}
private Customer getCustomerRequestObject() {
Customer customer = new Customer();
customer.setId(null);
customer.setFirstName("Ashok");
customer.setLastName("Katam");
customer.setEmail("ashokk@nisum.com");
customer.setDayPhone("8989898989");
customer.setCustomerType(CustomerType.LOYAL);
Address billingAddress = new Address();
billingAddress.setAddressLine1("Srinagar colony");
billingAddress.setAddressLine2("Ameerpet");
billingAddress.setAddressType(AddressType.BILLING);
billingAddress.setDefault(true);
billingAddress.setZipCode("500000");
billingAddress.setCity("Hyderabad");
billingAddress.setState("TS");
billingAddress.setCountry("IND");
Address shippingAddress1 = new Address();
shippingAddress1.setAddressLine1("Jawaharnagar colony");
shippingAddress1.setAddressLine2("Ameerpet");
shippingAddress1.setAddressType(AddressType.SHIPPING);
shippingAddress1.setDefault(true);
shippingAddress1.setZipCode("500001");
shippingAddress1.setCity("Hyderabad");
shippingAddress1.setState("TS");
shippingAddress1.setCountry("IND");
Address shippingAddress2 = new Address();
shippingAddress2.setAddressLine1("Krishnanagar colony");
shippingAddress2.setAddressLine2("Ameerpet");
shippingAddress2.setAddressType(AddressType.SHIPPING);
shippingAddress2.setDefault(false);
shippingAddress2.setZipCode("500002");
shippingAddress2.setCity("Hyderabad");
shippingAddress2.setState("TS");
shippingAddress2.setCountry("IND");
List<Address> addresses = Arrays.asList(billingAddress, shippingAddress1, shippingAddress2);
customer.setAddresses(addresses);
return customer;
}
private Customer getCustomerResponseObject() {
Customer customer = getCustomerRequestObject();
customer.setId(1L);
customer.setEmail(customer.getEmail().toLowerCase());
customer.setCreatedDate(LocalDate.now());
customer.setModifiedDate(LocalDateTime.now().plusSeconds(2));
return customer;
}
private List<Customer> getCustomerRequestObjectsList() {
Customer customer1 = getCustomerRequestObject();
Customer customer2 = getCustomerRequestObject();
customer2.setEmail("anandk@nisum.com");
customer2.setFirstName("Anand");
customer2.setLastName("Kumar");
Customer customer3 = getCustomerRequestObject();
customer3.setEmail("anandk1@nisum.com");
customer3.setCustomerType(CustomerType.REGULAR);
customer3.setFirstName("Anand");
customer3.setLastName("Katam");
return Arrays.asList(customer1, customer2, customer3);
}
private List<Customer> getCustomerResponseObjectsList() {
List<Customer> customers = getCustomerRequestObjectsList();
Long id = 1L;
for (Customer customer : customers) {
customer.setId(id++);
customer.setEmail(customer.getEmail().toLowerCase());
customer.setCreatedDate(LocalDate.now());
customer.setModifiedDate(LocalDateTime.now());
}
return customers;
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment