|
| 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.model.DocumentCreateOptions; |
| 24 | +import com.arangodb.model.DocumentReadOptions; |
| 25 | +import com.arangodb.model.StreamTransactionOptions; |
| 26 | +import com.arangodb.springframework.AbstractArangoTest; |
| 27 | +import com.arangodb.springframework.annotation.From; |
| 28 | +import com.arangodb.springframework.annotation.To; |
| 29 | +import com.arangodb.springframework.core.mapping.testdata.BasicEdgeLazyTestEntity; |
| 30 | +import com.arangodb.springframework.core.mapping.testdata.BasicEdgeTestEntity; |
| 31 | +import com.arangodb.springframework.core.mapping.testdata.BasicTestEntity; |
| 32 | +import org.junit.jupiter.api.AfterEach; |
| 33 | +import org.junit.jupiter.api.BeforeEach; |
| 34 | +import org.junit.jupiter.api.Test; |
| 35 | + |
| 36 | +import java.util.Arrays; |
| 37 | +import java.util.HashSet; |
| 38 | +import java.util.Set; |
| 39 | + |
| 40 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 41 | +import static org.hamcrest.Matchers.is; |
| 42 | +import static org.hamcrest.Matchers.notNullValue; |
| 43 | + |
| 44 | +/** |
| 45 | + * @author Mark Vollmary |
| 46 | + * |
| 47 | + */ |
| 48 | +public class EdgeMappingTxTest extends AbstractArangoTest { |
| 49 | + |
| 50 | + private DocumentCreateOptions insertOpts; |
| 51 | + private DocumentReadOptions findOpts; |
| 52 | + |
| 53 | + @BeforeEach |
| 54 | + void beginTx() { |
| 55 | + Set<Class<?>> cols = new HashSet<>(Arrays.asList(getClass().getDeclaredClasses())); |
| 56 | + cols.add(BasicTestEntity.class); |
| 57 | + cols.add(BasicEdgeLazyTestEntity.class); |
| 58 | + cols.add(BasicEdgeTestEntity.class); |
| 59 | + |
| 60 | + String[] txCols = cols.stream() |
| 61 | + .map(it -> template.collection(it).name()) |
| 62 | + .toArray(String[]::new); |
| 63 | + |
| 64 | + String tx = db.beginStreamTransaction(new StreamTransactionOptions() |
| 65 | + .readCollections(txCols) |
| 66 | + .writeCollections(txCols) |
| 67 | + ).getId(); |
| 68 | + |
| 69 | + insertOpts = new DocumentCreateOptions().streamTransactionId(tx); |
| 70 | + findOpts = new DocumentReadOptions().streamTransactionId(tx); |
| 71 | + } |
| 72 | + |
| 73 | + @AfterEach |
| 74 | + void abortTx() { |
| 75 | + db.abortStreamTransaction(insertOpts.getStreamTransactionId()); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void edgeFromTo() { |
| 80 | + final BasicTestEntity e1 = new BasicTestEntity(); |
| 81 | + template.insert(e1, insertOpts); |
| 82 | + final BasicTestEntity e2 = new BasicTestEntity(); |
| 83 | + template.insert(e2, insertOpts); |
| 84 | + final BasicEdgeTestEntity e0 = new BasicEdgeTestEntity(e1, e2); |
| 85 | + template.insert(e0, insertOpts); |
| 86 | + final BasicEdgeTestEntity document = template.find(e0.id, BasicEdgeTestEntity.class, findOpts).get(); |
| 87 | + assertThat(document, is(notNullValue())); |
| 88 | + assertThat(document.getFrom(), is(notNullValue())); |
| 89 | + assertThat(document.getFrom().getId(), is(e1.getId())); |
| 90 | + assertThat(document.getTo(), is(notNullValue())); |
| 91 | + assertThat(document.getTo().getId(), is(e2.getId())); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void edgeFromToLazy() { |
| 96 | + final BasicTestEntity e1 = new BasicTestEntity(); |
| 97 | + template.insert(e1, insertOpts); |
| 98 | + final BasicTestEntity e2 = new BasicTestEntity(); |
| 99 | + template.insert(e2, insertOpts); |
| 100 | + final BasicEdgeLazyTestEntity e0 = new BasicEdgeLazyTestEntity(e1, e2); |
| 101 | + template.insert(e0, insertOpts); |
| 102 | + final BasicEdgeLazyTestEntity document = template.find(e0.id, BasicEdgeLazyTestEntity.class, findOpts).get(); |
| 103 | + assertThat(document, is(notNullValue())); |
| 104 | + assertThat(document.getFrom(), is(notNullValue())); |
| 105 | + assertThat(document.getFrom().getId(), is(e1.getId())); |
| 106 | + assertThat(document.getTo(), is(notNullValue())); |
| 107 | + assertThat(document.getTo().getId(), is(e2.getId())); |
| 108 | + } |
| 109 | + |
| 110 | + public static class EdgeConstructorWithFromToParamsTestEntity extends BasicEdgeTestEntity { |
| 111 | + @From |
| 112 | + private final BasicTestEntity from; |
| 113 | + @To |
| 114 | + private final BasicTestEntity to; |
| 115 | + |
| 116 | + public EdgeConstructorWithFromToParamsTestEntity(final BasicTestEntity from, final BasicTestEntity to) { |
| 117 | + super(); |
| 118 | + this.from = from; |
| 119 | + this.to = to; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void edgeConstructorWithFromToParams() { |
| 125 | + final BasicTestEntity from = new BasicTestEntity(); |
| 126 | + final BasicTestEntity to = new BasicTestEntity(); |
| 127 | + template.insert(from, insertOpts); |
| 128 | + template.insert(to, insertOpts); |
| 129 | + final EdgeConstructorWithFromToParamsTestEntity edge = new EdgeConstructorWithFromToParamsTestEntity(from, to); |
| 130 | + template.insert(edge, insertOpts); |
| 131 | + final EdgeConstructorWithFromToParamsTestEntity document = template |
| 132 | + .find(edge.id, EdgeConstructorWithFromToParamsTestEntity.class, findOpts).get(); |
| 133 | + assertThat(document, is(notNullValue())); |
| 134 | + assertThat(document.from.id, is(from.id)); |
| 135 | + assertThat(document.to.id, is(to.id)); |
| 136 | + } |
| 137 | + |
| 138 | + public static class EdgeConstructorWithFromToLazyParamsTestEntity extends BasicEdgeTestEntity { |
| 139 | + @From |
| 140 | + private final BasicTestEntity from; |
| 141 | + @To |
| 142 | + private final BasicTestEntity to; |
| 143 | + |
| 144 | + public EdgeConstructorWithFromToLazyParamsTestEntity(final BasicTestEntity from, final BasicTestEntity to) { |
| 145 | + super(); |
| 146 | + this.from = from; |
| 147 | + this.to = to; |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + public void edgeConstructorWithFromToLazyParams() { |
| 153 | + final BasicTestEntity from = new BasicTestEntity(); |
| 154 | + final BasicTestEntity to = new BasicTestEntity(); |
| 155 | + template.insert(from, insertOpts); |
| 156 | + template.insert(to, insertOpts); |
| 157 | + final EdgeConstructorWithFromToLazyParamsTestEntity edge = new EdgeConstructorWithFromToLazyParamsTestEntity( |
| 158 | + from, to); |
| 159 | + template.insert(edge, insertOpts); |
| 160 | + final EdgeConstructorWithFromToLazyParamsTestEntity document = template |
| 161 | + .find(edge.id, EdgeConstructorWithFromToLazyParamsTestEntity.class, findOpts).get(); |
| 162 | + assertThat(document, is(notNullValue())); |
| 163 | + assertThat(document.from.getId(), is(from.id)); |
| 164 | + assertThat(document.to.getId(), is(to.id)); |
| 165 | + } |
| 166 | + |
| 167 | +} |
0 commit comments