|
| 1 | +/* |
| 2 | + * Copyright 2023 Fabian Steeg, hbz |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 the "License"; |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.metafacture.metafix; |
| 18 | + |
| 19 | +import org.metafacture.framework.StreamReceiver; |
| 20 | + |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 23 | +import org.mockito.Mock; |
| 24 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 25 | + |
| 26 | +import java.util.Arrays; |
| 27 | + |
| 28 | +/** |
| 29 | + * Tests Metafix custom configuration options. |
| 30 | + * |
| 31 | + * @author Fabian Steeg |
| 32 | + */ |
| 33 | +@ExtendWith(MockitoExtension.class) |
| 34 | +public class MetafixConfigTest { |
| 35 | + |
| 36 | + @Mock |
| 37 | + private StreamReceiver streamReceiver; |
| 38 | + |
| 39 | + public MetafixConfigTest() { |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void setRepeatedFieldsToEntitiesAndSetEntityMemberName() { |
| 44 | + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( |
| 45 | + "nothing()" |
| 46 | + ), |
| 47 | + i -> { |
| 48 | + i.setRepeatedFieldsToEntities(true); |
| 49 | + i.setEntityMemberName("*"); |
| 50 | + |
| 51 | + i.startRecord("1"); |
| 52 | + i.literal("name", "max"); |
| 53 | + i.literal("name", "mo"); |
| 54 | + i.endRecord(); |
| 55 | + }, |
| 56 | + o -> { |
| 57 | + o.get().startRecord("1"); |
| 58 | + o.get().startEntity("name"); |
| 59 | + o.get().literal("*", "max"); |
| 60 | + o.get().literal("*", "mo"); |
| 61 | + o.get().endEntity(); |
| 62 | + o.get().endRecord(); |
| 63 | + } |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void setRepeatedFieldsToEntitiesAndSetEntityMemberNameWithNumericalSubfield() { |
| 69 | + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( |
| 70 | + "nothing()" |
| 71 | + ), |
| 72 | + i -> { |
| 73 | + i.setRepeatedFieldsToEntities(true); |
| 74 | + i.setEntityMemberName("*"); |
| 75 | + |
| 76 | + i.startRecord("1"); |
| 77 | + i.startEntity("1001 "); |
| 78 | + i.literal("1", "max"); |
| 79 | + i.literal("1", "mo"); |
| 80 | + i.endEntity(); |
| 81 | + i.endRecord(); |
| 82 | + }, |
| 83 | + (o, f) -> { |
| 84 | + o.get().startRecord("1"); |
| 85 | + o.get().startEntity("1001 "); |
| 86 | + o.get().startEntity("1"); |
| 87 | + o.get().literal("*", "max"); |
| 88 | + o.get().literal("*", "mo"); |
| 89 | + f.apply(2).endEntity(); |
| 90 | + o.get().endRecord(); |
| 91 | + } |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void setRepeatedFieldsToEntitiesWithNumericalSubfields() { |
| 97 | + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( |
| 98 | + "nothing()" |
| 99 | + ), |
| 100 | + i -> { |
| 101 | + i.setRepeatedFieldsToEntities(true); |
| 102 | + |
| 103 | + i.startRecord("1"); |
| 104 | + i.startEntity("1001 "); |
| 105 | + i.literal("1", "max"); |
| 106 | + i.literal("1", "mo"); |
| 107 | + i.endEntity(); |
| 108 | + i.endRecord(); |
| 109 | + }, |
| 110 | + (o, f) -> { |
| 111 | + o.get().startRecord("1"); |
| 112 | + o.get().startEntity("1001 "); |
| 113 | + o.get().startEntity("1"); |
| 114 | + o.get().literal("1", "max"); |
| 115 | + o.get().literal("2", "mo"); |
| 116 | + f.apply(2).endEntity(); |
| 117 | + o.get().endRecord(); |
| 118 | + } |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void setEntityMemberNameNoArrayMarkerOrEntity() { |
| 124 | + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( |
| 125 | + "nothing()" |
| 126 | + ), |
| 127 | + i -> { |
| 128 | + i.setEntityMemberName("*"); // no arrays or entities, no effect |
| 129 | + |
| 130 | + i.startRecord("1"); |
| 131 | + i.startEntity("1001 "); |
| 132 | + i.literal("1", "max"); |
| 133 | + i.literal("1", "mo"); |
| 134 | + i.endEntity(); |
| 135 | + i.endRecord(); |
| 136 | + }, |
| 137 | + o -> { |
| 138 | + o.get().startRecord("1"); |
| 139 | + o.get().startEntity("1001 "); |
| 140 | + o.get().literal("1", "max"); |
| 141 | + o.get().literal("1", "mo"); |
| 142 | + o.get().endEntity(); |
| 143 | + o.get().endRecord(); |
| 144 | + } |
| 145 | + ); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void setEntityMemberNameWithArrayMarker() { |
| 150 | + MetafixTestHelpers.assertFix(streamReceiver, Arrays.asList( |
| 151 | + "nothing()" |
| 152 | + ), |
| 153 | + i -> { |
| 154 | + i.setEntityMemberName("*"); |
| 155 | + |
| 156 | + i.startRecord("1"); |
| 157 | + i.startEntity("1001 []"); |
| 158 | + i.literal("1", "max"); |
| 159 | + i.literal("1", "mo"); |
| 160 | + i.endEntity(); |
| 161 | + i.endRecord(); |
| 162 | + }, |
| 163 | + o -> { |
| 164 | + o.get().startRecord("1"); |
| 165 | + o.get().startEntity("1001 []"); |
| 166 | + o.get().literal("*", "max"); |
| 167 | + o.get().literal("*", "mo"); |
| 168 | + o.get().endEntity(); |
| 169 | + o.get().endRecord(); |
| 170 | + } |
| 171 | + ); |
| 172 | + } |
| 173 | + |
| 174 | +} |
0 commit comments