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

Added Junit review comments

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