diff --git a/pom.xml b/pom.xml index 7f2bfee..1e90a90 100644 --- a/pom.xml +++ b/pom.xml @@ -2,7 +2,7 @@ 4.0.0 br.com.insula opes - 1.3.2-SNAPSHOT + 1.4.1 Projeto Opes Sugestões de classes imutáveis para Rich Domain Model de casos de uso do Brasil diff --git a/src/main/java/br/com/insula/opes/json/deserializer/CnpjJsonDeserializer.java b/src/main/java/br/com/insula/opes/json/deserializer/CnpjJsonDeserializer.java new file mode 100644 index 0000000..00d27e0 --- /dev/null +++ b/src/main/java/br/com/insula/opes/json/deserializer/CnpjJsonDeserializer.java @@ -0,0 +1,19 @@ +package br.com.insula.opes.json.deserializer; + +import java.io.IOException; + +import org.codehaus.jackson.JsonParser; +import org.codehaus.jackson.JsonProcessingException; +import org.codehaus.jackson.map.DeserializationContext; +import org.codehaus.jackson.map.JsonDeserializer; + +import br.com.insula.opes.Cnpj; + +public class CnpjJsonDeserializer extends JsonDeserializer { + + @Override + public Cnpj deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { + return Cnpj.fromString(jp.getText()); + } + +} \ No newline at end of file diff --git a/src/main/java/br/com/insula/opes/json/deserializer/CpfJsonDeserializer.java b/src/main/java/br/com/insula/opes/json/deserializer/CpfJsonDeserializer.java new file mode 100644 index 0000000..8e41005 --- /dev/null +++ b/src/main/java/br/com/insula/opes/json/deserializer/CpfJsonDeserializer.java @@ -0,0 +1,19 @@ +package br.com.insula.opes.json.deserializer; + +import java.io.IOException; + +import org.codehaus.jackson.JsonParser; +import org.codehaus.jackson.JsonProcessingException; +import org.codehaus.jackson.map.DeserializationContext; +import org.codehaus.jackson.map.JsonDeserializer; + +import br.com.insula.opes.Cpf; + +public class CpfJsonDeserializer extends JsonDeserializer { + + @Override + public Cpf deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { + return Cpf.fromString(jp.getText()); + } + +} \ No newline at end of file diff --git a/src/main/java/br/com/insula/opes/json/serializer/CnpjJsonSerializer.java b/src/main/java/br/com/insula/opes/json/serializer/CnpjJsonSerializer.java new file mode 100644 index 0000000..7d2439f --- /dev/null +++ b/src/main/java/br/com/insula/opes/json/serializer/CnpjJsonSerializer.java @@ -0,0 +1,24 @@ +package br.com.insula.opes.json.serializer; + +import java.io.IOException; + +import org.codehaus.jackson.JsonGenerator; +import org.codehaus.jackson.JsonProcessingException; +import org.codehaus.jackson.map.SerializerProvider; +import org.codehaus.jackson.map.ser.std.SerializerBase; + +import br.com.insula.opes.Cnpj; + +public class CnpjJsonSerializer extends SerializerBase { + + public CnpjJsonSerializer() { + super(Cnpj.class); + } + + @Override + public void serialize(Cnpj value, JsonGenerator jgen, SerializerProvider provider) throws IOException, + JsonProcessingException { + jgen.writeString(value.toString()); + } + +} \ No newline at end of file diff --git a/src/main/java/br/com/insula/opes/json/serializer/CpfJsonSerializer.java b/src/main/java/br/com/insula/opes/json/serializer/CpfJsonSerializer.java new file mode 100644 index 0000000..60cfe73 --- /dev/null +++ b/src/main/java/br/com/insula/opes/json/serializer/CpfJsonSerializer.java @@ -0,0 +1,23 @@ +package br.com.insula.opes.json.serializer; + +import java.io.IOException; + +import org.codehaus.jackson.JsonGenerator; +import org.codehaus.jackson.JsonProcessingException; +import org.codehaus.jackson.map.SerializerProvider; +import org.codehaus.jackson.map.ser.std.SerializerBase; + +import br.com.insula.opes.Cpf; + +public class CpfJsonSerializer extends SerializerBase { + + public CpfJsonSerializer() { + super(Cpf.class); + } + + @Override + public void serialize(Cpf value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException { + jgen.writeString(value.toString()); + } + +} \ No newline at end of file diff --git a/src/test/java/br/com/insula/opes/json/deserializer/CnpjJsonDeserializerTest.java b/src/test/java/br/com/insula/opes/json/deserializer/CnpjJsonDeserializerTest.java new file mode 100644 index 0000000..34ff8ef --- /dev/null +++ b/src/test/java/br/com/insula/opes/json/deserializer/CnpjJsonDeserializerTest.java @@ -0,0 +1,52 @@ +package br.com.insula.opes.json.deserializer; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.codehaus.jackson.Version; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.map.module.SimpleModule; +import org.junit.Test; + +import br.com.insula.opes.Cnpj; + +public class CnpjJsonDeserializerTest { + + @Test + public void testDeserializeJsonParserDeserializationContext() throws Exception { + ObjectMapper mapper = createObjectMapper(); + + String json = "{\"cnpj\":\"86266937000129\"}"; + Teste teste = mapper.readValue(json, Teste.class); + assertEquals(Cnpj.fromString("86.266.937/0001-29"), teste.getCnpj()); + } + + private ObjectMapper createObjectMapper() { + ObjectMapper objectMapper = new ObjectMapper(); + SimpleModule module = new SimpleModule("OpesModule", new Version(1, 0, 0, null)); + module.addDeserializer(Cnpj.class, new CnpjJsonDeserializer()); + objectMapper.registerModule(module); + return objectMapper; + } + + @Test + public void testDeserializeJsonParserDeserializationContextWithNull() throws Exception { + ObjectMapper mapper = createObjectMapper(); + + String json = "{\"cnpj\":null}"; + Teste teste = mapper.readValue(json, Teste.class); + assertNull(teste.getCnpj()); + } + + static class Teste { + private Cnpj cnpj; + + public Cnpj getCnpj() { + return cnpj; + } + + public void setCnpj(Cnpj cnpj) { + this.cnpj = cnpj; + } + } +} diff --git a/src/test/java/br/com/insula/opes/json/deserializer/CpfJsonDeserializerTest.java b/src/test/java/br/com/insula/opes/json/deserializer/CpfJsonDeserializerTest.java new file mode 100644 index 0000000..0f6a7e0 --- /dev/null +++ b/src/test/java/br/com/insula/opes/json/deserializer/CpfJsonDeserializerTest.java @@ -0,0 +1,52 @@ +package br.com.insula.opes.json.deserializer; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.codehaus.jackson.Version; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.map.module.SimpleModule; +import org.junit.Test; + +import br.com.insula.opes.Cpf; + +public class CpfJsonDeserializerTest { + + @Test + public void testDeserializeJsonParserDeserializationContext() throws Exception { + ObjectMapper mapper = createObjectMapper(); + + String json = "{\"cpf\":\"00000000191\"}"; + Teste teste = mapper.readValue(json, Teste.class); + assertEquals(Cpf.fromString("000.000.001-91"), teste.getCpf()); + } + + private ObjectMapper createObjectMapper() { + ObjectMapper objectMapper = new ObjectMapper(); + SimpleModule module = new SimpleModule("OpesModule", new Version(1, 0, 0, null)); + module.addDeserializer(Cpf.class, new CpfJsonDeserializer()); + objectMapper.registerModule(module); + return objectMapper; + } + + @Test + public void testDeserializeJsonParserDeserializationContextWithNull() throws Exception { + ObjectMapper mapper = createObjectMapper(); + + String json = "{\"cpf\":null}"; + Teste teste = mapper.readValue(json, Teste.class); + assertNull(teste.getCpf()); + } + + static class Teste { + private Cpf cpf; + + public Cpf getCpf() { + return cpf; + } + + public void setCpf(Cpf cpf) { + this.cpf = cpf; + } + } +} diff --git a/src/test/java/br/com/insula/opes/json/serializer/CepJsonSerializerTest.java b/src/test/java/br/com/insula/opes/json/serializer/CepJsonSerializerTest.java new file mode 100644 index 0000000..d05ca9e --- /dev/null +++ b/src/test/java/br/com/insula/opes/json/serializer/CepJsonSerializerTest.java @@ -0,0 +1,39 @@ +package br.com.insula.opes.json.serializer; + +import static org.junit.Assert.assertEquals; + +import java.io.StringWriter; + +import org.codehaus.jackson.Version; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.map.module.SimpleModule; +import org.junit.Test; + +import br.com.insula.opes.Cep; + +public class CepJsonSerializerTest { + + @Test + public void testSerializeCepJsonGeneratorSerializerProvider() throws Exception { + ObjectMapper mapper = new ObjectMapper(); + SimpleModule module = new SimpleModule("OpesModule", new Version(1, 0, 0, null)); + module.addSerializer(Cep.class, new CepJsonSerializer()); + mapper.registerModule(module); + + StringWriter writer = new StringWriter(); + mapper.writeValue(writer, new Teste()); + assertEquals("{\"cep\":\"87030020\"}", writer.toString()); + } + + static class Teste { + private Cep cep = Cep.fromString("87030020"); + + public Cep getCep() { + return cep; + } + + public void setCep(Cep cpfCnpj) { + this.cep = cpfCnpj; + } + } +} diff --git a/src/test/java/br/com/insula/opes/json/serializer/CnpjJsonSerializerTest.java b/src/test/java/br/com/insula/opes/json/serializer/CnpjJsonSerializerTest.java new file mode 100644 index 0000000..bc548f5 --- /dev/null +++ b/src/test/java/br/com/insula/opes/json/serializer/CnpjJsonSerializerTest.java @@ -0,0 +1,39 @@ +package br.com.insula.opes.json.serializer; + +import static org.junit.Assert.assertEquals; + +import java.io.StringWriter; + +import org.codehaus.jackson.Version; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.map.module.SimpleModule; +import org.junit.Test; + +import br.com.insula.opes.Cnpj; + +public class CnpjJsonSerializerTest { + + @Test + public void testSerializeCnpjJsonGeneratorSerializerProvider() throws Exception { + ObjectMapper mapper = new ObjectMapper(); + SimpleModule module = new SimpleModule("OpesModule", new Version(1, 0, 0, null)); + module.addSerializer(Cnpj.class, new CnpjJsonSerializer()); + mapper.registerModule(module); + + StringWriter writer = new StringWriter(); + mapper.writeValue(writer, new Teste()); + assertEquals("{\"cnpj\":\"86266937000129\"}", writer.toString()); + } + + static class Teste { + private Cnpj cnpj = Cnpj.fromString("86.266.937/0001-29"); + + public Cnpj getCnpj() { + return cnpj; + } + + public void setCnpj(Cnpj cnpj) { + this.cnpj = cnpj; + } + } +} diff --git a/src/test/java/br/com/insula/opes/json/serializer/CpfJsonSerializerTest.java b/src/test/java/br/com/insula/opes/json/serializer/CpfJsonSerializerTest.java new file mode 100644 index 0000000..77d1a9d --- /dev/null +++ b/src/test/java/br/com/insula/opes/json/serializer/CpfJsonSerializerTest.java @@ -0,0 +1,39 @@ +package br.com.insula.opes.json.serializer; + +import static org.junit.Assert.assertEquals; + +import java.io.StringWriter; + +import org.codehaus.jackson.Version; +import org.codehaus.jackson.map.ObjectMapper; +import org.codehaus.jackson.map.module.SimpleModule; +import org.junit.Test; + +import br.com.insula.opes.Cpf; + +public class CpfJsonSerializerTest { + + @Test + public void testSerializeCpfJsonGeneratorSerializerProvider() throws Exception { + ObjectMapper mapper = new ObjectMapper(); + SimpleModule module = new SimpleModule("OpesModule", new Version(1, 0, 0, null)); + module.addSerializer(Cpf.class, new CpfJsonSerializer()); + mapper.registerModule(module); + + StringWriter writer = new StringWriter(); + mapper.writeValue(writer, new Teste()); + assertEquals("{\"cpf\":\"00000000191\"}", writer.toString()); + } + + static class Teste { + private Cpf cpf = Cpf.fromString("000.000.001-91"); + + public Cpf getCpf() { + return cpf; + } + + public void setCpf(Cpf cpf) { + this.cpf = cpf; + } + } +}