Initial commit

This commit is contained in:
2020-11-30 04:23:20 -05:00
parent ba76ff2463
commit 82669ccb12
29 changed files with 1050 additions and 2 deletions

View File

@@ -0,0 +1,13 @@
package com.barrelsofdata.springexamples;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = "com.barrelsofdata.springexamples")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}

View File

@@ -0,0 +1,6 @@
package com.barrelsofdata.springexamples.constants;
public enum EventType {
LEFT_MOUSE_BUTTON_CLICK,
RIGHT_MOUSE_BUTTON_CLICK
}

View File

@@ -0,0 +1,45 @@
package com.barrelsofdata.springexamples.controller;
import com.barrelsofdata.springexamples.dto.ExceptionDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.error.ErrorAttributeOptions;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.WebRequest;
import javax.servlet.RequestDispatcher;
import java.util.Date;
import java.util.Map;
@Controller
public class ApplicationErrorController implements ErrorController {
@Autowired
private ErrorAttributes errorAttributes;
@RequestMapping("/error")
public ResponseEntity<ExceptionDto> handleError(WebRequest request) {
Map<String, Object> requestErrors = errorAttributes.getErrorAttributes(request, ErrorAttributeOptions.defaults());
Object statusObject = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE, RequestAttributes.SCOPE_REQUEST);
StringBuilder errorMessage = new StringBuilder();
ExceptionDto.ExceptionDtoBuilder exceptionBuilder = ExceptionDto.builder();
if(requestErrors.containsKey("timestamp")) exceptionBuilder.timestamp((Date) requestErrors.get("timestamp"));
if(requestErrors.containsKey("error")) errorMessage.append(requestErrors.get("error"));
if(requestErrors.containsKey("message")) errorMessage.append((String) requestErrors.get("message"));
exceptionBuilder.error(errorMessage.toString());
HttpStatus status = statusObject != null ? HttpStatus.resolve(Integer.parseInt(statusObject.toString())) : HttpStatus.INTERNAL_SERVER_ERROR;
ExceptionDto exception = exceptionBuilder.build();
return new ResponseEntity<>(exception, status);
}
@Override
@SuppressWarnings( "deprecation" )
public String getErrorPath() {
return "/error";
}
}

View File

@@ -0,0 +1,30 @@
package com.barrelsofdata.springexamples.controller;
import com.barrelsofdata.springexamples.dto.EventRequestDto;
import com.barrelsofdata.springexamples.service.TelemetryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
@RestController
public class TelemetryController {
@Autowired private TelemetryService telemetryService;
@PutMapping(
value = "/telemetry",
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE
)
public ResponseEntity<String> receiveTelemetry(@RequestBody @Valid EventRequestDto eventRequest) {
telemetryService.receiveTelemetry(eventRequest);
return new ResponseEntity<>(HttpStatus.CREATED.getReasonPhrase(), HttpStatus.CREATED);
}
}

View File

@@ -0,0 +1,21 @@
package com.barrelsofdata.springexamples.dto;
import com.fasterxml.jackson.annotation.JsonAlias;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@ToString
@Setter
@Getter
public class EventDetailsDto {
@JsonAlias("w")
private Integer width;
@JsonAlias("h")
private Integer height;
@JsonAlias("x")
private Float x;
@JsonAlias("y")
private Float y;
}

View File

@@ -0,0 +1,28 @@
package com.barrelsofdata.springexamples.dto;
import com.barrelsofdata.springexamples.constants.EventType;
import com.fasterxml.jackson.annotation.JsonAlias;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import javax.validation.constraints.NotNull;
import java.sql.Timestamp;
@ToString
@Setter
@Getter
public class EventRequestDto {
@NotNull
@JsonAlias("ts")
private Timestamp timestamp;
@NotNull
@JsonAlias("id")
private Integer id;
@NotNull
@JsonAlias("ty")
private EventType type;
@JsonAlias("pl")
private EventDetailsDto payload;
}

View File

@@ -0,0 +1,14 @@
package com.barrelsofdata.springexamples.dto;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Builder;
import java.util.Date;
@Builder
public class ExceptionDto {
@JsonProperty("timestamp")
private Date timestamp;
@JsonProperty("error")
private String error;
}

View File

@@ -0,0 +1,17 @@
package com.barrelsofdata.springexamples.exception;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(code = HttpStatus.BAD_REQUEST)
public class JsonConversionException extends RuntimeException {
public JsonConversionException() {
super();
}
public JsonConversionException(String message) {
super(message);
}
public JsonConversionException(String message, Exception e) {
super(message, e);
}
}

