|
| 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 com.arangodb.springframework.annotation.From; |
| 24 | +import com.arangodb.springframework.annotation.To; |
| 25 | +import com.arangodb.springframework.core.mapping.testdata.BasicEdgeLazyTestEntity; |
| 26 | +import com.arangodb.springframework.core.mapping.testdata.BasicEdgeTestEntity; |
| 27 | +import com.arangodb.springframework.core.mapping.testdata.BasicTestEntity; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 31 | +import static org.hamcrest.Matchers.is; |
| 32 | +import static org.hamcrest.Matchers.notNullValue; |
| 33 | + |
| 34 | +/** |
| 35 | + * @author Mark Vollmary |
| 36 | + * |
| 37 | + */ |
| 38 | +abstract class EdgeMapping extends AbstractMappingTxTestAbstract { |
| 39 | + |
| 40 | + EdgeMapping(boolean withinTx) { |
| 41 | + super(withinTx, EdgeMapping.class.getDeclaredClasses()); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void edgeFromTo() { |
| 46 | + final BasicTestEntity e1 = new BasicTestEntity(); |
| 47 | + template.insert(e1, insertOpts); |
| 48 | + final BasicTestEntity e2 = new BasicTestEntity(); |
| 49 | + template.insert(e2, insertOpts); |
| 50 | + final BasicEdgeTestEntity e0 = new BasicEdgeTestEntity(e1, e2); |
| 51 | + template.insert(e0, insertOpts); |
| 52 | + final BasicEdgeTestEntity document = template.find(e0.id, BasicEdgeTestEntity.class, findOpts).get(); |
| 53 | + assertThat(document, is(notNullValue())); |
| 54 | + assertThat(document.getFrom(), is(notNullValue())); |
| 55 | + assertThat(document.getFrom().getId(), is(e1.getId())); |
| 56 | + assertThat(document.getTo(), is(notNullValue())); |
| 57 | + assertThat(document.getTo().getId(), is(e2.getId())); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void edgeFromToLazy() { |
| 62 | + final BasicTestEntity e1 = new BasicTestEntity(); |
| 63 | + template.insert(e1, insertOpts); |
| 64 | + final BasicTestEntity e2 = new BasicTestEntity(); |
| 65 | + template.insert(e2, insertOpts); |
| 66 | + final BasicEdgeLazyTestEntity e0 = new BasicEdgeLazyTestEntity(e1, e2); |
| 67 | + template.insert(e0, insertOpts); |
| 68 | + final BasicEdgeLazyTestEntity document = template.find(e0.id, BasicEdgeLazyTestEntity.class, findOpts).get(); |
| 69 | + assertThat(document, is(notNullValue())); |
| 70 | + assertThat(document.getFrom(), is(notNullValue())); |
| 71 | + assertThat(document.getFrom().getId(), is(e1.getId())); |
| 72 | + assertThat(document.getTo(), is(notNullValue())); |
| 73 | + assertThat(document.getTo().getId(), is(e2.getId())); |
| 74 | + } |
| 75 | + |
| 76 | + public static class EdgeConstructorWithFromToParamsTestEntity extends BasicEdgeTestEntity { |
| 77 | + @From |
| 78 | + private final BasicTestEntity from; |
| 79 | + @To |
| 80 | + private final BasicTestEntity to; |
| 81 | + |
| 82 | + public EdgeConstructorWithFromToParamsTestEntity(final BasicTestEntity from, final BasicTestEntity to) { |
| 83 | + super(); |
| 84 | + this.from = from; |
| 85 | + this.to = to; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void edgeConstructorWithFromToParams() { |
| 91 | + final BasicTestEntity from = new BasicTestEntity(); |
| 92 | + final BasicTestEntity to = new BasicTestEntity(); |
| 93 | + template.insert(from, insertOpts); |
| 94 | + template.insert(to, insertOpts); |
| 95 | + final EdgeConstructorWithFromToParamsTestEntity edge = new EdgeConstructorWithFromToParamsTestEntity(from, to); |
| 96 | + template.insert(edge, insertOpts); |
| 97 | + final EdgeConstructorWithFromToParamsTestEntity document = template |
| 98 | + .find(edge.id, EdgeConstructorWithFromToParamsTestEntity.class, findOpts).get(); |
| 99 | + assertThat(document, is(notNullValue())); |
| 100 | + assertThat(document.from.id, is(from.id)); |
| 101 | + assertThat(document.to.id, is(to.id)); |
| 102 | + } |
| 103 | + |
| 104 | + public static class EdgeConstructorWithFromToLazyParamsTestEntity extends BasicEdgeTestEntity { |
| 105 | + @From |
| 106 | + private final BasicTestEntity from; |
| 107 | + @To |
| 108 | + private final BasicTestEntity to; |
| 109 | + |
| 110 | + public EdgeConstructorWithFromToLazyParamsTestEntity(final BasicTestEntity from, final BasicTestEntity to) { |
| 111 | + super(); |
| 112 | + this.from = from; |
| 113 | + this.to = to; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void edgeConstructorWithFromToLazyParams() { |
| 119 | + final BasicTestEntity from = new BasicTestEntity(); |
| 120 | + final BasicTestEntity to = new BasicTestEntity(); |
| 121 | + template.insert(from, insertOpts); |
| 122 | + template.insert(to, insertOpts); |
| 123 | + final EdgeConstructorWithFromToLazyParamsTestEntity edge = new EdgeConstructorWithFromToLazyParamsTestEntity( |
| 124 | + from, to); |
| 125 | + template.insert(edge, insertOpts); |
| 126 | + final EdgeConstructorWithFromToLazyParamsTestEntity document = template |
| 127 | + .find(edge.id, EdgeConstructorWithFromToLazyParamsTestEntity.class, findOpts).get(); |
| 128 | + assertThat(document, is(notNullValue())); |
| 129 | + assertThat(document.from.getId(), is(from.id)); |
| 130 | + assertThat(document.to.getId(), is(to.id)); |
| 131 | + } |
| 132 | + |
| 133 | +} |
0 commit comments