Commit 75b72e89 authored by Simhadri Guntreddi's avatar Simhadri Guntreddi

Merge branch 'Junit_Test_Branch' into 'master'

Added Junit Test

See merge request !3
parents 76daa775 feaea994
No preview for this file type
This diff is collapsed.
spring:
application:
name: store-producer
datasource:
url: jdbc:postgresql://127.0.0.1:5432/epe
username: postgres
password: welcome
driver-class-name: org.postgresql.Driver
jpa:
show-sql: true
properties:
hibernate:
dialect: org.hibernate.dialect.PostgreSQLDialect
server:
port: 8200
kafka:
producer:
offer-transaction-record-topic : transactionRecord
schema:
api:
baseurl: http://localhost:8082
{
"name": "TransactionRecorder",
"type": "record",
"namespace": "com.safeway.epe.domain",
"fields": [
{
"name": "uuid",
"type": "string"
},
{
"name": "offertransactionresponse",
"type": "string"
},
{
"name": "offers",
"type": "string"
}
]
}
\ No newline at end of file
...@@ -2,6 +2,8 @@ package com.safeway.epe.controller; ...@@ -2,6 +2,8 @@ package com.safeway.epe.controller;
import com.safeway.epe.domain.Offsets; import com.safeway.epe.domain.Offsets;
import com.safeway.epe.service.TransactionService; import com.safeway.epe.service.TransactionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
...@@ -13,12 +15,14 @@ import java.util.List; ...@@ -13,12 +15,14 @@ import java.util.List;
@RestController @RestController
public class TransactionController public class TransactionController
{ {
Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired @Autowired
private TransactionService service; private TransactionService service;
@GetMapping("transaction/{uuid}") @GetMapping("transaction/{uuid}")
public ResponseEntity<List<Offsets>> produce(@PathVariable("uuid") String uuid) public ResponseEntity<List<Offsets>> produce(@PathVariable("uuid") String uuid)
{ {
logger.info("Started producing message for transactionId :: "+uuid);
return service.produceMessage(uuid); return service.produceMessage(uuid);
} }
} }
...@@ -43,7 +43,8 @@ public class TransactionServiceImpl implements TransactionService ...@@ -43,7 +43,8 @@ public class TransactionServiceImpl implements TransactionService
private ResponseEntity<List<Offsets>> sendData(TransactionRecorder transactionRecorder, boolean isReprocesed) { private ResponseEntity<List<Offsets>> sendData(TransactionRecorder transactionRecorder, boolean isReprocesed) {
Record record = TransactionRecordMapper.INSTANCE.recordConvertion(transactionRecorder.getUuid().toString(), transactionRecorder); Record record = TransactionRecordMapper.INSTANCE.recordConvertion(transactionRecorder.getUuid().toString(), transactionRecorder);
TransactionPayload transactionPayload = TransactionPayloadMapper.INSTANCE.transactionPayloadMapper(record); TransactionPayload transactionPayload = TransactionPayloadMapper.INSTANCE.transactionPayloadMapper(record);
return ResponseEntity.ok(producerService.produce(transactionPayload,configProps.getOfferTransactionRecordTopic(),transactionRecorder,isReprocesed)); List<Offsets> offsetsList = producerService.produce(transactionPayload,configProps.getOfferTransactionRecordTopic(),transactionRecorder,isReprocesed);
return ResponseEntity.ok(offsetsList);
} }
public ResponseEntity<String> errorResponse(TransactionRecorder transactionRecorder, String exceptionMessage, boolean isReprocesed) { public ResponseEntity<String> errorResponse(TransactionRecorder transactionRecorder, String exceptionMessage, boolean isReprocesed) {
......
{
"name": "TransactionRecorder",
"type": "record",
"namespace": "com.safeway.epe.domain",
"fields": [
{
"name": "uuid",
"type": "string"
},
{
"name": "offertransactionresponse",
"type": "string"
},
{
"name": "offers",
"type": "string"
}
]
}
\ No newline at end of file
//package com.safeway.epe; package com.safeway.epe;
//
//import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Disabled;
//import org.springframework.boot.test.context.SpringBootTest; import org.junit.jupiter.api.Test;
// import org.springframework.boot.test.context.SpringBootTest;
//@SpringBootTest
//class StoreProducerApplicationTests { @Disabled
// @SpringBootTest
// @Test class StoreProducerApplicationTests {
// void contextLoads() {
// } @Test
// void contextLoads() {
//} }
}
package com.safeway.epe.controller; package com.safeway.epe.controller;
import com.safeway.epe.domain.Offsets; import java.util.ArrayList;
import com.safeway.epe.domain.TransactionPayload; import java.util.Arrays;
import com.safeway.epe.domain.TransactionRecorder; import java.util.List;
import com.safeway.epe.repository.TransactionRepository; import java.util.Optional;
import com.safeway.epe.service.ProducerService; import java.util.UUID;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
import java.util.*; import com.safeway.epe.domain.Offsets;
import com.safeway.epe.domain.TransactionPayload;
import com.safeway.epe.domain.TransactionRecorder;
import com.safeway.epe.repository.TransactionRepository;
import com.safeway.epe.service.ProducerService;
@SpringBootTest @SpringBootTest
@DisplayName("Component_Test_Case_For_Transaction_Controller")
public class TransactionControllerTest { public class TransactionControllerTest {
@Autowired Logger logger = LoggerFactory.getLogger(this.getClass());
private TransactionController transactionController;
@MockBean @Autowired
private ProducerService producerService; private TransactionController transactionController;
@MockBean @MockBean
private TransactionRepository transactionRepository; private ProducerService producerService;
private static String transactionId = "0ed78832-ea04-4062-b7dd-35c6c903c44c"; @MockBean
private List<Offsets> listOfOffest; private TransactionRepository transactionRepository;
@Test private static String transactionId = "0ed78832-ea04-4062-b7dd-35c6c903c44c";
public void testProducer_Success() { private List<Offsets> listOfOffest;
listOfOffest=new ArrayList<>();
Offsets offsets=new Offsets();
offsets.setPartition("0");
offsets.setOffset("1");
listOfOffest.add(offsets);
UUID uuid = UUID.fromString(transactionId); @Test
Optional<TransactionRecorder> optionalOfferTransaction = Optional.of(createTransactionRecorder(uuid)); public void testProducer_Success() {
Mockito.when(transactionRepository.findById(Mockito.any(UUID.class))).thenReturn(optionalOfferTransaction); listOfOffest = new ArrayList<>();
Mockito.when(producerService.produce(Mockito.any(TransactionPayload.class), Mockito.anyString(), Offsets offsets = new Offsets();
Mockito.any(TransactionRecorder.class),Mockito.anyBoolean() offsets.setPartition("0");
)).thenReturn(listOfOffest); offsets.setOffset("1");
listOfOffest.add(offsets);
ResponseEntity<List<Offsets>> responseEntity = transactionController.produce(transactionId); UUID uuid = UUID.fromString(transactionId);
Assertions.assertEquals(responseEntity.getStatusCode(), HttpStatus.OK); Optional<TransactionRecorder> optionalOfferTransaction = Optional.of(createTransactionRecorder(uuid));
} Mockito.when(transactionRepository.findById(Mockito.any(UUID.class))).thenReturn(optionalOfferTransaction);
Mockito.when(producerService.produce(Mockito.any(TransactionPayload.class), Mockito.anyString(),
Mockito.any(TransactionRecorder.class), Mockito.anyBoolean())).thenReturn(listOfOffest);
@Test ResponseEntity<List<Offsets>> responseEntity = transactionController.produce(transactionId);
public void testProducer_Failure() { Assertions.assertEquals(responseEntity.getStatusCode(), HttpStatus.OK);
Optional<TransactionRecorder> optionalOfferTransaction = Optional.empty(); }
Mockito.when(transactionRepository.findById(Mockito.any(UUID.class))).thenReturn(optionalOfferTransaction);
ResponseEntity<List<Offsets>> responseEntity = transactionController.produce(transactionId);
Assertions.assertEquals(responseEntity.getStatusCode(), HttpStatus.NOT_FOUND);
Assertions.assertEquals(responseEntity.getBody(), Arrays.asList(new Offsets()));
}
private TransactionRecorder createTransactionRecorder(UUID uuid) { @Test
TransactionRecorder transactionRecorder = new TransactionRecorder(); public void testProducer_Failure() {
transactionRecorder.setUuid(uuid); Optional<TransactionRecorder> optionalOfferTransaction = Optional.empty();
transactionRecorder.setProcessed(true); Mockito.when(transactionRepository.findById(Mockito.any(UUID.class))).thenReturn(optionalOfferTransaction);
transactionRecorder.setOffers("offers"); ResponseEntity<List<Offsets>> responseEntity = transactionController.produce(transactionId);
transactionRecorder.setOfferTransactionResponse("TransactionResponse"); Assertions.assertEquals(responseEntity.getStatusCode(), HttpStatus.NOT_FOUND);
return transactionRecorder; Assertions.assertEquals(responseEntity.getBody(), Arrays.asList(new Offsets()));
} }
private TransactionRecorder createTransactionRecorder(UUID uuid) {
TransactionRecorder transactionRecorder = new TransactionRecorder();
transactionRecorder.setUuid(uuid);
transactionRecorder.setProcessed(true);
transactionRecorder.setOffers("offers");
transactionRecorder.setOfferTransactionResponse("TransactionResponse");
return transactionRecorder;
}
} }
package com.safeway.epe.service;
import com.safeway.epe.config.ProducerServiceProxy;
import com.safeway.epe.domain.ConsumerPayload;
import com.safeway.epe.domain.Offsets;
import com.safeway.epe.domain.TransactionPayload;
import com.safeway.epe.domain.TransactionRecorder;
import org.junit.jupiter.api.*;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import java.util.Arrays;
import java.util.List;
@DisplayName("Producer_Service_Test_Class")
public class ProducerServiceTest {
Logger logger = LoggerFactory.getLogger(this.getClass());
@InjectMocks
ProducerService producerService;
@Mock
ProducerServiceProxy mockProducerServiceProxy;
private final String topic = "trasactionRecordTopic";
private final boolean isReprocesed = true;
List<Offsets> mockOffsetsList;
TransactionPayload request;
TransactionRecorder transactionRecorder;
@BeforeEach
public void setUp() throws Exception {
logger.info("After Each Method Executed");
MockitoAnnotations.initMocks(this);
request = new TransactionPayload();
transactionRecorder = new TransactionRecorder();
}
@Test
public void testProduce(){
Mockito.when(mockProducerServiceProxy.produceTransactionService(Mockito.anyString(),Mockito.any())).thenReturn(mockConsumerPayloadResponse());
List<Offsets> offsetsList = producerService. produce(request, topic,transactionRecorder , isReprocesed);
Assertions.assertEquals(offsetsList,mockOffsetsList);
}
private ResponseEntity<ConsumerPayload> mockConsumerPayloadResponse(){
ConsumerPayload entity = new ConsumerPayload();
Offsets offsets = new Offsets();
offsets.setOffset("1");
offsets.setPartition("0");
mockOffsetsList = Arrays.asList(offsets);
entity.setOffsetsList(mockOffsetsList);
return ResponseEntity.ok(entity);
}
@AfterEach
public void tearDown(){
logger.info("After Each Method Executed");
}
}
...@@ -4,37 +4,40 @@ import com.safeway.epe.config.ConfigProps; ...@@ -4,37 +4,40 @@ import com.safeway.epe.config.ConfigProps;
import com.safeway.epe.domain.Offsets; import com.safeway.epe.domain.Offsets;
import com.safeway.epe.domain.TransactionRecorder; import com.safeway.epe.domain.TransactionRecorder;
import com.safeway.epe.repository.TransactionRepository; import com.safeway.epe.repository.TransactionRepository;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import java.util.*; import java.util.*;
@DisplayName("Transaction_Service_Impl_Test_Class")
public class TransactionServiceImplTest { public class TransactionServiceImplTest {
Logger logger = LoggerFactory.getLogger(this.getClass());
@InjectMocks @InjectMocks
TransactionServiceImpl transactionServiceImpl; TransactionServiceImpl transactionServiceImpl;
@Mock @Mock
private TransactionRepository mockTransactionRepository; private TransactionRepository mockTransactionRepository;
@Mock @Mock
private ProducerService testProducerService; private ProducerService mockProducerService;
@Mock @Mock
private ConfigProps configProps; private ConfigProps configProps;
@BeforeEach @BeforeEach
public void setUp() throws Exception { public void setUp() throws Exception {
logger.info("Before Each Method Executed");
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
} }
@Test @Test
@DisplayName("Produce Message to Kafka Topic") @DisplayName("Produce_Message_to_Kafka_Topic_Success_Scenario")
public void testProduceMessage(){ public void testProduceMessage(){
String transactionId = "997a9ed1-fb45-46d6-baee-0230f1da93dd"; String transactionId = "997a9ed1-fb45-46d6-baee-0230f1da93dd";
...@@ -48,6 +51,7 @@ public class TransactionServiceImplTest { ...@@ -48,6 +51,7 @@ public class TransactionServiceImplTest {
} }
@Test @Test
@DisplayName("Produce_Message_to_Kafka_Topic_Exception_Scenario")
public void testProduceMessage_Exception(){ public void testProduceMessage_Exception(){
String transactionId = "997a9ed1-fb45-46d6-baee-0230f1da93dd"; String transactionId = "997a9ed1-fb45-46d6-baee-0230f1da93dd";
...@@ -71,6 +75,10 @@ public class TransactionServiceImplTest { ...@@ -71,6 +75,10 @@ public class TransactionServiceImplTest {
return transactionRecorder; return transactionRecorder;
} }
@AfterEach
public void tearDown(){
logger.info("After Each Method Executed");
}
} }
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