View File

@@ -0,0 +1,7 @@
package com.barrelsofdata.springexamples.producer;
import org.springframework.kafka.KafkaException;
public interface Kafka {
void publish(String eventRequest) throws KafkaException;
}

View File

@@ -0,0 +1,36 @@
package com.barrelsofdata.springexamples.producer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.kafka.KafkaException;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.ListenableFutureCallback;
@Component
public class KafkaImpl implements Kafka {
private static final Logger logger = LoggerFactory.getLogger(Kafka.class);
@Autowired private KafkaTemplate<String, String> kafkaTemplate;
@Value("${spring.kafka.producer.topic}")
private String topic;
public void publish(String payload) throws KafkaException {
ListenableFuture<SendResult<String, String>> future = kafkaTemplate.send(topic, payload); // Blocks call if kafka broker isn't available/responding
future.addCallback(new ListenableFutureCallback<SendResult<String, String>>() {
@Override
public void onSuccess(SendResult<String, String> result) {
logger.info("Message published to Kafka partition {} with offset {}", result.getRecordMetadata().partition(), result.getRecordMetadata().offset());
}
@Override
public void onFailure(Throwable ex) {
logger.error("Unable to publish message {}", payload, ex);
}
});
}
}

View File

@@ -0,0 +1,7 @@
package com.barrelsofdata.springexamples.service;
import com.barrelsofdata.springexamples.dto.EventRequestDto;
public interface TelemetryService {
void receiveTelemetry(EventRequestDto eventRequest);
}

View File

@@ -0,0 +1,35 @@
package com.barrelsofdata.springexamples.service;
import com.barrelsofdata.springexamples.dto.EventRequestDto;
import com.barrelsofdata.springexamples.exception.JsonConversionException;
import com.barrelsofdata.springexamples.producer.Kafka;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.KafkaException;
import org.springframework.stereotype.Service;
@Service
public class TelemetryServiceImpl implements TelemetryService {
private static final Logger logger = LoggerFactory.getLogger(TelemetryService.class);
@Autowired private Kafka producer;
@Autowired private ObjectMapper jsonMapper;
@Override
public void receiveTelemetry(EventRequestDto eventRequest) {
try {
String payload = jsonMapper.writeValueAsString(eventRequest);
producer.publish(payload);
} catch (JsonProcessingException e) {
logger.error("Unable to convert message to json {}", eventRequest);
throw new JsonConversionException("Failed json conversion");
} catch (KafkaException e) {
logger.error("Kafka exception for request {}", eventRequest);
// TODO: Handle what you want to do with the data here
}
}
}

View File

