Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
K
kafka-mongo-poc
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
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Pavitra Mallemdoddi Papili
kafka-mongo-poc
Commits
fb6658d2
Commit
fb6658d2
authored
Apr 15, 2021
by
ppapili
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added test cases
parent
ba61a2a6
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
234 additions
and
1 deletion
+234
-1
pom.xml
pom.xml
+12
-0
EmployeeController.java
...va/com/poc/kafka/mongo/controller/EmployeeController.java
+1
-1
KafkaPocApplicationTests.java
...va/com/poc/kafka/mongo/test/KafkaPocApplicationTests.java
+23
-0
EmployeeControllerTest.java
...c/kafka/mongo/test/controller/EmployeeControllerTest.java
+159
-0
EmployeeServiceIntegrationTests.java
...a/mongo/test/service/EmployeeServiceIntegrationTests.java
+39
-0
No files found.
pom.xml
View file @
fb6658d2
...
...
@@ -36,6 +36,18 @@
<artifactId>
spring-boot-starter-test
</artifactId>
<scope>
test
</scope>
</dependency>
<dependency>
<groupId>
org.junit.vintage
</groupId>
<artifactId>
junit-vintage-engine
</artifactId>
<scope>
test
</scope>
<exclusions>
<exclusion>
<groupId>
org.hamcrest
</groupId>
<artifactId>
hamcrest-core
</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>
org.springframework.kafka
</groupId>
<artifactId>
spring-kafka-test
</artifactId>
...
...
src/main/java/com/poc/kafka/mongo/controller/EmployeeController.java
View file @
fb6658d2
...
...
@@ -55,7 +55,7 @@ public class EmployeeController {
}
@PostMapping
(
produces
=
"application/json"
,
consumes
=
"application/json"
,
path
=
"/upsert"
)
public
ResponseEntity
<
Emp
>
saveOrUpdateEmployee
(
@Valid
@RequestBody
Emp
emp
)
{
public
ResponseEntity
<
Void
>
saveOrUpdateEmployee
(
@Valid
@RequestBody
Emp
emp
)
{
employeeService
.
saveOrUpdate
(
emp
);
return
new
ResponseEntity
<>(
HttpStatus
.
OK
);
}
...
...
src/test/java/com/poc/kafka/KafkaPocApplicationTests.java
→
src/test/java/com/poc/kafka/
mongo/test/
KafkaPocApplicationTests.java
View file @
fb6658d2
package
com
.
poc
.
kafka
;
package
com
.
poc
.
kafka
.
mongo
.
test
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
org.junit.jupiter.api.Test
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
com.poc.kafka.mongo.controller.EmployeeController
;
@SpringBootTest
class
KafkaPocApplicationTests
{
@Autowired
private
EmployeeController
employeeController
;
@Test
void
contextLoads
()
{
assertThat
(
employeeController
).
isNotNull
();
}
}
src/test/java/com/poc/kafka/mongo/test/controller/EmployeeControllerTest.java
0 → 100644
View file @
fb6658d2
package
com
.
poc
.
kafka
.
mongo
.
test
.
controller
;
import
static
org
.
mockito
.
ArgumentMatchers
.
any
;
import
static
org
.
mockito
.
Mockito
.
times
;
import
static
org
.
mockito
.
Mockito
.
when
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
request
.
MockMvcRequestBuilders
.
delete
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
request
.
MockMvcRequestBuilders
.
get
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
request
.
MockMvcRequestBuilders
.
post
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultHandlers
.
print
;
import
static
org
.
springframework
.
test
.
web
.
servlet
.
result
.
MockMvcResultMatchers
.
status
;
import
java.util.ArrayList
;
import
java.util.List
;
import
org.junit.jupiter.api.BeforeEach
;
import
org.junit.jupiter.api.Test
;
import
org.mockito.Mockito
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest
;
import
org.springframework.boot.test.mock.mockito.MockBean
;
import
org.springframework.http.MediaType
;
import
org.springframework.test.web.servlet.MockMvc
;
import
org.springframework.test.web.servlet.result.MockMvcResultMatchers
;
import
com.fasterxml.jackson.core.JsonProcessingException
;
import
com.fasterxml.jackson.databind.ObjectMapper
;
import
com.poc.kafka.mongo.controller.EmployeeController
;
import
com.poc.kafka.mongo.exception.EmployeeException
;
import
com.poc.kafka.mongo.model.Emp
;
import
com.poc.kafka.mongo.service.EmployeeService
;
import
com.poc.kafka.mongo.util.ErrorEnum
;
@WebMvcTest
(
EmployeeController
.
class
)
public
class
EmployeeControllerTest
{
@Autowired
private
MockMvc
mockMvc
;
@MockBean
private
EmployeeService
employeeService
;
final
List
<
Emp
>
list
=
new
ArrayList
<>();
@BeforeEach
public
void
setup
()
{
Emp
e
=
new
Emp
();
e
.
setEmpId
(
101L
);
e
.
setEmpName
(
"ABC"
);
list
.
add
(
e
);
}
@Test
public
void
findAllEmployees
()
throws
Exception
{
when
(
employeeService
.
findAll
()).
thenReturn
(
list
);
/**
* s Provide ContentType and accept headers otherwise will end up with status
* 415
*/
this
.
mockMvc
.
perform
(
get
(
"/employees/"
).
accept
(
MediaType
.
APPLICATION_JSON
).
contentType
(
MediaType
.
APPLICATION_JSON
))
.
andDo
(
print
()).
andExpect
(
status
().
isOk
())
.
andExpect
(
MockMvcResultMatchers
.
jsonPath
(
"$.[0].empName"
).
value
(
"ABC"
));
Mockito
.
verify
(
employeeService
,
times
(
1
)).
findAll
();
}
@Test
public
void
findByEmpId
()
throws
Exception
{
when
(
employeeService
.
findByEmpId
(
101L
)).
thenReturn
(
list
.
get
(
0
));
this
.
mockMvc
.
perform
(
get
(
"/employees/empId/{empId}"
,
101L
).
accept
(
MediaType
.
APPLICATION_JSON
)
.
contentType
(
MediaType
.
APPLICATION_JSON
))
.
andDo
(
print
()).
andExpect
(
status
().
isOk
())
.
andExpect
(
MockMvcResultMatchers
.
jsonPath
(
"$.empName"
).
value
(
"ABC"
));
Mockito
.
verify
(
employeeService
,
times
(
1
)).
findByEmpId
(
any
(
Long
.
class
));
}
@Test
public
void
saveEmployee
()
throws
Exception
{
Emp
e11
=
new
Emp
();
e11
.
setEmpId
(
102L
);
e11
.
setEmpName
(
"XYZ"
);
e11
.
setDeptId
(
"10"
);
e11
.
setDeptName
(
"Development"
);
e11
.
setEmpSal
(
200000.0
);
when
(
employeeService
.
save
(
any
(
Emp
.
class
))).
thenReturn
(
e11
);
this
.
mockMvc
.
perform
(
post
(
"/employees/save"
).
contentType
(
MediaType
.
APPLICATION_JSON
)
.
accept
(
MediaType
.
APPLICATION_JSON
).
content
(
mapToJson
(
e11
)))
.
andDo
(
print
()).
andExpect
(
status
().
isOk
())
.
andExpect
(
MockMvcResultMatchers
.
jsonPath
(
"$.empName"
).
value
(
"XYZ"
));
Mockito
.
verify
(
employeeService
,
times
(
1
)).
save
(
any
(
Emp
.
class
));
}
@Test
public
void
upsertEmployee
()
throws
Exception
{
Emp
e11
=
new
Emp
();
e11
.
setEmpId
(
102L
);
e11
.
setEmpName
(
"XYZ"
);
e11
.
setDeptId
(
"10"
);
e11
.
setDeptName
(
"Development"
);
e11
.
setEmpSal
(
200005.0
);
Mockito
.
doNothing
().
when
(
employeeService
).
saveOrUpdate
(
any
(
Emp
.
class
));
this
.
mockMvc
.
perform
(
post
(
"/employees/upsert"
).
contentType
(
MediaType
.
APPLICATION_JSON
)
.
accept
(
MediaType
.
APPLICATION_JSON
).
content
(
mapToJson
(
e11
)))
.
andDo
(
print
()).
andExpect
(
status
().
isOk
());
Mockito
.
verify
(
employeeService
,
times
(
1
)).
saveOrUpdate
(
any
(
Emp
.
class
));
}
@Test
public
void
deleteByEmpId
()
throws
Exception
{
when
(
employeeService
.
deleteByEmpId
(
101L
)).
thenReturn
(
101L
);
this
.
mockMvc
.
perform
(
delete
(
"/employees/delete/{empId}"
,
101L
).
accept
(
MediaType
.
APPLICATION_JSON
)
.
contentType
(
MediaType
.
APPLICATION_JSON
)).
andDo
(
print
()).
andExpect
(
status
().
isOk
());
Mockito
.
verify
(
employeeService
,
times
(
1
)).
deleteByEmpId
(
any
(
Long
.
class
));
}
@Test
public
void
deleteByEmpIdForException
()
throws
Exception
{
when
(
employeeService
.
deleteByEmpId
(
101L
)).
thenThrow
(
new
EmployeeException
(
ErrorEnum
.
EC_1
.
name
(),
ErrorEnum
.
getDesription
(
"EC_1"
)));
this
.
mockMvc
.
perform
(
delete
(
"/employees/delete/{empId}"
,
101L
).
accept
(
MediaType
.
APPLICATION_JSON
)
.
contentType
(
MediaType
.
APPLICATION_JSON
)).
andDo
(
print
()).
andExpect
(
MockMvcResultMatchers
.
jsonPath
(
"$.errorDescription"
).
value
(
"Record not found"
));
Mockito
.
verify
(
employeeService
,
times
(
1
)).
deleteByEmpId
(
any
(
Long
.
class
));
}
@Test
public
void
publishEmployee
()
throws
Exception
{
Emp
e11
=
new
Emp
();
e11
.
setEmpId
(
103L
);
e11
.
setEmpName
(
"MOU"
);
e11
.
setDeptId
(
"10"
);
e11
.
setDeptName
(
"Development"
);
e11
.
setEmpSal
(
200000.0
);
Mockito
.
doNothing
().
when
(
employeeService
).
send
(
any
(
Emp
.
class
));
this
.
mockMvc
.
perform
(
post
(
"/employees/produce"
).
contentType
(
MediaType
.
APPLICATION_JSON
)
.
accept
(
MediaType
.
APPLICATION_JSON
).
content
(
mapToJson
(
e11
)))
.
andDo
(
print
()).
andExpect
(
status
().
isOk
()).
andExpect
(
MockMvcResultMatchers
.
content
().
string
(
"Employee details published"
));
Mockito
.
verify
(
employeeService
,
times
(
1
)).
send
(
any
(
Emp
.
class
));
}
private
String
mapToJson
(
Object
obj
)
throws
JsonProcessingException
{
ObjectMapper
objectMapper
=
new
ObjectMapper
();
return
objectMapper
.
writeValueAsString
(
obj
);
}
}
src/test/java/com/poc/kafka/mongo/test/service/EmployeeServiceIntegrationTests.java
0 → 100644
View file @
fb6658d2
package
com
.
poc
.
kafka
.
mongo
.
test
.
service
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
java.util.List
;
import
org.junit.jupiter.api.Assertions
;
import
org.junit.jupiter.api.Test
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.test.context.SpringBootTest
;
import
com.poc.kafka.mongo.exception.EmployeeException
;
import
com.poc.kafka.mongo.model.Emp
;
import
com.poc.kafka.mongo.service.EmployeeService
;
@SpringBootTest
public
class
EmployeeServiceIntegrationTests
{
@Autowired
EmployeeService
employeeService
;
/**
* Integration Testcases
*/
@Test
public
void
findAll
()
{
List
<
Emp
>
list
=
employeeService
.
findAll
();
assertNotNull
(
list
);
}
@Test
public
void
findByEmpId
()
{
Assertions
.
assertThrows
(
EmployeeException
.
class
,
()->
employeeService
.
findByEmpId
(
2000L
));
}
@Test
public
void
deleteById
()
{
Assertions
.
assertThrows
(
EmployeeException
.
class
,
()->
employeeService
.
deleteByEmpId
(
2000L
));
}
}
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