Repository: authorjapps/zerocode
Pull request #427 (2020-09-04 11:41:09)

ISSUE-423 Proto validation when missing classType
# <Feature Title>

PR Branch
**_#ADD LINK TO THE PR BRANCH_**

## Motivation and Context

## Checklist:

* [x] Unit tests added

* [x] Integration tests added

* [x] Test names are meaningful

* [x] Feature manually tested

* [x] Branch build passed

* [x] No 'package.*' in the imports

* [x] Relevant Wiki page updated with clear instruction for the end user
  * [ ] Not applicable. This was only a refactor change, no functional or behaviour changes were introduced

* [ ] Http test added to `http-testing` module(if applicable) ?
  * [x] Not applicable. The changes did not affect HTTP automation flow

* [x] Kafka test added to `kafka-testing` module(if applicable) ?
  * [ ] Not applicable. The changes did not affect Kafka automation flow



Patch:
diff --git a/core/src/main/java/org/jsmart/zerocode/core/kafka/helper/KafkaConsumerHelper.java b/core/src/main/java/org/jsmart/zerocode/core/kafka/helper/KafkaConsumerHelper.java
index c428d653..8b2ab68e 100644
--- a/core/src/main/java/org/jsmart/zerocode/core/kafka/helper/KafkaConsumerHelper.java
+++ b/core/src/main/java/org/jsmart/zerocode/core/kafka/helper/KafkaConsumerHelper.java
@@ -238,7 +238,7 @@ public class KafkaConsumerHelper {
 	private static String convertProtobufToJson(ConsumerRecord thisRecord, ConsumerLocalConfigs consumerLocalConfig) {
 		if (org.apache.commons.lang3.StringUtils.isEmpty(consumerLocalConfig.getProtoClassType())) {
 			throw new IllegalArgumentException(
-					"[ProtobufMessageClassType] is required consumer config for PROTO record Type.");
+					"[protoClassType] is required consumer config for recordType PROTO.");
 		}
 		MessageOrBuilder builderOrMessage = (MessageOrBuilder) createMessageOrBuilder(
 				consumerLocalConfig.getProtoClassType(), (byte[]) thisRecord.value());
diff --git a/core/src/main/java/org/jsmart/zerocode/core/kafka/helper/KafkaProducerHelper.java b/core/src/main/java/org/jsmart/zerocode/core/kafka/helper/KafkaProducerHelper.java
index 4a485a90..0d965df5 100644
--- a/core/src/main/java/org/jsmart/zerocode/core/kafka/helper/KafkaProducerHelper.java
+++ b/core/src/main/java/org/jsmart/zerocode/core/kafka/helper/KafkaProducerHelper.java
@@ -69,45 +69,46 @@ public class KafkaProducerHelper {
                 recordToSend.headers());
     }
 
-    public static ProducerRecord<Object, Object> prepareJsonRecordToSend(String topicName, ProducerJsonRecord recordToSend, String recordType,
-			String requestJson) {
+    public static ProducerRecord<Object, Object> prepareJsonRecordToSend(String topicName,
+                                                                         ProducerJsonRecord recordToSend,
+                                                                         String recordType,
+                                                                         String requestJson) {
         return ProducerRecordBuilder.from(topicName,
                 recordToSend.getKey(),
                 // --------------------------------------------
                 // It's a JSON as String. Nothing to worry !
                 // Kafka StringSerializer needs in this format.
                 // --------------------------------------------
-                KafkaConstants.PROTO.equalsIgnoreCase(recordType)?buildProtoMessage(recordToSend.getValue().toString(),requestJson):recordToSend.getValue().toString())
+                KafkaConstants.PROTO.equalsIgnoreCase(recordType) ? buildProtoMessage(recordToSend.getValue().toString(), requestJson) : recordToSend.getValue().toString())
                 .withHeaders(recordToSend.getHeaders())
                 .build();
     }