@@ -0,0 +1,138 @@
package com.barrelsofdata.springexamples;
import com.barrelsofdata.springexamples.service.TelemetryService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.common.serialization.StringDeserializer;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.kafka.core.DefaultKafkaConsumerFactory;
import org.springframework.kafka.listener.ContainerProperties;
import org.springframework.kafka.listener.KafkaMessageListenerContainer;
import org.springframework.kafka.listener.MessageListener;
import org.springframework.kafka.test.EmbeddedKafkaBroker;
import org.springframework.kafka.test.context.EmbeddedKafka;
import org.springframework.kafka.test.utils.ContainerTestUtils;
import org.springframework.kafka.test.utils.KafkaTestUtils;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@ExtendWith(SpringExtension.class)
@EmbeddedKafka
@AutoConfigureMockMvc
public class ApplicationIntegrationTest {
private int NUMBER_OF_BROKERS = 2;
private boolean CONTROLLER_SHUTDOWN = false;
private int NUMBER_OF_PARTITIONS = 2;
@Value("${spring.kafka.producer.topic}")
private String TOPIC;
@Autowired private TelemetryService telemetryService;
@Autowired private MockMvc mockMvc;
@Autowired
private EmbeddedKafkaBroker embeddedKafkaBroker = new EmbeddedKafkaBroker(NUMBER_OF_BROKERS, CONTROLLER_SHUTDOWN, NUMBER_OF_PARTITIONS, TOPIC);
private BlockingQueue<ConsumerRecord<String, String>> records;
private KafkaMessageListenerContainer<String, String> container;
@BeforeEach
void setUp() {
Map<String, Object> consumerConfigs = new HashMap<>(KafkaTestUtils.consumerProps("consumer", "false", embeddedKafkaBroker));
DefaultKafkaConsumerFactory<String, String> consumerFactory = new DefaultKafkaConsumerFactory<>(consumerConfigs, new StringDeserializer(), new StringDeserializer());
ContainerProperties containerProperties = new ContainerProperties(TOPIC);
container = new KafkaMessageListenerContainer<>(consumerFactory, containerProperties);
records = new LinkedBlockingQueue<>();
container.setupMessageListener((MessageListener<String, String>) records::add);
container.start();
ContainerTestUtils.waitForAssignment(container, embeddedKafkaBroker.getPartitionsPerTopic());
}
@AfterEach
void tearDown() {
container.stop();
}
@ParameterizedTest(name = "Integration: API request success")
@CsvSource(value = {
"{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"LEFT_MOUSE_BUTTON_CLICK\",\"pl\":{\"x\":1000,\"y\":5000,\"w\":213,\"h\":124}};{\"timestamp\":\"2020-11-25T09:53:14.000+00:00\",\"id\":123,\"type\":\"LEFT_MOUSE_BUTTON_CLICK\",\"payload\":{\"width\":213,\"height\":124,\"x\":1000.0,\"y\":5000.0}}",
"{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"RIGHT_MOUSE_BUTTON_CLICK\"};{\"timestamp\":\"2020-11-25T09:53:14.000+00:00\",\"id\":123,\"type\":\"RIGHT_MOUSE_BUTTON_CLICK\",\"payload\":null}"}
, delimiter = ';')
public void success(String inputJson, String kafkaJson) throws Exception {
HttpHeaders headers = new HttpHeaders();
mockMvc.perform(
put("/telemetry")
.contentType(MediaType.APPLICATION_JSON)
.content(inputJson)
.headers(headers))
.andExpect(
status().isCreated()
)
.andExpect(
content().contentType(MediaType.APPLICATION_JSON)
)
.andExpect(
content().string(HttpStatus.CREATED.getReasonPhrase())
);
Thread.sleep(1000);
ConsumerRecord<String, String> singleRecord = records.poll(100, TimeUnit.MILLISECONDS);
Assertions.assertThat(singleRecord).isNotNull();
Assertions.assertThat(singleRecord.key()).isNull();
Assertions.assertThat(singleRecord.value()).isEqualTo(kafkaJson);
}
@ParameterizedTest(name = "Integration: API request success")
@CsvSource(value = {
"{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"LEFT_MOUSE_BUTTON_CLICK\",\"pl\":{\"x\":1000,\"y\":5000,\"w\":213,\"h\":124}};{\"timestamp\":\"2020-11-25T09:53:14.000+00:00\",\"id\":123,\"type\":\"LEFT_MOUSE_BUTTON_CLICK\",\"payload\":{\"width\":213,\"height\":124,\"x\":1000.0,\"y\":5000.0}}",
"{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"RIGHT_MOUSE_BUTTON_CLICK\"};{\"timestamp\":\"2020-11-25T09:53:14.000+00:00\",\"id\":123,\"type\":\"RIGHT_MOUSE_BUTTON_CLICK\",\"payload\":null}"}
, delimiter = ';')
public void unsupportedMedia(String inputJson, String kafkaJson) throws Exception {
HttpHeaders headers = new HttpHeaders();
mockMvc.perform(
put("/telemetry")
.contentType(MediaType.APPLICATION_JSON)
.content(inputJson)
.headers(headers))
.andExpect(
status().isCreated()
)
.andExpect(
content().contentType(MediaType.APPLICATION_JSON)
)
.andExpect(
content().string(HttpStatus.CREATED.getReasonPhrase())
);
Thread.sleep(1000);
ConsumerRecord<String, String> singleRecord = records.poll(100, TimeUnit.MILLISECONDS);
Assertions.assertThat(singleRecord).isNotNull();
Assertions.assertThat(singleRecord.key()).isNull();
Assertions.assertThat(singleRecord.value()).isEqualTo(kafkaJson);
}
}

View File

