Commit 8e5b72b4 authored by Simhadri Guntreddi's avatar Simhadri Guntreddi

Added Junit review comments

parent a36e15b3
......@@ -6,9 +6,15 @@ import java.util.List;
import java.util.Optional;
import java.util.UUID;
import com.safeway.epe.util.OffsetsArgumentsProvider;
import com.safeway.epe.util.TransactionServiceArgumentProvider;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.junit.jupiter.params.provider.ArgumentsSources;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -40,21 +46,16 @@ public class TransactionControllerTest {
private TransactionRepository transactionRepository;
private static String transactionId = "0ed78832-ea04-4062-b7dd-35c6c903c44c";
private List<Offsets> listOfOffest;
@Test
public void testProducer_Success() {
listOfOffest = new ArrayList<>();
Offsets offsets = new Offsets();
offsets.setPartition("0");
offsets.setOffset("1");
listOfOffest.add(offsets);
@ParameterizedTest
@ArgumentsSource(TransactionServiceArgumentProvider.class)
public void testProducer_Success(TransactionRecorder transactionRecorder) {
UUID uuid = UUID.fromString(transactionId);
Optional<TransactionRecorder> optionalOfferTransaction = Optional.of(createTransactionRecorder(uuid));
Optional<TransactionRecorder> optionalOfferTransaction = Optional.of(transactionRecorder);
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);
Mockito.any(TransactionRecorder.class), Mockito.anyBoolean())).thenReturn(offsetsResponse());
ResponseEntity<List<Offsets>> responseEntity = transactionController.produce(transactionId);
Assertions.assertEquals(responseEntity.getStatusCode(), HttpStatus.OK);
......@@ -69,16 +70,27 @@ public class TransactionControllerTest {
Assertions.assertEquals(responseEntity.getBody(), Arrays.asList(new Offsets()));
}
@Test
public void testProducer_WhenIsProcessed_False() {
listOfOffest = new ArrayList<>();
Offsets offsets = new Offsets();
offsets.setPartition("0");
offsets.setOffset("1");
listOfOffest.add(offsets);
@ParameterizedTest
@ArgumentsSource(TransactionServiceArgumentProvider.class)
public void testProducer_WhenIsProcessed_False(TransactionRecorder transactionRecorder) {
transactionRecorder.setProcessed(false);
UUID uuid = UUID.fromString(transactionId);
Optional<TransactionRecorder> optionalOfferTransaction = Optional.of((transactionRecorder));
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(offsetsResponse());
ResponseEntity<List<Offsets>> responseEntity = transactionController.produce(transactionId);
Assertions.assertEquals(responseEntity.getStatusCode(), HttpStatus.OK);
}
@Disabled
@ParameterizedTest
@ArgumentsSources({@ArgumentsSource(TransactionServiceArgumentProvider.class),@ArgumentsSource(OffsetsArgumentsProvider.class)})
public void testProducer_With_Two_ArgumentSources(TransactionRecorder transactionRecorder, List<Offsets> listOfOffest) {
transactionRecorder.setProcessed(false);
UUID uuid = UUID.fromString(transactionId);
Optional<TransactionRecorder> optionalOfferTransaction = Optional.of(createTransactionRecorderIsProcessedFalse(uuid));
Optional<TransactionRecorder> optionalOfferTransaction = Optional.of((transactionRecorder));
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);
......@@ -87,21 +99,13 @@ public class TransactionControllerTest {
Assertions.assertEquals(responseEntity.getStatusCode(), HttpStatus.OK);
}
private TransactionRecorder createTransactionRecorder(UUID uuid) {
TransactionRecorder transactionRecorder = new TransactionRecorder();
transactionRecorder.setUuid(uuid);
transactionRecorder.setProcessed(true);
transactionRecorder.setOffers("offers");
transactionRecorder.setOfferTransactionResponse("TransactionResponse");
return transactionRecorder;
}
private TransactionRecorder createTransactionRecorderIsProcessedFalse(UUID uuid) {
TransactionRecorder transactionRecorder = new TransactionRecorder();
transactionRecorder.setUuid(uuid);
transactionRecorder.setProcessed(false);
transactionRecorder.setOffers("offers");
transactionRecorder.setOfferTransactionResponse("TransactionResponse");
return transactionRecorder;
private List<Offsets> offsetsResponse() {
List<Offsets> listOfOffest = new ArrayList<>();
Offsets offsets = new Offsets();
offsets.setPartition("0");
offsets.setOffset("1");
listOfOffest.add(offsets);
return listOfOffest;
}
}
......@@ -4,7 +4,11 @@ import com.safeway.epe.config.ConfigProps;
import com.safeway.epe.domain.Offsets;
import com.safeway.epe.domain.TransactionRecorder;
import com.safeway.epe.repository.TransactionRepository;
import com.safeway.epe.util.TransactionServiceArgumentProvider;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ArgumentsSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
......@@ -36,14 +40,15 @@ public class TransactionServiceImplTest {
MockitoAnnotations.initMocks(this);
}
@Test
@ParameterizedTest
@ArgumentsSource(TransactionServiceArgumentProvider.class)
@DisplayName("Produce_Message_to_Kafka_Topic_Success_Scenario")
public void testProduceMessage(){
public void testProduceMessage(TransactionRecorder transactionRecorder){
String transactionId = "997a9ed1-fb45-46d6-baee-0230f1da93dd";
UUID uuid = UUID.fromString(transactionId);
Optional<TransactionRecorder> testTransactionRecord = Optional.of(createTransactionRecorder(uuid));
Optional<TransactionRecorder> testTransactionRecord = Optional.of(transactionRecorder);
Mockito.when(mockTransactionRepository.findById(uuid)).thenReturn(testTransactionRecord);
ResponseEntity<List<Offsets>> offsetsList = transactionServiceImpl.produceMessage(transactionId);
Assertions.assertEquals(offsetsList.getStatusCode(), HttpStatus.OK);
......@@ -65,16 +70,6 @@ public class TransactionServiceImplTest {
}
private TransactionRecorder createTransactionRecorder(UUID uuid) {
TransactionRecorder transactionRecorder = new TransactionRecorder();
transactionRecorder.setUuid(uuid);
transactionRecorder.setProcessed(true);
transactionRecorder.setOffers("offers");
transactionRecorder.setOfferTransactionResponse("OfferTransactionResponse");
return transactionRecorder;
}
@AfterEach
public void tearDown(){
logger.info("After Each Method Executed");
......
package com.safeway.epe.util;
import com.safeway.epe.domain.Offsets;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
public class OffsetsArgumentsProvider implements ArgumentsProvider {
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) throws Exception {
return Stream.of(offsetsResponse()).map(Arguments::of);
}
static List<Offsets> offsetsResponse() {
List<Offsets> listOfOffest = new ArrayList<>();
Offsets offsets = new Offsets();
offsets.setPartition("0");
offsets.setOffset("1");
listOfOffest.add(offsets);
return listOfOffest;
}
}
package com.safeway.epe.util;
import com.safeway.epe.domain.TransactionRecorder;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import java.util.UUID;
import java.util.stream.Stream;
public class TransactionServiceArgumentProvider implements ArgumentsProvider {
String transactionId = "0ed78832-ea04-4062-b7dd-35c6c903c44c";
UUID uuid = UUID.fromString(transactionId);
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) throws Exception {
return Stream.of(createTransactionRecorder(uuid)).map(Arguments::of);
}
static TransactionRecorder createTransactionRecorder(UUID uuid) {
TransactionRecorder transactionRecorder = new TransactionRecorder();
transactionRecorder.setUuid(uuid);
transactionRecorder.setProcessed(true);
transactionRecorder.setOffers("offers");
transactionRecorder.setOfferTransactionResponse("TransactionResponse");
return transactionRecorder;
}
}
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