Commit fe31b0f2 authored by Amar Bogari's avatar Amar Bogari

added component and integration test cases..

parent 4d78a549
package com.nisum.offertransactionservice.controller;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import com.github.tomakehurst.wiremock.junit.WireMockClassRule;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerList;
import com.nisum.offertransactionservice.OffertransactionserviceApplication;
import com.nisum.offertransactionservice.dto.*;
import com.nisum.offertransactionservice.model.OfferLookup;
import org.junit.ClassRule;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.*;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
import org.springframework.cloud.netflix.ribbon.StaticServerList;
import org.springframework.context.annotation.Bean;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import java.net.URI;
import java.util.ArrayList;
......@@ -26,60 +33,52 @@ import java.util.stream.Collectors;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.DEFINED_PORT)
@AutoConfigureWireMock
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.DEFINED_PORT,classes = OffertransactionserviceApplication.class)
@AutoConfigureWireMock(port = 0)
@ActiveProfiles(value = "integration")
@ContextConfiguration(classes = {OfferTransactionControllerIntegrationTest.LocalRibbonClientConfiguration.class})
public class OfferTransactionControllerIntegrationTest{
@ClassRule
public WireMockRule wireMockRule = new WireMockRule(9090);
public static WireMockClassRule mockClassRule = new WireMockClassRule(WireMockConfiguration.options().dynamicPort());
@Autowired
private TestRestTemplate testRestTemplate;
@BeforeEach
public void setUp() {
mockForOfferTransactionCall();
mockForEndOfSale();
mockForEndOfTransaction();
}
@AfterEach
public void setWireMockRule(){
wireMockRule.stop();
@BeforeAll
public static void beforeAll(){
mockClassRule.start();
}
private void mockForOfferTransactionCall() {
wireMockRule.start();
wireMockRule.stubFor(post(urlEqualTo("/promotionEngine/calculateDiscount"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBodyFile("response.json")
));
@AfterAll
public static void afterAll() {
mockClassRule.stop();
}
private void mockForEndOfSale() {
wireMockRule.start();
wireMockRule.stubFor(post(urlEqualTo("/endOfSale"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBodyFile("endOfSale-response.json")
));
@TestConfiguration
public static class LocalRibbonClientConfiguration {
@Bean
public ServerList<Server> ribbonServerList() {
return new StaticServerList<>(new Server("localhost", mockClassRule.port()));
}
}
private void mockForEndOfTransaction() {
wireMockRule.start();
wireMockRule.stubFor(post(urlEqualTo("/endOfTransaction"))
private void mockForOfferTransactionCall() {
mockClassRule.stubFor(post(urlEqualTo("/promotionEngine/calculateDiscount"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBodyFile("endOfTransaction-response.json")
.withBodyFile("response.json")
));
}
@Test
public void getOfferTransactionResponseTest() {
mockForOfferTransactionCall();
URI uri = URI.create("http://localhost:7072/offer-service/offerTransactionCall");
RequestEntity requestEntity = new RequestEntity(getOfferTransactionRequest(),HttpMethod.POST,uri);
......@@ -87,17 +86,17 @@ public class OfferTransactionControllerIntegrationTest{
new ParameterizedTypeReference<OfferTransactionResponse>(){}
);
OfferTransactionResponse offerTransactionResponse = responseEntity.getBody();
System.out.println(offerTransactionResponse);
Assertions.assertNotNull(offerTransactionResponse.getTransactionId());
Assertions.assertNotNull(offerTransactionResponse.getTransactionId());
Assertions.assertEquals(2,offerTransactionResponse.getDiscountedItemList().size());
}
@Test
public void endOfSaleTest() {
URI uri = URI.create("http://localhost:7072/endOfSale");
URI uri = URI.create("http://localhost:7072/offer-service/endOfSale");
RequestEntity requestEntity = new RequestEntity(getEndOfSaleReq(),HttpMethod.POST,uri);
ResponseEntity<EndOfSaleResponse> responseEntity = testRestTemplate.exchange("http://localhost:7072/endOfSale", HttpMethod.POST,requestEntity,
ResponseEntity<EndOfSaleResponse> responseEntity = testRestTemplate.exchange("http://localhost:7072/offer-service/endOfSale", HttpMethod.POST,requestEntity,
new ParameterizedTypeReference<EndOfSaleResponse>(){}
);
EndOfSaleResponse endOfSaleResponse = responseEntity.getBody();
......@@ -105,17 +104,27 @@ public class OfferTransactionControllerIntegrationTest{
}
private void mockForEndOfTransaction() {
mockClassRule.stubFor(post(urlEqualTo("/store/producer"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBody("Success from Producer")
));
}
@Test
public void endOfTransactionTest() {
URI uri = URI.create("http://localhost:7072/endOfTransaction");
mockForEndOfTransaction();
URI uri = URI.create("http://localhost:7072/offer-service/endOfTransaction");
String uuid = "86fd4146-0540-405b-b621-a95f4ccdfa0d";
RequestEntity requestEntity = new RequestEntity(uuid,HttpMethod.POST,uri);
ResponseEntity<String> responseEntity = testRestTemplate.exchange("http://localhost:7072/endOfTransaction", HttpMethod.POST,requestEntity,
ResponseEntity<String> responseEntity = testRestTemplate.exchange("http://localhost:7072/offer-service/endOfTransaction", HttpMethod.POST,requestEntity,
new ParameterizedTypeReference<String>(){}
);
String endOfTransactionRes = responseEntity.getBody();
Assertions.assertNotNull(endOfTransactionRes);
Assertions.assertEquals("Success from Producer",endOfTransactionRes);
}
......
......@@ -13,16 +13,17 @@ import com.nisum.offertransactionservice.service.OfferCallingPEService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentMatchers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.junit.runner.RunWith;
import org.mockito.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Description;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import reactor.core.publisher.Mono;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.IOException;
import java.time.LocalDateTime;
......@@ -32,27 +33,31 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;
@SpringBootTest
@RunWith(SpringRunner.class)
class OfferTransactionControllerTest {
@InjectMocks
@Autowired
private OfferTransactionController offerTransactionController;
@Mock
@MockBean
private OfferLookupRepo offerLookupRepo;
@Mock
@MockBean
private OfferMetaDataRepo offerMetaDataRepo;
@Mock
@MockBean
private EndOfSaleRepo endOfSaleRepo;
@Mock
@MockBean
private PromotionEngineFeignClient clientService;
@Mock
@MockBean
private StoreProducerFeignClient storeProducerFeignClient;
@Autowired
private OfferCallingPEService offerCallingPEService;
private String HH_ID_FOR_END_OF_SALE = "id01";
private String TRANSACTION_ID_FOR_END_OF_SALE = "tx01";
......@@ -61,26 +66,22 @@ class OfferTransactionControllerTest {
private String DISCOUNT_DESC = "10 percent";
@Mock
private OfferCallingPEService offerCallingPEService;
@Description("Component test for getOfferTransactionResponse method")
@Test
void getOfferTransactionResponse() throws InterruptedException {
when(offerLookupRepo.findByHhId(ArgumentMatchers.any(),ArgumentMatchers.any())).thenReturn(getOffers());
void getOfferTransactionResponse() throws InterruptedException, NoSuchFieldException {
when(offerLookupRepo.findByHhId(ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(getOffers());
when(clientService.callPEService(Mockito.any(PERequest.class))).thenReturn(getPeResponse());
when(offerCallingPEService.getOfferMetaDto(ArgumentMatchers.any())).thenReturn(getOfferLookupDto());
Mockito.doReturn(Optional.of(getOfferMeta())).when(offerMetaDataRepo).findByOfferIdAndOfferStatusIgnoreCase(Mockito.anyLong(), Mockito.anyString());
OfferTransactionResponse offerTransactionResponse = offerTransactionController.getOfferTransactionResponse(getOfferTransactionRequest());
assertEquals(true,Objects.nonNull(offerTransactionResponse.getTransactionId()));
assertEquals(true, Objects.nonNull(offerTransactionResponse.getTransactionId()));
}
@Description("Component test for endOfSale method")
@Test
void endOfSale() throws IOException {
when(endOfSaleRepo.save(Mockito.any(EndOfSaleEntity.class))).thenReturn(new EndOfSaleEntity());
assertEquals(true,Objects.nonNull(offerTransactionController.endOfSale(buildEndOfSaleReq())));
assertEquals(true, Objects.nonNull(offerTransactionController.endOfSale(buildEndOfSaleReq())));
}
@Description("Component test for endOfTransaction method")
......@@ -88,7 +89,7 @@ class OfferTransactionControllerTest {
@ValueSource(strings = {UU_ID_FOR_END_OF_TRANSACTION})
void endOfTransaction(String uuId) {
when(storeProducerFeignClient.callStoreProducer(Mockito.anyString())).thenReturn(getResponseEntityString());
assertEquals(true,Objects.nonNull(offerTransactionController.endOfTransaction(uuId)));
assertEquals(true, Objects.nonNull(offerTransactionController.endOfTransaction(uuId)));
}
private List<OfferLookup> getOffers() {
......@@ -111,11 +112,11 @@ class OfferTransactionControllerTest {
PEResponse peResponse = new PEResponse();
peResponse.setHhid(123L);
peResponse.setDiscountedItemList(getItemList());
return new ResponseEntity<PEResponse>(peResponse,HttpStatus.ACCEPTED);
return new ResponseEntity<PEResponse>(peResponse, HttpStatus.ACCEPTED);
}
private EndOfSaleReq buildEndOfSaleReq() {
OfferTransactionResponse offerTransactionResponse = new OfferTransactionResponse(HH_ID_FOR_END_OF_SALE, TRANSACTION_ID_FOR_END_OF_SALE, getItemList(),DISCOUNT_DESC);
OfferTransactionResponse offerTransactionResponse = new OfferTransactionResponse(HH_ID_FOR_END_OF_SALE, TRANSACTION_ID_FOR_END_OF_SALE, getItemList(), DISCOUNT_DESC);
EndOfSaleReq endOfSaleReq = new EndOfSaleReq(offerTransactionResponse, getOffers());
return endOfSaleReq;
}
......@@ -128,17 +129,18 @@ class OfferTransactionControllerTest {
itemList.add(item2);
return itemList;
}
private ResponseEntity<String> getResponseEntityString() {
return new ResponseEntity<String> ("STRING",HttpStatus.ACCEPTED);
return new ResponseEntity<String>("STRING", HttpStatus.ACCEPTED);
}
private OfferMeta getOfferMeta(){
private OfferMeta getOfferMeta() {
OfferMeta offerMetaDTO = new OfferMeta();
offerMetaDTO.setDiscount("discount");
offerMetaDTO.setEligibility("eligibility");
offerMetaDTO.setExpiryTime(LocalDateTime.of(2021,01,01,01,01));
offerMetaDTO.setStartTime(LocalDateTime.of(2020,01,01,01,01));
offerMetaDTO.setExpiryTime(LocalDateTime.of(2021, 01, 01, 01, 01));
offerMetaDTO.setStartTime(LocalDateTime.of(2020, 01, 01, 01, 01));
offerMetaDTO.setOfferDesc("30 percent");
offerMetaDTO.setOfferId(1L);
offerMetaDTO.setOfferStatus("Active");
......@@ -148,7 +150,7 @@ class OfferTransactionControllerTest {
}
private List<OfferLookupDTO> getOfferLookupDto(){
private List<OfferLookupDTO> getOfferLookupDto() {
OfferLookupDTO offerLookupDTO = new OfferLookupDTO();
offerLookupDTO.setId("123");
offerLookupDTO.setIdType("1234");
......@@ -160,8 +162,8 @@ class OfferTransactionControllerTest {
OfferMetaDTO offerMetaDTO = new OfferMetaDTO();
offerMetaDTO.setDiscount("discount");
offerMetaDTO.setEligibility("eligibility");
offerMetaDTO.setExpiryTime(LocalDateTime.of(2021,01,01,01,01));
offerMetaDTO.setStartTime(LocalDateTime.of(2020,01,01,01,01));
offerMetaDTO.setExpiryTime(LocalDateTime.of(2021, 01, 01, 01, 01));
offerMetaDTO.setStartTime(LocalDateTime.of(2020, 01, 01, 01, 01));
offerMetaDTO.setOfferDesc("30 percent");
offerMetaDTO.setOfferId(1L);
offerMetaDTO.setOfferStatus("Active");
......@@ -182,8 +184,8 @@ class OfferTransactionControllerTest {
OfferMetaDTO offerMetaDTO1 = new OfferMetaDTO();
offerMetaDTO1.setDiscount("discount");
offerMetaDTO1.setEligibility("eligibility");
offerMetaDTO1.setExpiryTime(LocalDateTime.of(2021,01,01,01,01));
offerMetaDTO1.setStartTime(LocalDateTime.of(2020,01,01,01,01));
offerMetaDTO1.setExpiryTime(LocalDateTime.of(2021, 01, 01, 01, 01));
offerMetaDTO1.setStartTime(LocalDateTime.of(2020, 01, 01, 01, 01));
offerMetaDTO1.setOfferDesc("20 percent");
offerMetaDTO1.setOfferId(1L);
offerMetaDTO1.setOfferStatus("Active");
......
server.port = 7072
spring.application.name=ots
# PE Application properties
pe.application.name=pe
pe.application.url=http://localhost:9090
endpoint.url.promotionEngineUrl=/promotionEngine/calculateDiscount
# Store Producer Application properties
storeproducer.application.name=storeproducer
endpoint.url.storeProducerUrl=/store/producer
#Eureka server url
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
endpoints.restart.enabled=true
endpoints.shutdown.enabled=true
endpoints.health.sensitive=false
maxattempts=3
backoff=2000
eureka.client.enabled=false
\ No newline at end of file
server.port = 7072
spring.application.name=ots
# PE Application properties
pe.application.name=pe
pe.application.url=http://localhost:9090
endpoint.url.promotionEngineUrl=/promotionEngine/calculateDiscount
# Store Producer Application properties
storeproducer.application.name=storeproducer
endpoint.url.storeProducerUrl=/store/producer
#Eureka server url
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
endpoints.restart.enabled=true
endpoints.shutdown.enabled=true
endpoints.health.sensitive=false
maxattempts=3
backoff=2000
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