@@ -0,0 +1,121 @@
package com.barrelsofdata.springexamples.controller;
import com.barrelsofdata.springexamples.dto.EventRequestDto;
import com.barrelsofdata.springexamples.exception.JsonConversionException;
import com.barrelsofdata.springexamples.producer.Kafka;
import com.barrelsofdata.springexamples.service.TelemetryService;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.test.web.servlet.MockMvc;
import static org.mockito.ArgumentMatchers.any;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@ExtendWith(SpringExtension.class)
@AutoConfigureMockMvc
public class TelemetryControllerTest {
@MockBean private TelemetryService telemetryService;
@MockBean private Kafka kafka;
@Autowired private MockMvc mockMvc;
@ParameterizedTest(name = "Success API request")
@ValueSource(strings = {"{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"LEFT_MOUSE_BUTTON_CLICK\",\"pl\":{\"x\":1000,\"y\":5000,\"w\":213,\"h\":124}}","{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"RIGHT_MOUSE_BUTTON_CLICK\"}"})
public void success(String json) throws Exception {
HttpHeaders headers = new HttpHeaders();
Mockito.doNothing().when(telemetryService).receiveTelemetry(any(EventRequestDto.class));
mockMvc.perform(
put("/telemetry")
.contentType(MediaType.APPLICATION_JSON)
.content(json)
.headers(headers))
.andExpect(
status().isCreated()
)
.andExpect(
content().contentType(MediaType.APPLICATION_JSON)
)
.andExpect(
content().string(HttpStatus.CREATED.getReasonPhrase())
);
}
@ParameterizedTest(name = "Json conversion fail API response")
@ValueSource(strings = {"{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"LEFT_MOUSE_BUTTON_CLICK\",\"pl\":{\"x\":1000,\"y\":5000,\"w\":213,\"h\":124}}","{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"RIGHT_MOUSE_BUTTON_CLICK\"}"})
public void failBadJson(String json) throws Exception {
String expectedErrorMessage = "Failed json conversion";
HttpHeaders headers = new HttpHeaders();
Mockito.doThrow(new JsonConversionException(expectedErrorMessage)).when(telemetryService).receiveTelemetry(any(EventRequestDto.class));
mockMvc.perform(
put("/telemetry")
.contentType(MediaType.APPLICATION_JSON)
.content(json)
.headers(headers))
.andExpect(
status().isBadRequest()
);
}
@ParameterizedTest(name = "Unsupported media type")
@ValueSource(strings = {"{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"LEFT_MOUSE_BUTTON_CLICK\",\"pl\":{\"x\":1000,\"y\":5000,\"w\":213,\"h\":124}}}","{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"RIGHT_MOUSE_BUTTON_CLICK\"}}"})
public void unsupportedMediaType(String json) throws Exception {
HttpHeaders headers = new HttpHeaders();
Mockito.doNothing().when(telemetryService).receiveTelemetry(any(EventRequestDto.class));
mockMvc.perform(
put("/telemetry")
.contentType(MediaType.TEXT_PLAIN)
.content(json)
.headers(headers))
.andExpect(
status().isUnsupportedMediaType()
);
}
@ParameterizedTest(name = "Missing required field or wrong value for type, bad request")
@ValueSource(strings = {"{\"ts\":\"1606297994000\",\"ty\":\"LEFT_MOUSE_BUTTON_CLICK\",\"pl\":{\"x\":1000,\"y\":5000,\"w\":213,\"h\":124}}","{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"MOUSE_BUTTON_CLICK\"}"})
public void missingRequiredField(String json) throws Exception {
HttpHeaders headers = new HttpHeaders();
Mockito.doNothing().when(telemetryService).receiveTelemetry(any(EventRequestDto.class));
mockMvc.perform(
put("/telemetry")
.contentType(MediaType.APPLICATION_JSON)
.content(json)
.headers(headers))
.andExpect(
status().isBadRequest()
);
}
@ParameterizedTest(name = "Method not allowed for non-PUT requests")
@ValueSource(strings = {"{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"LEFT_MOUSE_BUTTON_CLICK\",\"pl\":{\"x\":1000,\"y\":5000,\"w\":213,\"h\":124}}","{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"RIGHT_MOUSE_BUTTON_CLICK\"}"})
public void methodNotAllowed(String json) throws Exception {
HttpHeaders headers = new HttpHeaders();
Mockito.doNothing().when(telemetryService).receiveTelemetry(any(EventRequestDto.class));
mockMvc.perform(
post("/telemetry")
.contentType(MediaType.APPLICATION_JSON)
.content(json)
.headers(headers))
.andExpect(
status().isMethodNotAllowed()
);
}
}

View File

