Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-8232

This commit is contained in:
amit2103 2018-08-19 00:04:04 +05:30
commit 74bfeb18ad
7 changed files with 104 additions and 82 deletions

View File

@ -5,29 +5,37 @@ import org.apache.avro.io.DatumReader;
import org.apache.avro.io.Decoder; import org.apache.avro.io.Decoder;
import org.apache.avro.io.DecoderFactory; import org.apache.avro.io.DecoderFactory;
import org.apache.avro.specific.SpecificDatumReader; import org.apache.avro.specific.SpecificDatumReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException; import java.io.IOException;
public class AvroDeSerealizer { public class AvroDeSerealizer {
public AvroHttpRequest deSerealizeAvroHttpRequestJSON(byte[] data){ private static Logger logger = LoggerFactory.getLogger(AvroDeSerealizer.class);
public AvroHttpRequest deSerealizeAvroHttpRequestJSON(byte[] data) {
DatumReader<AvroHttpRequest> reader = new SpecificDatumReader<>(AvroHttpRequest.class); DatumReader<AvroHttpRequest> reader = new SpecificDatumReader<>(AvroHttpRequest.class);
Decoder decoder = null; Decoder decoder = null;
try { try {
decoder = DecoderFactory.get().jsonDecoder(AvroHttpRequest.getClassSchema(), new String(data)); decoder = DecoderFactory.get()
.jsonDecoder(AvroHttpRequest.getClassSchema(), new String(data));
return reader.read(null, decoder); return reader.read(null, decoder);
} catch (IOException e) { } catch (IOException e) {
logger.error("Deserialization error" + e.getMessage());
}
return null; return null;
} }
}
public AvroHttpRequest deSerealizeAvroHttpRequestBinary(byte[] data){ public AvroHttpRequest deSerealizeAvroHttpRequestBinary(byte[] data) {
DatumReader<AvroHttpRequest> employeeReader = new SpecificDatumReader<>(AvroHttpRequest.class); DatumReader<AvroHttpRequest> employeeReader = new SpecificDatumReader<>(AvroHttpRequest.class);
Decoder decoder = DecoderFactory.get().binaryDecoder(data, null); Decoder decoder = DecoderFactory.get()
.binaryDecoder(data, null);
try { try {
return employeeReader.read(null, decoder); return employeeReader.read(null, decoder);
} catch (IOException e) { } catch (IOException e) {
logger.error("Deserialization error" + e.getMessage());
}
return null; return null;
} }
} }
}

View File

@ -3,42 +3,48 @@ package com.baeldung.avro.util.serealization;
import com.baeldung.avro.util.model.AvroHttpRequest; import com.baeldung.avro.util.model.AvroHttpRequest;
import org.apache.avro.io.*; import org.apache.avro.io.*;
import org.apache.avro.specific.SpecificDatumWriter; import org.apache.avro.specific.SpecificDatumWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
public class AvroSerealizer { public class AvroSerealizer {
public byte[] serealizeAvroHttpRequestJSON(AvroHttpRequest request){ private static final Logger logger = LoggerFactory.getLogger(AvroSerealizer.class);
public byte[] serealizeAvroHttpRequestJSON(AvroHttpRequest request) {
DatumWriter<AvroHttpRequest> writer = new SpecificDatumWriter<>(AvroHttpRequest.class); DatumWriter<AvroHttpRequest> writer = new SpecificDatumWriter<>(AvroHttpRequest.class);
byte[] data = new byte[0]; byte[] data = new byte[0];
ByteArrayOutputStream stream = new ByteArrayOutputStream(); ByteArrayOutputStream stream = new ByteArrayOutputStream();
Encoder jsonEncoder = null; Encoder jsonEncoder = null;
try { try {
jsonEncoder = EncoderFactory.get().jsonEncoder(AvroHttpRequest.getClassSchema(), stream); jsonEncoder = EncoderFactory.get()
.jsonEncoder(AvroHttpRequest.getClassSchema(), stream);
writer.write(request, jsonEncoder); writer.write(request, jsonEncoder);
jsonEncoder.flush(); jsonEncoder.flush();
data = stream.toByteArray(); data = stream.toByteArray();
} catch (IOException e) { } catch (IOException e) {
data =null; logger.error("Serialization error " + e.getMessage());
} }
return data; return data;
} }
public byte[] serealizeAvroHttpRequestBinary(AvroHttpRequest request){ public byte[] serealizeAvroHttpRequestBinary(AvroHttpRequest request) {
DatumWriter<AvroHttpRequest> writer = new SpecificDatumWriter<>(AvroHttpRequest.class); DatumWriter<AvroHttpRequest> writer = new SpecificDatumWriter<>(AvroHttpRequest.class);
byte[] data = new byte[0]; byte[] data = new byte[0];
ByteArrayOutputStream stream = new ByteArrayOutputStream(); ByteArrayOutputStream stream = new ByteArrayOutputStream();
Encoder jsonEncoder = EncoderFactory.get().binaryEncoder(stream,null); Encoder jsonEncoder = EncoderFactory.get()
.binaryEncoder(stream, null);
try { try {
writer.write(request, jsonEncoder); writer.write(request, jsonEncoder);
jsonEncoder.flush(); jsonEncoder.flush();
data = stream.toByteArray(); data = stream.toByteArray();
} catch (IOException e) { } catch (IOException e) {
data = null; logger.error("Serialization error " + e.getMessage());
} }
return data; return data;
} }
} }

