BAEL-518 google protocol buffers

* BEEL-518 code for protobuf article

* BEEL-518 add generated protobuf class

* BAEL-701 updated the method argument

* BEEL-550 use newest version of protobuff

* Bael 389 - Building URL dynamically between host and pathname (#1323)

* Project for " A Guide to the Java API for WebSocket" article

* Setting dependencies correctly

* Formatting adjustments

* Removing tomcat7 maven plugin

* Applying formatt - No spaces

* BAEL-389 - Building URL dynamically between host and pathname

* Rename classes (#1331)

* BAEL-550 Axon framework

* BEEL-550 create axon module

* BEEL-550 proper naming

* BEEL-550 better example of message service

* BEEL-550 proper name of method

* BEEL-550 remove not needed comments

* BEEL-550 proper message

* BEEL-550 axon test scope test

* BEEL-550 tries to migrate to axon 3

* BEEL-550 migrate to vesrion 3 successfull

* ACO refactor

* BAEL-518 Small refactoring in protobuffer module
This commit is contained in:
Tomasz Lelek
2017-03-09 15:59:18 +01:00
committed by pedja4
parent 6e00fd16d5
commit 8dcb9af712
5 changed files with 3155 additions and 1 deletions
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,27 @@
package protobuf;
option java_package = "com.baeldung.protobuf";
option java_outer_classname = "AddressBookProtos";
message Person {
required string name = 1;
required int32 id = 2;
optional string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];
}
repeated PhoneNumber phones = 4;
}
message AddressBook {
repeated Person people = 1;
}
@@ -0,0 +1,90 @@
package com.baeldung.protobuf;
import org.junit.After;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Random;
import static org.junit.Assert.assertEquals;
public class ProtobufTest {
private final String filePath = "address_book";
@After
public void cleanup() throws IOException {
Files.deleteIfExists(Paths.get(filePath));
}
@Test
public void givenGeneratedProtobufClass_whenCreateClass_thenShouldCreateJavaInstance() {
//when
String email = "j@baeldung.com";
int id = new Random().nextInt();
String name = "Michael Program";
String number = "01234567890";
AddressBookProtos.Person.PhoneType type = AddressBookProtos.Person.PhoneType.HOME;
AddressBookProtos.Person person =
AddressBookProtos.Person.newBuilder()
.setId(id)
.setName(name)
.setEmail(email)
.addPhones(
AddressBookProtos.Person.PhoneNumber.newBuilder()
.setNumber(number)
.setType(type))
.build();
//then
assertEquals(person.getEmail(), email);
assertEquals(person.getId(), id);
assertEquals(person.getName(), name);
assertEquals(person.getPhones(0).getNumber(), number);
assertEquals(person.getPhones(0).getType(), type);
assertEquals(person.getPhonesList().size(), 1);
}
@Test
public void givenAddressBookWithOnePerson_whenSaveAsAFile_shouldLoadFromFileToJavaClass() throws IOException {
//given
String email = "j@baeldung.com";
int id = new Random().nextInt();
String name = "Michael Program";
String number = "01234567890";
AddressBookProtos.Person.PhoneType type = AddressBookProtos.Person.PhoneType.HOME;
AddressBookProtos.Person person =
AddressBookProtos.Person.newBuilder()
.setId(id)
.setName(name)
.setEmail(email)
.addPhones(
AddressBookProtos.Person.PhoneNumber.newBuilder()
.setNumber(number)
.setType(type))
.build();
//when
AddressBookProtos.AddressBook addressBook = AddressBookProtos.AddressBook.newBuilder().addPeople(person).build();
FileOutputStream fos = new FileOutputStream(filePath);
addressBook.writeTo(fos);
fos.close();
//then
FileInputStream fis = new FileInputStream(filePath);
AddressBookProtos.AddressBook deserialized =
AddressBookProtos.AddressBook.newBuilder().mergeFrom(fis).build();
fis.close();
assertEquals(deserialized.getPeople(0).getEmail(), email);
assertEquals(deserialized.getPeople(0).getId(), id);
assertEquals(deserialized.getPeople(0).getName(), name);
assertEquals(deserialized.getPeople(0).getPhones(0).getNumber(), number);
assertEquals(deserialized.getPeople(0).getPhones(0).getType(), type);
assertEquals(deserialized.getPeople(0).getPhonesList().size(), 1);
}
}