Skip to content

Commit

Permalink
Introduce Metamorph 1.1
Browse files Browse the repository at this point in the history
With this version of Metamorph entities (well, entity events) can be passed through.
It is slightly incomaptible with the default Metamorph where two tests would fail:

- org.metafacture.metamorph.collectors.EntityTest > shouldEmitEntityOnEachFlushEvent
- org.metafacture.metamorph.functions.UniqueTest -> shouldAllowSelectingTheUniqueScope

So this introduces a "<version>" element under the "<meta>" element in morph.

The data is flattened, as with Metamorph 1, but the entity's "start"
and "end" events are passed through so that the receiver can handle the flattened
data structure, unflatten it etc.

By preserving the entity events it's now also possible, without any workarounds,
to handle reiterations of entities having the same name.

- add "version" element to metamorph.xsd

See #107.
See also https://github.com/hagbeck/metafacture-sandbox/tree/master/enrich_marcxml.
  • Loading branch information
dr0i committed Jun 2, 2020
1 parent 8b6652f commit ded25a8
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 5 deletions.
6 changes: 4 additions & 2 deletions metamorph-test/src/main/resources/schemata/metamorph.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@
<complexType>
<sequence>
<element ref="tns:name" maxOccurs="1" minOccurs="0" />
<element ref="tns:version" maxOccurs="1" minOccurs="0" />
<element ref="tns:annotation" maxOccurs="1" minOccurs="0" />
</sequence>
</complexType>
</element>

<element name="name" type="string" />
<element name="version" type="string" />
<element name="annotation" type="string" />

<element name="macros">
Expand Down Expand Up @@ -302,9 +304,9 @@
<attribute name="value" type="string" use="required" />

<attribute name="reset" type="boolean" use="optional"
default="false" />
default="false" />
<attribute name="sameEntity" type="boolean" use="optional"
default="false" />
default="false" />
<attribute name="flushWith" type="string" use="optional" />
</complexType>
</element>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ public void startEntity(final String name) {
entityCountStack.push(Integer.valueOf(entityCount));

flattener.startEntity(name);


if (maps.containsKey(METADATA) && maps.get(METADATA).getOrDefault("version","").equals("1.1"))
outputStreamReceiver.startEntity(name);

}

Expand All @@ -278,7 +278,8 @@ public void endEntity() {
dispatch(flattener.getCurrentPath(), "", null);
currentEntityCount = entityCountStack.pop().intValue();
flattener.endEntity();

if (maps.containsKey(METADATA) && maps.get(METADATA).getOrDefault("version","").equals("1.1"))
outputStreamReceiver.endEntity();
}


Expand Down
2 changes: 2 additions & 0 deletions metamorph/src/main/resources/schemata/metamorph.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@
<complexType>
<sequence>
<element ref="tns:name" maxOccurs="1" minOccurs="0" />
<element ref="tns:version" maxOccurs="1" minOccurs="0" />
<element ref="tns:annotation" maxOccurs="1" minOccurs="0" />
</sequence>
</complexType>
</element>

<element name="name" type="string" />
<element name="version" type="string" />
<element name="annotation" type="string" />

<element name="macros">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;

Expand All @@ -30,9 +31,11 @@
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.metafacture.framework.StreamReceiver;
import org.metafacture.framework.helpers.DefaultStreamReceiver;
import org.metafacture.metamorph.api.Maps;
import org.metafacture.metamorph.api.NamedValueReceiver;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
Expand All @@ -45,13 +48,17 @@
* @author Christoph Böhme (rewrite)
*/
public final class MetamorphTest {
StringBuilder resultCollector=new StringBuilder();

@Rule
public MockitoRule mockitoRule = MockitoJUnit.rule();

@Mock
private NamedValueReceiver namedValueReceiver;

@Mock
private StreamReceiver receiver;

private Metamorph metamorph;

@Before
Expand Down Expand Up @@ -189,4 +196,57 @@ public void shouldThrowIllegalStateExceptionIfEntityIsNotClosed() {
metamorph.endRecord(); // Exception expected
}

@Test
public void metamorph1() {
metamorph = InlineMorph.in(this) //
.with("<rules>") //
.with(" <data source='_else'/>")//
.with("</rules>")//
.createConnectedTo(receiver);

metamorph.startRecord("1");
metamorph.startEntity("clone");
metamorph.literal("id", "0");
metamorph.endEntity();
metamorph.startEntity("clone");
metamorph.literal("id", "1");
metamorph.endEntity();
metamorph.endRecord();

final InOrder ordered = inOrder(receiver);
ordered.verify(receiver).startRecord("1");
ordered.verify(receiver).literal("clone.id", "0");
ordered.verify(receiver).literal("clone.id", "1");
ordered.verify(receiver).endRecord();
}

@Test
public void metamorph1_1() {
metamorph = InlineMorph.in(this).with("<meta>") //
.with("<version>1.1</version>")//
.with("</meta>")//
.with("<rules>")//
.with(" <data source='_else'/>")//
.with("</rules>")//
.createConnectedTo(receiver);

metamorph.startRecord("1");
metamorph.startEntity("clone");
metamorph.literal("id", "0");
metamorph.endEntity();
metamorph.startEntity("clone");
metamorph.literal("id", "1");
metamorph.endEntity();
metamorph.endRecord();

final InOrder ordered = inOrder(receiver);
ordered.verify(receiver).startRecord("1");
ordered.verify(receiver).startEntity("clone");
ordered.verify(receiver).literal("clone.id", "0");
ordered.verify(receiver).endEntity();
ordered.verify(receiver).startEntity("clone");
ordered.verify(receiver).literal("clone.id", "1");
ordered.verify(receiver).endEntity();
ordered.verify(receiver).endRecord();
}
}

0 comments on commit ded25a8

Please sign in to comment.