View File

@ -24,8 +24,10 @@ public class AvroSerealizerDeSerealizerTest {
serealizer = new AvroSerealizer(); serealizer = new AvroSerealizer();
deSerealizer = new AvroDeSerealizer(); deSerealizer = new AvroDeSerealizer();
ClientIdentifier clientIdentifier = ClientIdentifier.newBuilder(). ClientIdentifier clientIdentifier = ClientIdentifier.newBuilder()
setHostName("localhost").setIpAddress("255.255.255.0").build(); .setHostName("localhost")
.setIpAddress("255.255.255.0")
.build();
List<CharSequence> employees = new ArrayList(); List<CharSequence> employees = new ArrayList();
employees.add("James"); employees.add("James");
@ -33,43 +35,49 @@ public class AvroSerealizerDeSerealizerTest {
employees.add("David"); employees.add("David");
employees.add("Han"); employees.add("Han");
request = AvroHttpRequest.newBuilder().setRequestTime(01l) request = AvroHttpRequest.newBuilder()
.setActive(Active.YES).setClientIdentifier(clientIdentifier) .setRequestTime(01l)
.setEmployeeNames(employees).build(); .setActive(Active.YES)
.setClientIdentifier(clientIdentifier)
.setEmployeeNames(employees)
.build();
} }
@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
} }
@Test @Test
public void WhenSerialized_UsingJSONEncoder_ObjectGetsSerialized(){ public void WhenSerializedUsingJSONEncoder_thenObjectGetsSerialized() {
byte[] data = serealizer.serealizeAvroHttpRequestJSON(request); byte[] data = serealizer.serealizeAvroHttpRequestJSON(request);
assertTrue(Objects.nonNull(data)); assertTrue(Objects.nonNull(data));
assertTrue(data.length > 0); assertTrue(data.length > 0);
} }
@Test @Test
public void WhenSerialized_UsingBinaryEncoder_ObjectGetsSerialized(){ public void WhenSerializedUsingBinaryEncoder_thenObjectGetsSerialized() {
byte[] data = serealizer.serealizeAvroHttpRequestBinary(request); byte[] data = serealizer.serealizeAvroHttpRequestBinary(request);
assertTrue(Objects.nonNull(data)); assertTrue(Objects.nonNull(data));
assertTrue(data.length > 0); assertTrue(data.length > 0);
} }
@Test @Test
public void WhenDeserialize_UsingJSONDecoder_ActualAndExpectedObjectsAreEqual(){ public void WhenDeserializeUsingJSONDecoder_thenActualAndExpectedObjectsAreEqual() {
byte[] data = serealizer.serealizeAvroHttpRequestJSON(request); byte[] data = serealizer.serealizeAvroHttpRequestJSON(request);
AvroHttpRequest actualRequest = deSerealizer.deSerealizeAvroHttpRequestJSON(data); AvroHttpRequest actualRequest = deSerealizer.deSerealizeAvroHttpRequestJSON(data);
assertEquals(actualRequest,request); assertEquals(actualRequest, request);
assertTrue(actualRequest.getRequestTime().equals(request.getRequestTime())); assertTrue(actualRequest.getRequestTime()
} .equals(request.getRequestTime()));
}
@Test @Test
public void WhenDeserialize_UsingBinaryecoder_ActualAndExpectedObjectsAreEqual(){ public void WhenDeserializeUsingBinaryecoder_thenActualAndExpectedObjectsAreEqual() {
byte[] data = serealizer.serealizeAvroHttpRequestBinary(request); byte[] data = serealizer.serealizeAvroHttpRequestBinary(request);
AvroHttpRequest actualRequest = deSerealizer.deSerealizeAvroHttpRequestBinary(data); AvroHttpRequest actualRequest = deSerealizer.deSerealizeAvroHttpRequestBinary(data);
assertEquals(actualRequest,request); assertEquals(actualRequest, request);
assertTrue(actualRequest.getRequestTime().equals(request.getRequestTime())); assertTrue(actualRequest.getRequestTime()
} .equals(request.getRequestTime()));
}
} }

