|
| 1 | +/* |
| 2 | + * DISCLAIMER |
| 3 | + * |
| 4 | + * Copyright 2018 ArangoDB GmbH, Cologne, Germany |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + * |
| 18 | + * Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | + */ |
| 20 | + |
| 21 | +package com.arangodb.springframework.core.mapping; |
| 22 | + |
| 23 | +import static org.hamcrest.Matchers.is; |
| 24 | +import static org.hamcrest.Matchers.nullValue; |
| 25 | +import static org.junit.Assert.assertThat; |
| 26 | + |
| 27 | +import java.util.Optional; |
| 28 | + |
| 29 | +import org.junit.Test; |
| 30 | +import org.springframework.core.convert.converter.Converter; |
| 31 | + |
| 32 | +import com.arangodb.entity.BaseDocument; |
| 33 | +import com.arangodb.entity.DocumentEntity; |
| 34 | +import com.arangodb.springframework.AbstractArangoTest; |
| 35 | +import com.arangodb.springframework.annotation.Document; |
| 36 | +import com.arangodb.springframework.core.convert.DBDocumentEntity; |
| 37 | +import com.arangodb.util.MapBuilder; |
| 38 | +import com.arangodb.velocypack.VPackBuilder; |
| 39 | +import com.arangodb.velocypack.VPackSlice; |
| 40 | +import com.arangodb.velocypack.ValueType; |
| 41 | + |
| 42 | +/** |
| 43 | + * @author Mark Vollmary |
| 44 | + * |
| 45 | + */ |
| 46 | +public class CustomMappingTest extends AbstractArangoTest { |
| 47 | + |
| 48 | + private static final String FIELD = "test"; |
| 49 | + |
| 50 | + @Document |
| 51 | + public static class CustomVPackTestEntity { |
| 52 | + private String value; |
| 53 | + |
| 54 | + public CustomVPackTestEntity() { |
| 55 | + super(); |
| 56 | + } |
| 57 | + |
| 58 | + public CustomVPackTestEntity(final String value) { |
| 59 | + super(); |
| 60 | + this.value = value; |
| 61 | + } |
| 62 | + |
| 63 | + public String getValue() { |
| 64 | + return value; |
| 65 | + } |
| 66 | + |
| 67 | + public void setValue(final String value) { |
| 68 | + this.value = value; |
| 69 | + } |
| 70 | + |
| 71 | + } |
| 72 | + |
| 73 | + public static class CustomVPackWriteTestConverter implements Converter<CustomVPackTestEntity, VPackSlice> { |
| 74 | + @Override |
| 75 | + public VPackSlice convert(final CustomVPackTestEntity source) { |
| 76 | + return new VPackBuilder().add(ValueType.OBJECT).add(FIELD, source.getValue()).close().slice(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + public static class CustomVPackReadTestConverter implements Converter<VPackSlice, CustomVPackTestEntity> { |
| 81 | + @Override |
| 82 | + public CustomVPackTestEntity convert(final VPackSlice source) { |
| 83 | + return new CustomVPackTestEntity(source.get(FIELD).getAsString()); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void customToVPack() { |
| 89 | + final DocumentEntity meta = template.insert(new CustomVPackTestEntity("abc")); |
| 90 | + final Optional<BaseDocument> doc = template.find(meta.getId(), BaseDocument.class); |
| 91 | + assertThat(doc.isPresent(), is(true)); |
| 92 | + assertThat(doc.get().getAttribute(FIELD), is("abc")); |
| 93 | + assertThat(doc.get().getAttribute("value"), is(nullValue())); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void vpackToCustom() { |
| 98 | + final DocumentEntity meta = template.insert(new MapBuilder().put(FIELD, "abc").get()); |
| 99 | + final Optional<CustomVPackTestEntity> doc = template.find(meta.getId(), CustomVPackTestEntity.class); |
| 100 | + assertThat(doc.isPresent(), is(true)); |
| 101 | + assertThat(doc.get().getValue(), is("abc")); |
| 102 | + } |
| 103 | + |
| 104 | + @Document |
| 105 | + public static class CustomDBEntityTestEntity { |
| 106 | + private String value; |
| 107 | + |
| 108 | + public CustomDBEntityTestEntity() { |
| 109 | + super(); |
| 110 | + } |
| 111 | + |
| 112 | + public CustomDBEntityTestEntity(final String value) { |
| 113 | + super(); |
| 114 | + this.value = value; |
| 115 | + } |
| 116 | + |
| 117 | + public String getValue() { |
| 118 | + return value; |
| 119 | + } |
| 120 | + |
| 121 | + public void setValue(final String value) { |
| 122 | + this.value = value; |
| 123 | + } |
| 124 | + |
| 125 | + } |
| 126 | + |
| 127 | + public static class CustomDBEntityWriteTestConverter |
| 128 | + implements Converter<CustomDBEntityTestEntity, DBDocumentEntity> { |
| 129 | + @Override |
| 130 | + public DBDocumentEntity convert(final CustomDBEntityTestEntity source) { |
| 131 | + final DBDocumentEntity entity = new DBDocumentEntity(); |
| 132 | + entity.put(FIELD, source.getValue()); |
| 133 | + return entity; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + public static class CustomDBEntityReadTestConverter |
| 138 | + implements Converter<DBDocumentEntity, CustomDBEntityTestEntity> { |
| 139 | + @Override |
| 140 | + public CustomDBEntityTestEntity convert(final DBDocumentEntity source) { |
| 141 | + return new CustomDBEntityTestEntity((String) source.get(FIELD)); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + public void customToDBEntity() { |
| 147 | + final DocumentEntity meta = template.insert(new CustomDBEntityTestEntity("abc")); |
| 148 | + final Optional<BaseDocument> doc = template.find(meta.getId(), BaseDocument.class); |
| 149 | + assertThat(doc.isPresent(), is(true)); |
| 150 | + assertThat(doc.get().getAttribute(FIELD), is("abc")); |
| 151 | + assertThat(doc.get().getAttribute("value"), is(nullValue())); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + public void vpackToDBEntity() { |
| 156 | + final DocumentEntity meta = template.insert(new MapBuilder().put(FIELD, "abc").get()); |
| 157 | + final Optional<CustomDBEntityTestEntity> doc = template.find(meta.getId(), CustomDBEntityTestEntity.class); |
| 158 | + assertThat(doc.isPresent(), is(true)); |
| 159 | + assertThat(doc.get().getValue(), is("abc")); |
| 160 | + } |
| 161 | + |
| 162 | +} |
0 commit comments