@@ -0,0 +1,52 @@
package com.barrelsofdata.springexamples.producer;
import com.barrelsofdata.springexamples.dto.EventRequestDto;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.kafka.common.KafkaException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.InjectMocks;
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.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.util.concurrent.SettableListenableFuture;
import static org.mockito.ArgumentMatchers.any;
@SpringBootTest
@ExtendWith(SpringExtension.class)
public class KafkaTest {
@MockBean private KafkaTemplate<String, String> kafkaTemplate;
@Autowired @InjectMocks private KafkaImpl producer;
@Autowired private ObjectMapper mapper;
@ParameterizedTest(name = "Check successful send")
@ValueSource(strings = {"{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"LEFT_MOUSE_BUTTON_CLICK\",\"pl\":{\"x\":1000,\"y\":5000,\"w\":213,\"h\":124}}","{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"RIGHT_MOUSE_BUTTON_CLICK\"}"})
public void successSend(String json) throws JsonProcessingException {
EventRequestDto eventRequestDto = mapper.readValue(json, EventRequestDto.class);
String kafkaPayload = mapper.writeValueAsString(eventRequestDto);
Mockito.doReturn(new SettableListenableFuture<>()).when(kafkaTemplate).send(any(String.class), any(String.class));
Assertions.assertDoesNotThrow(() -> producer.publish(kafkaPayload));
}
@ParameterizedTest(name = "Check failed send")
@ValueSource(strings = {"{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"LEFT_MOUSE_BUTTON_CLICK\",\"pl\":{\"x\":1000,\"y\":5000,\"w\":213,\"h\":124}}","{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"RIGHT_MOUSE_BUTTON_CLICK\"}"})
public void failedSend(String json) throws JsonProcessingException, InterruptedException {
EventRequestDto eventRequestDto = mapper.readValue(json, EventRequestDto.class);
String kafkaPayload = mapper.writeValueAsString(eventRequestDto);
Mockito.doThrow(KafkaException.class).when(kafkaTemplate).send(any(String.class), any(String.class));
Assertions.assertThrows(KafkaException.class, () -> producer.publish(kafkaPayload));
}
}

View File

@@ -0,0 +1,44 @@
package com.barrelsofdata.springexamples.service;
import com.barrelsofdata.springexamples.dto.EventRequestDto;
import com.barrelsofdata.springexamples.exception.JsonConversionException;
import com.barrelsofdata.springexamples.producer.Kafka;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.InjectMocks;
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.test.context.junit.jupiter.SpringExtension;
import static org.mockito.ArgumentMatchers.any;
@SpringBootTest
@ExtendWith(SpringExtension.class)
public class TelemetryServiceTest {
@MockBean private Kafka kafka;
@MockBean private ObjectMapper mapper;
@Autowired @InjectMocks private TelemetryServiceImpl telemetryService;
@ParameterizedTest(name = "Successful publish")
@ValueSource(strings = {"{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"LEFT_MOUSE_BUTTON_CLICK\",\"pl\":{\"x\":1000,\"y\":5000,\"w\":213,\"h\":124}}","{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"RIGHT_MOUSE_BUTTON_CLICK\"}"})
public void successPublish(String json) throws JsonProcessingException {
EventRequestDto eventRequestDto = new ObjectMapper().readValue(json, EventRequestDto.class);
telemetryService.receiveTelemetry(eventRequestDto);
}
@ParameterizedTest(name = "Json conversion failure")
@ValueSource(strings = {"{\"ts\":\"1606297994000\",\"id\":\"123\",\"ty\":\"LEFT_MOUSE_BUTTON_CLICK\",\"pl\":{\"x\":1000,\"y\":5000,\"w\":213,\"h\":124}}"})
public void failPublish(String json) throws JsonProcessingException {
EventRequestDto eventRequestDto = new ObjectMapper().readValue(json, EventRequestDto.class);
Mockito.doThrow(JsonProcessingException.class).when(mapper).writeValueAsString(any(EventRequestDto.class));
JsonConversionException exception = Assertions.assertThrows(JsonConversionException.class, () -> telemetryService.receiveTelemetry(eventRequestDto));
Assertions.assertEquals("Failed json conversion", exception.getMessage());
}
}

View File

@@ -0,0 +1,13 @@
server.port=9080
spring.main.banner-mode=off
debug=false
spring.kafka.bootstrap-servers=${spring.embedded.kafka.brokers}
spring.kafka.producer.topic=telemetryTest
spring.kafka.properties.retries=1
spring.kafka.properties.max.block.ms=500
logging.level.org.springframework=WARN
logging.level.org.apache.catalina=WARN
logging.level.org.apache.kafka=INFO
logging.level.com.barrelsofdata=DEBUG