View File

@ -25,7 +25,7 @@ public class User {
if (this.getClass() != o.getClass()) if (this.getClass() != o.getClass())
return false; return false;
User user = (User) o; User user = (User) o;
return id != user.id && (!name.equals(user.name) && !email.equals(user.email)); return id == user.id && (name.equals(user.name) && email.equals(user.email));
} }
@Override @Override
@ -38,4 +38,5 @@ public class User {
return hash; return hash;
} }
// getters and setters here // getters and setters here
} }

View File

@ -3,3 +3,4 @@
- [Guide to @Immutable Annotation in Hibernate](http://www.baeldung.com/hibernate-immutable) - [Guide to @Immutable Annotation in Hibernate](http://www.baeldung.com/hibernate-immutable)
- [Hibernate Many to Many Annotation Tutorial](http://www.baeldung.com/hibernate-many-to-many) - [Hibernate Many to Many Annotation Tutorial](http://www.baeldung.com/hibernate-many-to-many)
- [Programmatic Transactions in the Spring TestContext Framework](http://www.baeldung.com/spring-test-programmatic-transactions) - [Programmatic Transactions in the Spring TestContext Framework](http://www.baeldung.com/spring-test-programmatic-transactions)
- [Hibernate Criteria Queries](http://www.baeldung.com/hibernate-criteria-queries)

View File

@ -12,4 +12,3 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
- [Introduction to Spring REST Docs](http://www.baeldung.com/spring-rest-docs) - [Introduction to Spring REST Docs](http://www.baeldung.com/spring-rest-docs)
- [Spring ResponseStatusException](http://www.baeldung.com/spring-response-status-exception) - [Spring ResponseStatusException](http://www.baeldung.com/spring-response-status-exception)
- [Spring Assert Statements](http://www.baeldung.com/spring-assert) - [Spring Assert Statements](http://www.baeldung.com/spring-assert)
- [Spring Security 5 OAuth2 Login](http://www.baeldung.com/spring-security-5-oauth2-login)

View File

@ -11,7 +11,6 @@
- [Stored Procedures with Hibernate](http://www.baeldung.com/stored-procedures-with-hibernate-tutorial) - [Stored Procedures with Hibernate](http://www.baeldung.com/stored-procedures-with-hibernate-tutorial)
- [Hibernate: save, persist, update, merge, saveOrUpdate](http://www.baeldung.com/hibernate-save-persist-update-merge-saveorupdate) - [Hibernate: save, persist, update, merge, saveOrUpdate](http://www.baeldung.com/hibernate-save-persist-update-merge-saveorupdate)
- [Eager/Lazy Loading In Hibernate](http://www.baeldung.com/hibernate-lazy-eager-loading) - [Eager/Lazy Loading In Hibernate](http://www.baeldung.com/hibernate-lazy-eager-loading)
- [Hibernate Criteria Queries](http://www.baeldung.com/hibernate-criteria-queries)
- [Hibernate One to Many Annotation Tutorial](http://www.baeldung.com/hibernate-one-to-many) - [Hibernate One to Many Annotation Tutorial](http://www.baeldung.com/hibernate-one-to-many)
- [Guide to @Immutable Annotation in Hibernate](http://www.baeldung.com/hibernate-immutable) - [Guide to @Immutable Annotation in Hibernate](http://www.baeldung.com/hibernate-immutable)
- [The DAO with Spring and Hibernate](http://www.baeldung.com/persistence-layer-with-spring-and-hibernate) - [The DAO with Spring and Hibernate](http://www.baeldung.com/persistence-layer-with-spring-and-hibernate)