-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
kafka-producer/src/test/java/com/networknt/kafka/producer/ProduceRequestTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.networknt.kafka.producer; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; | ||
import org.junit.jupiter.api.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Test the JSON serialization for the object with the Jackson ObjectMapper | ||
* | ||
* @author Steve Hu | ||
*/ | ||
public class ProduceRequestTest { | ||
@Test | ||
public void testToJson() throws JsonProcessingException { | ||
ObjectMapper mapper = new ObjectMapper(); | ||
mapper.registerModule(new Jdk8Module()); | ||
String json = "{\"key1\":\"value1\",\"key2\":\"value2\"}"; | ||
JsonNode jsonNode = mapper.readTree(json); | ||
ProduceRecord produceRecord = new ProduceRecord(); | ||
produceRecord.setValue(Optional.of(jsonNode)); | ||
List<ProduceRecord> records = new ArrayList<>(); | ||
records.add(produceRecord); | ||
ProduceRequest produceRequest = new ProduceRequest(); | ||
produceRequest.setRecords(records); | ||
String result = mapper.writeValueAsString(produceRequest); | ||
System.out.println(result); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters