Commit fe31b0f2 authored by Amar Bogari's avatar Amar Bogari

added component and integration test cases..

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