Skip to content

Commit

Permalink
fixed mapping of fields that have types annotated by @ProtoClasses
Browse files Browse the repository at this point in the history
  • Loading branch information
kgorozhankin committed Aug 19, 2016
1 parent 4ee7f47 commit b8aad3a
Show file tree
Hide file tree
Showing 9 changed files with 2,218 additions and 479 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package net.badata.protobuf.converter.utils;

import net.badata.protobuf.converter.annotation.ProtoClass;
import net.badata.protobuf.converter.annotation.ProtoClasses;
import net.badata.protobuf.converter.resolver.FieldResolver;

import java.lang.reflect.Field;
Expand All @@ -42,7 +43,8 @@ public final class FieldUtils {
* Check whether field has own mapper.
*
* @param field Testing field.
* @return true if field type has {@link net.badata.protobuf.converter.annotation.ProtoClass} annotation.
* @return true if field type has {@link net.badata.protobuf.converter.annotation.ProtoClass} or
* {@link net.badata.protobuf.converter.annotation.ProtoClasses} annotation.
*/
public static boolean isComplexType(final Field field) {
return isComplexType(field.getType());
Expand All @@ -52,10 +54,11 @@ public static boolean isComplexType(final Field field) {
* Check whether type has own mapper.
*
* @param type Testing type.
* @return true if field type has {@link net.badata.protobuf.converter.annotation.ProtoClass} annotation.
* @return true if field type has {@link net.badata.protobuf.converter.annotation.ProtoClass} or
* {@link net.badata.protobuf.converter.annotation.ProtoClasses} annotation.
*/
public static boolean isComplexType(final Class<?> type) {
return type.isAnnotationPresent(ProtoClass.class);
return type.isAnnotationPresent(ProtoClass.class) || type.isAnnotationPresent(ProtoClasses.class);
}

/**
Expand Down
18 changes: 0 additions & 18 deletions src/test/java/net/badata/protobuf/converter/ConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,22 +214,4 @@ public void testFieldIgnoreDomainToProtobuf() {
Assert.assertTrue(result.getComplexListValueList().isEmpty());
}

@Test
public void testMultiMapping() {
Converter converter = Converter.create();
ConverterProto.MultiMappingTest protobufResult = converter.toProtobuf(ConverterProto.MultiMappingTest.class,
testDomain);

Assert.assertNotNull(protobufResult);
Assert.assertEquals(testDomain.getIntValue(), (Object) protobufResult.getIntValue());
Assert.assertEquals(testDomain.getLongValue(), (Object) protobufResult.getLongValueChanged());

ConverterDomain.Test domainResult = converter.toDomain(ConverterDomain.Test.class, protobufResult);

Assert.assertNotNull(domainResult);
Assert.assertEquals((Object) protobufResult.getIntValue(), domainResult.getIntValue());
Assert.assertEquals((Object) protobufResult.getLongValueChanged(), domainResult.getLongValue());
Assert.assertNull(domainResult.getDoubleValue());
}

}
104 changes: 104 additions & 0 deletions src/test/java/net/badata/protobuf/converter/MultiMappingTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* Copyright (C) 2016 BAData Creative Studio
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/

package net.badata.protobuf.converter;

import net.badata.protobuf.converter.domain.MultiMappingDomain;
import net.badata.protobuf.converter.proto.MultiMappingProto;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.util.Collections;

/**
* Created by jsjem on 19.08.2016.
*
* @author jsjem
*/
public class MultiMappingTest {

private MultiMappingDomain.MultiMappingOwner testDomain;
private MultiMappingProto.MultiMappingTest testProtobuf;


@Before
public void setUp() throws Exception {
createTestProtobuf();
createTestDomain();
}

private void createTestProtobuf() {
testProtobuf = MultiMappingProto.MultiMappingTest.newBuilder()
.setMultiMappingValue(MultiMappingProto.MultiMappingFirst.newBuilder()
.setIntValue(13)
.setLongValue(14))
.addMultiMappingListValue(MultiMappingProto.MultiMappingSecond.newBuilder()
.setIntValue(16)
.setLongValueChanged(17)
.setUnusableValue("Some string"))
.build();
}

private void createTestDomain() {
testDomain = new MultiMappingDomain.MultiMappingOwner();
MultiMappingDomain.MultiMappingChild child = new MultiMappingDomain.MultiMappingChild();
child.setIntValue(31);
child.setLongValue(41);
testDomain.setMultiMappingValue(child);
MultiMappingDomain.MultiMappingChild listChild = new MultiMappingDomain.MultiMappingChild();
listChild.setIntValue(61);
listChild.setLongValue(71);
testDomain.setMultiMappingListValue(Collections.singletonList(listChild));
}

@Test
public void testMultiMapping() {
Converter converter = Converter.create();
MultiMappingProto.MultiMappingTest protobufResult = converter
.toProtobuf(MultiMappingProto.MultiMappingTest.class, testDomain);

Assert.assertNotNull(protobufResult);

MultiMappingProto.MultiMappingFirst multiMappingFirst = protobufResult.getMultiMappingValue();
MultiMappingProto.MultiMappingSecond multiMappingSecond = protobufResult.getMultiMappingListValue(0);

Assert.assertEquals(testDomain.getMultiMappingValue().getIntValue(), (Object) multiMappingFirst.getIntValue());
Assert.assertEquals(testDomain.getMultiMappingValue().getLongValue(),
(Object) multiMappingFirst.getLongValue());

Assert.assertEquals(testDomain.getMultiMappingListValue().get(0).getIntValue(),
(Object) multiMappingSecond.getIntValue());
Assert.assertEquals(testDomain.getMultiMappingListValue().get(0).getLongValue(), (Object) multiMappingSecond
.getLongValueChanged());
Assert.assertEquals("", multiMappingSecond.getUnusableValue());

MultiMappingDomain.MultiMappingOwner domainResult = converter
.toDomain(MultiMappingDomain.MultiMappingOwner.class, testProtobuf);

Assert.assertNotNull(domainResult);

MultiMappingDomain.MultiMappingChild child = domainResult.getMultiMappingValue();
MultiMappingDomain.MultiMappingChild listChild = domainResult.getMultiMappingListValue().get(0);

Assert.assertEquals((Object) testProtobuf.getMultiMappingValue().getIntValue(), child.getIntValue());
Assert.assertEquals((Object) testProtobuf.getMultiMappingValue().getLongValue(), child.getLongValue());
Assert.assertEquals((Object) testProtobuf.getMultiMappingListValue(0).getIntValue(), listChild.getIntValue());
Assert.assertEquals((Object) testProtobuf.getMultiMappingListValue(0).getLongValueChanged(),
listChild.getLongValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.google.protobuf.Message;
import net.badata.protobuf.converter.annotation.ProtoClass;
import net.badata.protobuf.converter.annotation.ProtoClasses;
import net.badata.protobuf.converter.annotation.ProtoField;
import net.badata.protobuf.converter.exception.MappingException;
import net.badata.protobuf.converter.inspection.DefaultValue;
Expand All @@ -27,9 +26,7 @@
*/
public class ConverterDomain {

@ProtoClasses({@ProtoClass(ConverterProto.ConverterTest.class),
@ProtoClass(value = ConverterProto.MultiMappingTest.class, mapper = MultiMappingMapperImpl.class,
fieldFactory = FieldResolverFactoryImpl.class)})
@ProtoClass(ConverterProto.ConverterTest.class)
public static class Test {

@ProtoField
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/*
* Copyright (C) 2016 BAData Creative Studio
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*/

package net.badata.protobuf.converter.domain;

import com.google.protobuf.Message;
import net.badata.protobuf.converter.annotation.ProtoClass;
import net.badata.protobuf.converter.annotation.ProtoClasses;
import net.badata.protobuf.converter.annotation.ProtoField;
import net.badata.protobuf.converter.exception.MappingException;
import net.badata.protobuf.converter.mapping.DefaultMapperImpl;
import net.badata.protobuf.converter.mapping.MappingResult;
import net.badata.protobuf.converter.proto.MultiMappingProto;
import net.badata.protobuf.converter.resolver.AnnotatedFieldResolverFactoryImpl;
import net.badata.protobuf.converter.resolver.DefaultFieldResolverImpl;
import net.badata.protobuf.converter.resolver.FieldResolver;

import java.lang.reflect.Field;
import java.util.List;

/**
* Created by jsjem on 19.08.2016.
*
* @author jsjem
*/
public class MultiMappingDomain {

@ProtoClass(MultiMappingProto.MultiMappingTest.class)
public static class MultiMappingOwner {
@ProtoField
private MultiMappingChild multiMappingValue;
@ProtoField
private List<MultiMappingChild> multiMappingListValue;

public MultiMappingChild getMultiMappingValue() {
return multiMappingValue;
}

public void setMultiMappingValue(final MultiMappingChild multiMappingValue) {
this.multiMappingValue = multiMappingValue;
}

public List<MultiMappingChild> getMultiMappingListValue() {
return multiMappingListValue;
}

public void setMultiMappingListValue(final List<MultiMappingChild> multiMappingListValue) {
this.multiMappingListValue = multiMappingListValue;
}
}

@ProtoClasses({@ProtoClass(MultiMappingProto.MultiMappingFirst.class),
@ProtoClass(value = MultiMappingProto.MultiMappingSecond.class,
mapper = MultiMappingMapperImpl.class,
fieldFactory = FieldResolverFactoryImpl.class)})
public static class MultiMappingChild {
@ProtoField
private int intValue;
@ProtoField
private long longValue;

public int getIntValue() {
return intValue;
}

public void setIntValue(final int intValue) {
this.intValue = intValue;
}

public long getLongValue() {
return longValue;
}

public void setLongValue(final long longValue) {
this.longValue = longValue;
}
}

public static class FieldResolverFactoryImpl extends AnnotatedFieldResolverFactoryImpl {

public static final String FIELD_INT_VALUE = "intValue";
public static final String FIELD_LONG_VALUE = "longValue";

@Override
public FieldResolver createResolver(final Field field) {
if (FIELD_INT_VALUE.equals(field.getName())) {
return super.createResolver(field);
}
if (FIELD_LONG_VALUE.equals(field.getName())) {
DefaultFieldResolverImpl fieldResolver = (DefaultFieldResolverImpl) super.createResolver(field);
fieldResolver.setProtobufName("longValueChanged");
return fieldResolver;
}
return new DefaultFieldResolverImpl(field);
}
}

public static class MultiMappingMapperImpl extends DefaultMapperImpl {

@Override
public <T extends Message.Builder> MappingResult mapToProtobufField(final FieldResolver fieldResolver,
final Object domain, final T protobufBuilder) throws MappingException {
if (FieldResolverFactoryImpl.FIELD_INT_VALUE.equals(fieldResolver.getDomainName()) ||
FieldResolverFactoryImpl.FIELD_LONG_VALUE.equals(fieldResolver.getDomainName())) {
return super.mapToProtobufField(fieldResolver, domain, protobufBuilder);
}
return new MappingResult(MappingResult.Result.MAPPED, null, protobufBuilder);
}

@Override
public <T extends Message> MappingResult mapToDomainField(final FieldResolver fieldResolver, final T
protobuf, final Object domain) throws MappingException {
if (FieldResolverFactoryImpl.FIELD_INT_VALUE.equals(fieldResolver.getDomainName()) ||
FieldResolverFactoryImpl.FIELD_LONG_VALUE.equals(fieldResolver.getDomainName())) {
return super.mapToDomainField(fieldResolver, protobuf, domain);
}
return new MappingResult(MappingResult.Result.MAPPED, null, domain);
}
}
}
Loading

0 comments on commit b8aad3a

Please sign in to comment.