Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix: adding null check for the DescriptorsUtil function
  • Loading branch information
Shreyansh committed Sep 7, 2023
commit 317f91da397f8d350836538bee35341f350be1cc
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
package com.gotocompany.dagger.core.utils;

import com.google.protobuf.Descriptors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Arrays;

import static com.google.protobuf.Descriptors.FieldDescriptor.JavaType.MESSAGE;

/**
* Utility class that contains helper methods to get {@link Descriptors} {@link Descriptors.FieldDescriptor}.
*/
public class DescriptorsUtil {

private static final Logger LOGGER = LoggerFactory.getLogger(DescriptorsUtil.class.getName());

/**
* Gets FieldDescriptor .
*
* @param descriptor the descriptor
* @param columnName the columnName
* @param columnName the columnName
* @return the fieldDescriptor
*/
public static Descriptors.FieldDescriptor getFieldDescriptor(Descriptors.Descriptor descriptor, String columnName) {
if (descriptor == null || columnName == null) {
return null;
}
String[] nestedFields = columnName.split("\\.");
if (nestedFields.length == 1) {
return descriptor.findFieldByName(columnName);
Expand All @@ -28,18 +37,22 @@ public static Descriptors.FieldDescriptor getFieldDescriptor(Descriptors.Descrip
/**
* Gets FieldDescriptor .
*
* @param parentDescriptor the descriptor
* @param parentDescriptor the descriptor
* @param nestedColumnNames the array of columnNames
* @return the fieldDescriptor
*/
public static Descriptors.FieldDescriptor getNestedFieldDescriptor(Descriptors.Descriptor parentDescriptor, String[] nestedColumnNames) {
if (parentDescriptor == null || nestedColumnNames == null || nestedColumnNames.length == 0) {
return null;
}
String childColumnName = nestedColumnNames[0];
if (nestedColumnNames.length == 1) {
return parentDescriptor.findFieldByName(childColumnName);
}
Descriptors.FieldDescriptor childFieldDescriptor = parentDescriptor.findFieldByName(childColumnName);
if (childFieldDescriptor == null) {
throw new IllegalArgumentException(String.format("Field Descriptor not found for field: %s in the proto of %s", childColumnName, parentDescriptor.getFullName()));
if (childFieldDescriptor == null || childFieldDescriptor.getJavaType() != MESSAGE) {
LOGGER.info(String.format("Either the Field Descriptor for the field '%s' is missing in the proto '%s', or the Field Descriptor is not of the MESSAGE type.", childColumnName, parentDescriptor.getFullName()));
return null;
}
Descriptors.Descriptor childDescriptor = childFieldDescriptor.getMessageType();
return getNestedFieldDescriptor(childDescriptor, Arrays.copyOfRange(nestedColumnNames, 1, nestedColumnNames.length));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,39 +405,4 @@ public void shouldThrowErrorWhenNestedFieldIsNotPresentInOutputDescriptor() thro
verify(resultFuture, times(1)).completeExceptionally(exceptionCaptor2.capture());
assertEquals("Field Descriptor not found for field: driver_pickup_location.invalid_address", exceptionCaptor2.getValue().getMessage());
}
@Test
public void shouldThrowErrorWhenNestedParentFieldIsNotPresentInOutputDescriptor() throws InvalidProtocolBufferException {
descriptor = TestBookingLogMessage.getDescriptor();
outputMapping.put("driver-pickup-location.address", new OutputMapping("$.driver_pickup_location.address"));
TestLocation location = TestLocation.newBuilder().setAddress("Indonesia").setName("GojekTech").build();
TestBookingLogMessage bookingLogMessage = TestBookingLogMessage.newBuilder().setDriverPickupLocation(location).setCustomerId("123456").build();

grpcSourceConfig = new GrpcSourceConfigBuilder().setEndpoint("localhost").setServicePort(8000).setGrpcRequestProtoSchema("com.gotocompany.dagger.consumer.TestGrpcRequest").setGrpcResponseProtoSchema("com.gotocompany.dagger.consumer.TestGrpcResponse").setGrpcMethodUrl("com.gotocompany.dagger.consumer.test/TestMethod").setRequestPattern("{\"key\": \"%s\"}").setRequestVariables("customer_id").setOutputMapping(outputMapping).createGrpcSourceConfig();
grpcSourceConfig.setRetainResponseType(false);

outputColumnNames = Arrays.asList("driver-pickup-location.address");
columnNameManager = new ColumnNameManager(inputColumnNames, outputColumnNames);

DynamicMessage message = DynamicMessage.parseFrom(TestBookingLogMessage.getDescriptor(), bookingLogMessage.toByteArray());
GrpcResponseHandler grpcResponseHandler = new GrpcResponseHandler(grpcSourceConfig, meterStatsManager, rowManager, columnNameManager, descriptor, resultFuture, errorReporter, new PostResponseTelemetry());

Row resultStreamData = new Row(2);
Row outputData = new Row(2);
outputData.setField(0, "Indonesia");
outputData.setField(1, "GojekTech");
resultStreamData.setField(0, inputData);
resultStreamData.setField(1, outputData);

grpcResponseHandler.startTimer();
assertThrows(Exception.class, () -> grpcResponseHandler.onNext(message));

ArgumentCaptor<IllegalArgumentException> exceptionCaptor = ArgumentCaptor.forClass(IllegalArgumentException.class);
verify(errorReporter, times(1)).reportFatalException(exceptionCaptor.capture());
assertEquals("Field Descriptor not found for field: driver-pickup-location in the proto of com.gotocompany.dagger.consumer.TestBookingLogMessage", exceptionCaptor.getValue().getMessage());

ArgumentCaptor<IllegalArgumentException> exceptionCaptor2 = ArgumentCaptor.forClass(IllegalArgumentException.class);
verify(resultFuture, times(1)).completeExceptionally(exceptionCaptor2.capture());
assertEquals("Field Descriptor not found for field: driver-pickup-location in the proto of com.gotocompany.dagger.consumer.TestBookingLogMessage", exceptionCaptor2.getValue().getMessage());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

Expand Down Expand Up @@ -39,6 +38,24 @@ public void shouldRunGetNestedFieldColumnsDescriptor() {
assertEquals("customer_id", fieldDescriptor.getName());
}

@Test
public void shouldGiveNullForEmptyFieldFieldDescriptor() {
String nonExistentField = "customer-id";
Descriptors.FieldDescriptor nonExistentFieldDescriptor = DescriptorsUtil.getFieldDescriptor(null, nonExistentField);
assertNull(nonExistentFieldDescriptor);
}
@Test
public void shouldGiveNullForNullColumnFieldFieldDescriptor() {
Descriptors.Descriptor descriptor = TestCustomerLogMessage.getDescriptor();
Descriptors.FieldDescriptor nonExistentFieldDescriptor = DescriptorsUtil.getFieldDescriptor(descriptor, null);
assertNull(nonExistentFieldDescriptor);
}
@Test
public void shouldGiveNullForEmptyColumnFieldFieldDescriptor() {
Descriptors.Descriptor descriptor = TestCustomerLogMessage.getDescriptor();
Descriptors.FieldDescriptor nonExistentFieldDescriptor = DescriptorsUtil.getFieldDescriptor(descriptor, "");
assertNull(nonExistentFieldDescriptor);
}
@Test
public void shouldGiveNullForInvalidFieldFieldDescriptor() {
Descriptors.Descriptor descriptor = TestCustomerLogMessage.getDescriptor();
Expand All @@ -55,30 +72,32 @@ public void shouldGiveNullForInvalidNestedFieldDescriptor() {
assertNull(invalidFieldDescriptor);
}


@Test
public void shouldGiveNullForInvlidNestedFieldColumnsDescriptor() {
public void shouldGiveNullForInvalidNestedFieldColumnsDescriptor() {
Descriptors.Descriptor parentDescriptor = TestEnrichedBookingLogMessage.getDescriptor();
String[] invalidNestedFieldNames = {"customer_profile", "customer-id"};
Descriptors.FieldDescriptor invalidFieldDescriptor = DescriptorsUtil.getNestedFieldDescriptor(parentDescriptor, invalidNestedFieldNames);
assertNull(invalidFieldDescriptor);
}

@Test
public void shouldThrowExceptionWhenNestedColumnDoesNotExists() {
Descriptors.Descriptor parentDescriptor = TestEnrichedBookingLogMessage.getDescriptor();
String invalidNestedFieldNames = "booking_log.driver_pickup-location.name";
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
() -> DescriptorsUtil.getFieldDescriptor(parentDescriptor, invalidNestedFieldNames));
assertEquals("Field Descriptor not found for field: driver_pickup-location in the proto of com.gotocompany.dagger.consumer.TestBookingLogMessage",
exception.getMessage());
public void shouldGiveNullForNullNestedFieldDescriptor() {
String[] nonExistentField = new String[]{"customer-id"};
Descriptors.FieldDescriptor nonExistentFieldDescriptor = DescriptorsUtil.getNestedFieldDescriptor(null, nonExistentField);
assertNull(nonExistentFieldDescriptor);
}

@Test
public void shouldThrowExceptionWhenNestedColumnGetFieldDescriptorDoesNotExists() {
Descriptors.Descriptor parentDescriptor = TestEnrichedBookingLogMessage.getDescriptor();
String[] invalidNestedFieldNames = {"booking_log", "driver_pickup-location", "name"};
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class,
() -> DescriptorsUtil.getNestedFieldDescriptor(parentDescriptor, invalidNestedFieldNames));
assertEquals("Field Descriptor not found for field: driver_pickup-location in the proto of com.gotocompany.dagger.consumer.TestBookingLogMessage",
exception.getMessage());
public void shouldGiveNullForNullColumnNestedFieldDescriptor() {
Descriptors.Descriptor descriptor = TestCustomerLogMessage.getDescriptor();
Descriptors.FieldDescriptor nonExistentFieldDescriptor = DescriptorsUtil.getNestedFieldDescriptor(descriptor, null);
assertNull(nonExistentFieldDescriptor);
}
@Test
public void shouldGiveNullForEmptyColumnNestedFieldDescriptor() {
Descriptors.Descriptor descriptor = TestCustomerLogMessage.getDescriptor();
Descriptors.FieldDescriptor nonExistentFieldDescriptor = DescriptorsUtil.getNestedFieldDescriptor(descriptor, new String[]{});
assertNull(nonExistentFieldDescriptor);
}
}