-    
-
-
-	private static Object buildProtoMessage(String message, String requestJson) {
-		String protobufMessageClassName = readRecordType(requestJson, KafkaConstants.PROTO_BUF_MESSAGE_CLASS_TYPE);
-		Builder builder = createBuilder(protobufMessageClassName);
-		try {
-			JsonFormat.parser().merge(message, builder);
-		} catch (InvalidProtocolBufferException e) {
-			throw new IllegalArgumentException(e);
-		}
-		return builder.build().toByteArray();
-	}
-
-	private static Builder createBuilder(String messageClass) {
-		try {
-			Class<Message> msgClass = (Class<Message>) Class.forName(messageClass);
-			Method method = msgClass.getMethod("newBuilder", null);
-			return (Builder) method.invoke(null, null);
-		} catch (IllegalAccessException | ClassNotFoundException | NoSuchMethodException | SecurityException
-				| IllegalArgumentException | InvocationTargetException e) {
-			throw new IllegalArgumentException(e);
-		}
-
-	}
-
-	public static String readRecordType(String requestJson, String jsonPath) {
+
+
+    private static Object buildProtoMessage(String message, String requestJson) {
+        String protobufMessageClassName = protoClassType(requestJson, KafkaConstants.PROTO_BUF_MESSAGE_CLASS_TYPE);
+        Builder builder = createBuilder(protobufMessageClassName);
+        try {
+            JsonFormat.parser().merge(message, builder);
+        } catch (InvalidProtocolBufferException e) {
+            throw new IllegalArgumentException(e);
+        }
+        return builder.build().toByteArray();
+    }
+
+    private static Builder createBuilder(String messageClass) {
+        try {
+            Class<Message> msgClass = (Class<Message>) Class.forName(messageClass);
+            Method method = msgClass.getMethod("newBuilder", null);
+            return (Builder) method.invoke(null, null);
+        } catch (IllegalAccessException | ClassNotFoundException | NoSuchMethodException | SecurityException
+                | IllegalArgumentException | InvocationTargetException e) {
+            throw new IllegalArgumentException(e);
+        }
+
+    }
+
+    public static String readRecordType(String requestJson, String jsonPath) {
         try {
             return JsonPath.read(requestJson, jsonPath);
         } catch (PathNotFoundException pEx) {
@@ -116,4 +117,14 @@ public class KafkaProducerHelper {
         }
     }
 
+    public static String protoClassType(String requestJson, String classTypeJsonPath) {
+        try {
+            return JsonPath.read(requestJson, classTypeJsonPath);
+        } catch (PathNotFoundException pEx) {
+            LOGGER.error("Could not find path '" + classTypeJsonPath + "' in the request. returned default type 'RAW'.");
+            String errMsg = "Missing 'protoClassType' for 'recordType:PROTO'. Please provide 'protoClassType' and rerun ";
+            throw new RuntimeException(errMsg);
+        }
+    }
+
 }
diff --git a/core/src/main/java/org/jsmart/zerocode/core/kafka/send/KafkaSender.java b/core/src/main/java/org/jsmart/zerocode/core/kafka/send/KafkaSender.java
index d9c0946b..f8f89945 100644
--- a/core/src/main/java/org/jsmart/zerocode/core/kafka/send/KafkaSender.java
+++ b/core/src/main/java/org/jsmart/zerocode/core/kafka/send/KafkaSender.java
@@ -156,7 +156,9 @@ public class KafkaSender {
     private String sendJson(String topicName,
                             Producer<?, ?> producer,
                             ProducerJsonRecord recordToSend,
-                            Boolean isAsync, String recordType, String requestJson) throws InterruptedException, ExecutionException {
+                            Boolean isAsync,
+                            String recordType,
+                            String requestJson) throws InterruptedException, ExecutionException {
         ProducerRecord record = prepareJsonRecordToSend(topicName, recordToSend,recordType, requestJson);
 
         RecordMetadata metadata;


Test patch:
diff --git a/core/src/test/java/org/jsmart/zerocode/core/kafka/helper/KafkaProducerHelperTest.java b/core/src/test/java/org/jsmart/zerocode/core/kafka/helper/KafkaProducerHelperTest.java
new file mode 100644
index 00000000..77f46e9e
--- /dev/null
+++ b/core/src/test/java/org/jsmart/zerocode/core/kafka/helper/KafkaProducerHelperTest.java
@@ -0,0 +1,37 @@
+package org.jsmart.zerocode.core.kafka.helper;
+
+
+import org.jsmart.zerocode.core.kafka.KafkaConstants;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+import static org.jsmart.zerocode.core.kafka.helper.KafkaProducerHelper.protoClassType;
+
+public class KafkaProducerHelperTest  {
+    @Rule
+    public ExpectedException expectedException = ExpectedException.none();
+
+    @Test
+    public void test_protoClassType() {
+        String requestJson = "{\n" +
+                "\"recordType\": \"PROTO\"," +
+                "\"protoClassType\": \"org.jsmart.zerocode.proto.Foo$Bar\"" +
+                "}";
+        String classType = protoClassType(requestJson, KafkaConstants.PROTO_BUF_MESSAGE_CLASS_TYPE);
+        assertThat(classType, is("org.jsmart.zerocode.proto.Foo$Bar"));
+    }
+
+    @Test
+    public void test_protoClassTypeMissing() {
+
+        String requestJson = "{\n" +
+                "\"recordType\": \"PROTO\"" +
+                //"\"protoClassType\": \"org.jsmart.zerocode.proto.Foo$Bar\"" +
+                "}";
+        expectedException.expectMessage("Missing 'protoClassType' for 'recordType:PROTO'. Please provide 'protoClassType'");
+        protoClassType(requestJson, KafkaConstants.PROTO_BUF_MESSAGE_CLASS_TYPE);
+    }
+}
\ No newline at end of file
diff --git a/kafka-testing/pom.xml b/kafka-testing/pom.xml
index b97e8d5d..213d0bc6 100644
--- a/kafka-testing/pom.xml
+++ b/kafka-testing/pom.xml
@@ -68,7 +68,7 @@
 					</includes>
 				</configuration>
 			</plugin>
-			<plugin>
+			<!--<plugin>
 				<artifactId>maven-assembly-plugin</artifactId>
 				<version>2.5.5</version>
 				<configuration>
@@ -78,8 +78,8 @@
 						<descriptor>src/assembly/dist.xml</descriptor>
 					</descriptors>
 				</configuration>
-			</plugin>
-			<plugin>
+			</plugin>-->
+			<!--<plugin>
 				<artifactId>maven-dependency-plugin</artifactId>
 				<executions>
 					<execution>
@@ -92,7 +92,7 @@
 						</configuration>
 					</execution>
 				</executions>
-			</plugin>
+			</plugin>-->
 			<plugin>
 				<groupId>com.github.os72</groupId>
 				<artifactId>protoc-jar-maven-plugin</artifactId>
diff --git a/kafka-testing/src/test/java/org/jsmart/zerocode/integration/tests/kafka/protobuf/KafkaProtobufTest.java b/kafka-testing/src/test/java/org/jsmart/zerocode/integration/tests/kafka/protobuf/KafkaProtobufTest.java
index f8a8b120..4720bb7e 100644
--- a/kafka-testing/src/test/java/org/jsmart/zerocode/integration/tests/kafka/protobuf/KafkaProtobufTest.java
+++ b/kafka-testing/src/test/java/org/jsmart/zerocode/integration/tests/kafka/protobuf/KafkaProtobufTest.java
@@ -15,5 +15,4 @@ public class KafkaProtobufTest {
     public void testProduceConsume() throws Exception {
     }
 
-
 }