Commit f68a108e authored by Simhadri Guntreddi's avatar Simhadri Guntreddi

Merge branch 'Feign_Client_Branch' into 'master'

Feign client and test cases branch

See merge request !2
parents 6ddb2d93 ca2521e1
File added
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
import com.commercehub.gradle.plugin.avro.GenerateAvroJavaTask
plugins { plugins {
id 'org.springframework.boot' version '2.2.6.RELEASE' id 'org.springframework.boot' version '2.2.6.RELEASE'
id 'io.spring.dependency-management' version '1.0.9.RELEASE' id 'io.spring.dependency-management' version '1.0.9.RELEASE'
id 'java' id 'java'
id 'com.commercehub.gradle.plugin.avro' version '0.9.1'
} }
group = 'com.safeway.epe' group = 'com.safeway.epe'
...@@ -20,24 +18,20 @@ configurations { ...@@ -20,24 +18,20 @@ configurations {
} }
} }
ext {
set('springCloudVersion', "Hoxton.SR4")
}
repositories { repositories {
gradlePluginPortal()
mavenCentral() mavenCentral()
maven { url "http://packages.confluent.io/maven/" }
maven { url "https://plugins.gradle.org/m2/"}
} }
dependencies { dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.mapstruct:mapstruct:1.3.1.Final' implementation 'org.mapstruct:mapstruct:1.3.1.Final'
implementation 'io.confluent:kafka-avro-serializer:5.4.0'
compile (group: 'io.confluent', name: 'kafka-schema-registry', version: '5.4.0'){
exclude group:'org.slf4j', module: 'slf4j-log4j12'
}
compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.30' compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.30'
compile "org.apache.avro:avro:1.9.1"
compile group: 'org.apache.kafka', name: 'kafka-clients', version: '5.4.1-ccs'
compileOnly 'org.projectlombok:lombok' compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools' developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'org.postgresql:postgresql' runtimeOnly 'org.postgresql:postgresql'
...@@ -49,24 +43,14 @@ dependencies { ...@@ -49,24 +43,14 @@ dependencies {
} }
} }
test {
useJUnitPlatform()
}
avro {
createSetters = false
fieldVisibility = "PRIVATE"
stringType = "String"
outputCharacterEncoding = "UTF-8"
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
} }
task generateAvro(type: GenerateAvroJavaTask) { test {
source("src/main/resources/avro/") useJUnitPlatform()
outputDir = file("build/generated/java/main")
} }
sourceSets {
main {
java.srcDirs += generateAvro.outputs
}
}
#Thu May 07 17:07:35 IST 2020
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-bin.zip
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip
File added
...@@ -2,8 +2,10 @@ package com.safeway.epe; ...@@ -2,8 +2,10 @@ package com.safeway.epe;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication @SpringBootApplication
@EnableFeignClients("com.safeway.epe.*")
public class StoreProducerApplication { public class StoreProducerApplication {
public static void main(String[] args) { public static void main(String[] args) {
......
package com.safeway.epe.config;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Data
@Configuration
@ConfigurationProperties(prefix = "kafka.producer")
public class ConfigProps {
private String offerTransactionRecordTopic;
@Value("${schema.api.baseurl}")
private String schemaBaseUrl;
}
package com.safeway.epe.config;
import com.safeway.epe.domain.ConsumerPayload;
import com.safeway.epe.domain.TransactionPayload;
import feign.Headers;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
@FeignClient(name = "service", url = "${schema.api.baseurl}")
@Headers({"User-Agent:Producer","Accept:application/vnd.kafka.v2+json"})
public interface ProducerServiceProxy
{
@PostMapping(value="topics/{topic}", consumes = "application/vnd.kafka.json.v2+json")
ResponseEntity<ConsumerPayload> produceTransactionService(@PathVariable("topic") String topic,
@RequestBody TransactionPayload transactionPayload);
}
...@@ -17,7 +17,7 @@ public class TransactionController ...@@ -17,7 +17,7 @@ public class TransactionController
private TransactionService service; private TransactionService service;
@GetMapping("transaction/{uuid}") @GetMapping("transaction/{uuid}")
public ResponseEntity<List<Offsets>> getTransaction(@PathVariable("uuid") String uuid) public ResponseEntity<List<Offsets>> produce(@PathVariable("uuid") String uuid)
{ {
return service.produceMessage(uuid); return service.produceMessage(uuid);
} }
......
...@@ -33,16 +33,10 @@ public class TransactionRecorder ...@@ -33,16 +33,10 @@ public class TransactionRecorder
@Column(name = "uuid") @Column(name = "uuid")
@JsonProperty("uuid") @JsonProperty("uuid")
UUID uuid; UUID uuid;
// @Type(type = "jsonb")
// @JsonProperty("offertransactionresponse")
@Column(name = "offertransactionresponse") @Column(name = "offertransactionresponse")
String offerTransactionResponse; String offerTransactionResponse;
// @Type(type="jsonb")
// @JsonProperty("offers")
@Column(name="offers") @Column(name="offers")
String offers; String offers;
@Column(name="isprocessed") @Column(name="isprocessed")
boolean isProcessed; boolean isProcessed;
} }
package com.safeway.epe.service; package com.safeway.epe.service;
import com.safeway.epe.config.ProducerServiceProxy;
import com.safeway.epe.domain.ConsumerPayload; import com.safeway.epe.domain.ConsumerPayload;
import com.safeway.epe.domain.Offsets; import com.safeway.epe.domain.Offsets;
import com.safeway.epe.domain.TransactionPayload; import com.safeway.epe.domain.TransactionPayload;
...@@ -9,7 +10,6 @@ import org.slf4j.LoggerFactory; ...@@ -9,7 +10,6 @@ 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.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.util.List; import java.util.List;
@Component @Component
...@@ -21,13 +21,12 @@ public class ProducerService ...@@ -21,13 +21,12 @@ public class ProducerService
private TransactionService transactionService; private TransactionService transactionService;
@Autowired @Autowired
private RestTemplate restTemplate; private ProducerServiceProxy producerServiceProxy;
public List<Offsets> produce(TransactionPayload request, String topic, TransactionRecorder transactionRecorder, boolean isReprocesed) public List<Offsets> produce(TransactionPayload request, String topic, TransactionRecorder transactionRecorder, boolean isReprocesed)
{ {
String topicUri = "/topics/".concat(topic); ResponseEntity<ConsumerPayload> response = producerServiceProxy.produceTransactionService(topic,request);
ResponseEntity<ConsumerPayload> response = restTemplate.postForEntity(topicUri, request, ConsumerPayload.class); logger.info("response ::" + response);
logger.info("response ::response ::" + response);
if(response != null && (response.getStatusCodeValue() == 200 || response.getStatusCode().is2xxSuccessful())) if(response != null && (response.getStatusCodeValue() == 200 || response.getStatusCode().is2xxSuccessful()))
{ {
......
...@@ -9,5 +9,5 @@ import java.util.List; ...@@ -9,5 +9,5 @@ import java.util.List;
public interface TransactionService public interface TransactionService
{ {
ResponseEntity<List<Offsets>> produceMessage(String uuid); ResponseEntity<List<Offsets>> produceMessage(String uuid);
ResponseEntity<String> errorResponse(TransactionRecorder transactionRecorder, java.lang.String exceptionMessage, boolean isReprocesed); ResponseEntity<String> errorResponse(TransactionRecorder transactionRecorder, String exceptionMessage, boolean isReprocesed);
} }
package com.safeway.epe.service; package com.safeway.epe.service;
import com.safeway.epe.Config.ConfigProps; import com.safeway.epe.config.ConfigProps;
import com.safeway.epe.domain.Offsets; import com.safeway.epe.domain.Offsets;
import com.safeway.epe.domain.Record; import com.safeway.epe.domain.Record;
import com.safeway.epe.domain.TransactionPayload; import com.safeway.epe.domain.TransactionPayload;
......
{
"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.Test;
import org.springframework.boot.test.context.SpringBootTest; //import org.springframework.boot.test.context.SpringBootTest;
//
@SpringBootTest //@SpringBootTest
class StoreProducerApplicationTests { //class StoreProducerApplicationTests {
//
@Test // @Test
void contextLoads() { // void contextLoads() {
} // }
//
} //}
package com.safeway.epe.controller;
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;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.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.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
import java.util.*;
@SpringBootTest
public class TransactionControllerTest {
@Autowired
private TransactionController transactionController;
@MockBean
private ProducerService producerService;
@MockBean
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);
UUID uuid = UUID.fromString(transactionId);
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);
ResponseEntity<List<Offsets>> responseEntity = transactionController.produce(transactionId);
Assertions.assertEquals(responseEntity.getStatusCode(), HttpStatus.OK);
}
@Test
public void testProducer_Failure() {
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) {
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.ConfigProps;
import com.safeway.epe.domain.Offsets;
import com.safeway.epe.domain.TransactionRecorder;
import com.safeway.epe.repository.TransactionRepository;
import org.junit.jupiter.api.Assertions;
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.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import java.util.*;
public class TransactionServiceImplTest {
@InjectMocks
TransactionServiceImpl transactionServiceImpl;
@Mock
private TransactionRepository mockTransactionRepository;
@Mock
private ProducerService testProducerService;
@Mock
private ConfigProps configProps;
@BeforeEach
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
}
@Test
@DisplayName("Produce Message to Kafka Topic")
public void testProduceMessage(){
String transactionId = "997a9ed1-fb45-46d6-baee-0230f1da93dd";
UUID uuid = UUID.fromString(transactionId);
Optional<TransactionRecorder> testTransactionRecord = Optional.of(createTransactionRecorder(uuid));
Mockito.when(mockTransactionRepository.findById(uuid)).thenReturn(testTransactionRecord);
ResponseEntity<List<Offsets>> offsetsList = transactionServiceImpl.produceMessage(transactionId);
Assertions.assertEquals(offsetsList.getStatusCode(), HttpStatus.OK);
}
@Test
public void testProduceMessage_Exception(){
String transactionId = "997a9ed1-fb45-46d6-baee-0230f1da93dd";
UUID uuid = UUID.fromString(transactionId);
Optional<TransactionRecorder> testTransactionRecord = Optional.empty();
Mockito.when(mockTransactionRepository.findById(uuid)).thenReturn(testTransactionRecord);
ResponseEntity<List<Offsets>> responseOffsetsList = transactionServiceImpl.produceMessage(transactionId);
Assertions.assertEquals(responseOffsetsList.getStatusCode(), HttpStatus.NOT_FOUND);
Assertions.assertEquals(responseOffsetsList.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("OfferTransactionResponse");
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