Commit 65f2dd55 authored by Ashok Kumar K's avatar Ashok Kumar K

Added few test cases for CustomerController

parent 73d1aaa6
...@@ -4,7 +4,6 @@ import org.springframework.beans.factory.annotation.Value; ...@@ -4,7 +4,6 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration; import org.springframework.data.mongodb.config.AbstractMongoClientConfiguration;
import org.springframework.data.mongodb.config.EnableMongoAuditing; import org.springframework.data.mongodb.config.EnableMongoAuditing;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
import static com.nisum.ecomcustomer.config.mongodb.CustomMongoConverters.*; import static com.nisum.ecomcustomer.config.mongodb.CustomMongoConverters.*;
...@@ -23,21 +22,6 @@ public class MongoConfig extends AbstractMongoClientConfiguration { ...@@ -23,21 +22,6 @@ public class MongoConfig extends AbstractMongoClientConfiguration {
return databaseName; return databaseName;
} }
//TODO - know about the good practices.
// @Bean
// @Override
// public MappingMongoConverter mappingMongoConverter(MongoDatabaseFactory databaseFactory, MongoCustomConversions customConversions, MongoMappingContext mappingContext) {
// MappingMongoConverter mmc = super.mappingMongoConverter(databaseFactory, customConversions, mappingContext);
// mmc.setTypeMapper(getTypeMapper());
// return mmc;
// }
//
// @Bean
// public MongoTypeMapper getTypeMapper() {
// return new CustomMongoTypeMapper();
// }
@Override @Override
protected void configureConverters(MongoConverterConfigurationAdapter adapter) { protected void configureConverters(MongoConverterConfigurationAdapter adapter) {
adapter.registerConverter(new CustomerTypeToIntegerConverter()); adapter.registerConverter(new CustomerTypeToIntegerConverter());
...@@ -47,13 +31,6 @@ public class MongoConfig extends AbstractMongoClientConfiguration { ...@@ -47,13 +31,6 @@ public class MongoConfig extends AbstractMongoClientConfiguration {
} }
} }
class CustomMongoTypeMapper extends DefaultMongoTypeMapper {
CustomMongoTypeMapper() {
super(null);
}
}
...@@ -23,6 +23,7 @@ public class CustomerDbOpsEventListener extends AbstractMongoEventListener<Custo ...@@ -23,6 +23,7 @@ public class CustomerDbOpsEventListener extends AbstractMongoEventListener<Custo
if (source.getId() == null) { if (source.getId() == null) {
DbSequence dbSequence = dbSequenceRepository.findBySequenceName(Customer.SEQUENCE_NAME).orElse(new DbSequence(Customer.SEQUENCE_NAME)); DbSequence dbSequence = dbSequenceRepository.findBySequenceName(Customer.SEQUENCE_NAME).orElse(new DbSequence(Customer.SEQUENCE_NAME));
source.setId(dbSequence.incrementLastId()); source.setId(dbSequence.incrementLastId());
source.setEmail(source.getEmail().toLowerCase());
source.setCreatedDate(LocalDate.now()); source.setCreatedDate(LocalDate.now());
dbSequenceRepository.save(dbSequence); dbSequenceRepository.save(dbSequence);
} }
......
...@@ -46,9 +46,7 @@ public class CustomerController { ...@@ -46,9 +46,7 @@ public class CustomerController {
return ResponseEntity.ok(customerService.getCustomerById(id)); return ResponseEntity.ok(customerService.getCustomerById(id));
} }
//TODO - make email field unique @GetMapping("/email/{email}")
// @GetMapping("/email/{email}")
public ResponseEntity<Customer> getCustomerByEmail(@PathVariable @Email String email) { public ResponseEntity<Customer> getCustomerByEmail(@PathVariable @Email String email) {
return ResponseEntity.ok(customerService.getCustomerByEmail(email)); return ResponseEntity.ok(customerService.getCustomerByEmail(email));
} }
......
...@@ -26,6 +26,11 @@ public class CustomerServiceExceptionHandler { ...@@ -26,6 +26,11 @@ public class CustomerServiceExceptionHandler {
return buildErrorResponseEntity(ex.getLocalizedMessage(), HttpStatus.NOT_FOUND, httpServletRequest); return buildErrorResponseEntity(ex.getLocalizedMessage(), HttpStatus.NOT_FOUND, httpServletRequest);
} }
@ExceptionHandler(EmailAlreadyRegisteredException.class)
public ResponseEntity<ErrorResponse> handleEmailAlreadyRegisteredException(EmailAlreadyRegisteredException ex, HttpServletRequest httpServletRequest) {
return buildErrorResponseEntity(ex.getLocalizedMessage(), HttpStatus.CONFLICT, httpServletRequest);
}
@ExceptionHandler(MethodArgumentNotValidException.class) @ExceptionHandler(MethodArgumentNotValidException.class)
public ResponseEntity<Object> handleMethodArgumentNotValidException(MethodArgumentNotValidException ex, HttpServletRequest httpServletRequest) { public ResponseEntity<Object> handleMethodArgumentNotValidException(MethodArgumentNotValidException ex, HttpServletRequest httpServletRequest) {
ErrorResponse errorResponse = buildErrorResponse("Invalid payload or params", HttpStatus.BAD_REQUEST, httpServletRequest); ErrorResponse errorResponse = buildErrorResponse("Invalid payload or params", HttpStatus.BAD_REQUEST, httpServletRequest);
......
package com.nisum.ecomcustomer.exceptions;
public class EmailAlreadyRegisteredException extends RuntimeException {
public EmailAlreadyRegisteredException() {
super("email already registered");
}
public EmailAlreadyRegisteredException(String message) {
super(message);
}
public EmailAlreadyRegisteredException(String message, Throwable cause) {
super(message, cause);
}
}
...@@ -5,14 +5,19 @@ import org.springframework.data.mongodb.repository.MongoRepository; ...@@ -5,14 +5,19 @@ import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query; import org.springframework.data.mongodb.repository.Query;
import java.util.List; import java.util.List;
import java.util.Optional;
public interface CustomerRepository extends MongoRepository<Customer, Long> { public interface CustomerRepository extends MongoRepository<Customer, Long> {
Customer findByEmailIgnoreCase(String email); Optional<Customer> findByEmailIgnoreCase(String email);
@Query(value = "{'firstName': {$regex : ?0, $options: 'i'}}") @Query(value = "{'firstName': {$regex : ?0, $options: 'i'}}")
List<Customer> findByFirstName(String fName); List<Customer> findByFirstName(String fName);
List<Customer> findByLastNameIgnoreCase(String lName); List<Customer> findByLastNameIgnoreCase(String lName);
boolean existsByEmail(String email);
boolean existsByEmailIn(List<String> emails);
} }
package com.nisum.ecomcustomer.service.impl; package com.nisum.ecomcustomer.service.impl;
import com.nisum.ecomcustomer.exceptions.CustomerNotFoundException; import com.nisum.ecomcustomer.exceptions.CustomerNotFoundException;
import com.nisum.ecomcustomer.exceptions.EmailAlreadyRegisteredException;
import com.nisum.ecomcustomer.model.Customer; import com.nisum.ecomcustomer.model.Customer;
import com.nisum.ecomcustomer.repository.CustomerRepository; import com.nisum.ecomcustomer.repository.CustomerRepository;
import com.nisum.ecomcustomer.service.CustomerService; import com.nisum.ecomcustomer.service.CustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
@Slf4j
@Service @Service
public class CustomerServiceImpl implements CustomerService { public class CustomerServiceImpl implements CustomerService {
...@@ -22,11 +26,17 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -22,11 +26,17 @@ public class CustomerServiceImpl implements CustomerService {
@Override @Override
public Customer register(Customer customer) { public Customer register(Customer customer) {
customer.setId(null); // auto-generated customer.setId(null); // auto-generated
if (customerRepository.existsByEmail(customer.getEmail())) {
throw new EmailAlreadyRegisteredException();
}
return customerRepository.save(customer); return customerRepository.save(customer);
} }
@Override @Override
public List<Customer> registerAll(List<Customer> customers) { public List<Customer> registerAll(List<Customer> customers) {
List<String> emails = customers.stream().map(Customer::getEmail).collect(Collectors.toList());
if (customerRepository.existsByEmailIn(emails))
throw new EmailAlreadyRegisteredException("one of the emails is already registered");
return customerRepository.saveAll(customers); return customerRepository.saveAll(customers);
} }
...@@ -54,7 +64,7 @@ public class CustomerServiceImpl implements CustomerService { ...@@ -54,7 +64,7 @@ public class CustomerServiceImpl implements CustomerService {
@Override @Override
public Customer getCustomerByEmail(String email) { public Customer getCustomerByEmail(String email) {
return customerRepository.findByEmailIgnoreCase(email); return customerRepository.findByEmailIgnoreCase(email).orElseThrow(CustomerNotFoundException::new);
} }
@Override @Override
......
server:
port: 8181
spring:
data:
mongodb:
host: localhost
port: 27017
database: ecom-test
logging:
level:
web: debug
\ No newline at end of file
package com.nisum.ecomcustomer.controller;
import com.nisum.ecomcustomer.model.Customer;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
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.test.context.ActiveProfiles;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.TestInstance.Lifecycle;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
@TestInstance(Lifecycle.PER_CLASS)
@ActiveProfiles("test")
public class CustomerControllerIntegrationTest {
@LocalServerPort
private int port;
private String url;
@Autowired
TestRestTemplate testRestTemplate;
@BeforeAll
public void setUp() {
url = "http://localhost:" + port + "/customer";
}
@Test
public void testGetById() {
Customer responseObject = testRestTemplate.getForObject(url + "/id/2", Customer.class);
assertEquals("Ashokk", responseObject.getFirstName());
}
}
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