Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
E
ecom-customer
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Ashok Kumar K
ecom-customer
Commits
65f2dd55
Commit
65f2dd55
authored
May 28, 2020
by
Ashok Kumar K
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added few test cases for CustomerController
parent
73d1aaa6
Changes
10
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
371 additions
and
28 deletions
+371
-28
MongoConfig.java
...va/com/nisum/ecomcustomer/config/mongodb/MongoConfig.java
+0
-23
CustomerDbOpsEventListener.java
.../config/mongodb/listeners/CustomerDbOpsEventListener.java
+1
-0
CustomerController.java
...com/nisum/ecomcustomer/controller/CustomerController.java
+1
-3
CustomerServiceExceptionHandler.java
...mcustomer/exceptions/CustomerServiceExceptionHandler.java
+5
-0
EmailAlreadyRegisteredException.java
...mcustomer/exceptions/EmailAlreadyRegisteredException.java
+17
-0
CustomerRepository.java
...com/nisum/ecomcustomer/repository/CustomerRepository.java
+6
-1
CustomerServiceImpl.java
.../nisum/ecomcustomer/service/impl/CustomerServiceImpl.java
+11
-1
application-test.yml
src/main/resources/application-test.yml
+13
-0
CustomerControllerIntegrationTest.java
...ustomer/controller/CustomerControllerIntegrationTest.java
+41
-0
CustomerControllerTest.java
...nisum/ecomcustomer/controller/CustomerControllerTest.java
+276
-0
No files found.
src/main/java/com/nisum/ecomcustomer/config/mongodb/MongoConfig.java
View file @
65f2dd55
...
...
@@ -4,7 +4,6 @@ import org.springframework.beans.factory.annotation.Value;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.data.mongodb.config.AbstractMongoClientConfiguration
;
import
org.springframework.data.mongodb.config.EnableMongoAuditing
;
import
org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper
;
import
org.springframework.data.mongodb.repository.config.EnableMongoRepositories
;
import
static
com
.
nisum
.
ecomcustomer
.
config
.
mongodb
.
CustomMongoConverters
.*;
...
...
@@ -23,21 +22,6 @@ public class MongoConfig extends AbstractMongoClientConfiguration {
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
protected
void
configureConverters
(
MongoConverterConfigurationAdapter
adapter
)
{
adapter
.
registerConverter
(
new
CustomerTypeToIntegerConverter
());
...
...
@@ -47,13 +31,6 @@ public class MongoConfig extends AbstractMongoClientConfiguration {
}
}
class
CustomMongoTypeMapper
extends
DefaultMongoTypeMapper
{
CustomMongoTypeMapper
()
{
super
(
null
);
}
}
src/main/java/com/nisum/ecomcustomer/config/mongodb/listeners/CustomerDbOpsEventListener.java
View file @
65f2dd55
...
...
@@ -23,6 +23,7 @@ public class CustomerDbOpsEventListener extends AbstractMongoEventListener<Custo
if
(
source
.
getId
()
==
null
)
{
DbSequence
dbSequence
=
dbSequenceRepository
.
findBySequenceName
(
Customer
.
SEQUENCE_NAME
).
orElse
(
new
DbSequence
(
Customer
.
SEQUENCE_NAME
));
source
.
setId
(
dbSequence
.
incrementLastId
());
source
.
setEmail
(
source
.
getEmail
().
toLowerCase
());
source
.
setCreatedDate
(
LocalDate
.
now
());
dbSequenceRepository
.
save
(
dbSequence
);
}
...
...
src/main/java/com/nisum/ecomcustomer/controller/CustomerController.java
View file @
65f2dd55
...
...
@@ -46,9 +46,7 @@ public class CustomerController {
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
)
{
return
ResponseEntity
.
ok
(
customerService
.
getCustomerByEmail
(
email
));
}
...
...
src/main/java/com/nisum/ecomcustomer/exceptions/CustomerServiceExceptionHandler.java
View file @
65f2dd55
...
...
@@ -26,6 +26,11 @@ public class CustomerServiceExceptionHandler {
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
)
public
ResponseEntity
<
Object
>
handleMethodArgumentNotValidException
(
MethodArgumentNotValidException
ex
,
HttpServletRequest
httpServletRequest
)
{
ErrorResponse
errorResponse
=
buildErrorResponse
(
"Invalid payload or params"
,
HttpStatus
.
BAD_REQUEST
,
httpServletRequest
);
...
...
src/main/java/com/nisum/ecomcustomer/exceptions/EmailAlreadyRegisteredException.java
0 → 100644
View file @
65f2dd55
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
);
}
}
src/main/java/com/nisum/ecomcustomer/repository/CustomerRepository.java
View file @
65f2dd55
...
...
@@ -5,14 +5,19 @@ import org.springframework.data.mongodb.repository.MongoRepository;
import
org.springframework.data.mongodb.repository.Query
;
import
java.util.List
;
import
java.util.Optional
;
public
interface
CustomerRepository
extends
MongoRepository
<
Customer
,
Long
>
{
Customer
findByEmailIgnoreCase
(
String
email
);
Optional
<
Customer
>
findByEmailIgnoreCase
(
String
email
);
@Query
(
value
=
"{'firstName': {$regex : ?0, $options: 'i'}}"
)
List
<
Customer
>
findByFirstName
(
String
fName
);
List
<
Customer
>
findByLastNameIgnoreCase
(
String
lName
);
boolean
existsByEmail
(
String
email
);
boolean
existsByEmailIn
(
List
<
String
>
emails
);
}
src/main/java/com/nisum/ecomcustomer/service/impl/CustomerServiceImpl.java
View file @
65f2dd55
package
com
.
nisum
.
ecomcustomer
.
service
.
impl
;
import
com.nisum.ecomcustomer.exceptions.CustomerNotFoundException
;
import
com.nisum.ecomcustomer.exceptions.EmailAlreadyRegisteredException
;
import
com.nisum.ecomcustomer.model.Customer
;
import
com.nisum.ecomcustomer.repository.CustomerRepository
;
import
com.nisum.ecomcustomer.service.CustomerService
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.data.mongodb.core.MongoTemplate
;
import
org.springframework.stereotype.Service
;
import
java.util.List
;
import
java.util.stream.Collectors
;
@Slf4j
@Service
public
class
CustomerServiceImpl
implements
CustomerService
{
...
...
@@ -22,11 +26,17 @@ public class CustomerServiceImpl implements CustomerService {
@Override
public
Customer
register
(
Customer
customer
)
{
customer
.
setId
(
null
);
// auto-generated
if
(
customerRepository
.
existsByEmail
(
customer
.
getEmail
()))
{
throw
new
EmailAlreadyRegisteredException
();
}
return
customerRepository
.
save
(
customer
);
}
@Override
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
);
}
...
...
@@ -54,7 +64,7 @@ public class CustomerServiceImpl implements CustomerService {
@Override
public
Customer
getCustomerByEmail
(
String
email
)
{
return
customerRepository
.
findByEmailIgnoreCase
(
email
);
return
customerRepository
.
findByEmailIgnoreCase
(
email
)
.
orElseThrow
(
CustomerNotFoundException:
:
new
)
;
}
@Override
...
...
src/main/resources/application-test.yml
0 → 100644
View file @
65f2dd55
server
:
port
:
8181
spring
:
data
:
mongodb
:
host
:
localhost
port
:
27017
database
:
ecom-test
logging
:
level
:
web
:
debug
\ No newline at end of file
src/test/java/com/nisum/ecomcustomer/controller/CustomerControllerIntegrationTest.java
0 → 100644
View file @
65f2dd55
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
());
}
}
src/test/java/com/nisum/ecomcustomer/controller/CustomerControllerTest.java
0 → 100644
View file @
65f2dd55
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment