forked from rgushel/protobuf-converter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixed mapping of fields that have types annotated by @ProtoClasses
- Loading branch information
kgorozhankin
committed
Aug 19, 2016
1 parent
4ee7f47
commit b8aad3a
Showing
9 changed files
with
2,218 additions
and
479 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
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
104 changes: 104 additions & 0 deletions
104
src/test/java/net/badata/protobuf/converter/MultiMappingTest.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,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()); | ||
} | ||
} |
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
134 changes: 134 additions & 0 deletions
134
src/test/java/net/badata/protobuf/converter/domain/MultiMappingDomain.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,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); | ||
} | ||
} | ||
} |
Oops, something went wrong.