Skip to content

Commit 2d4f976

Browse files
authored
Merge pull request #351 from metafacture/347-findingPathModul
2 parents 747c4ce + 7e3c150 commit 2d4f976

File tree

3 files changed

+190
-0
lines changed

3 files changed

+190
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2024 Tobias Bülte, 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.formatting.ObjectTemplate;
20+
import org.metafacture.framework.FluxCommand;
21+
import org.metafacture.framework.MetafactureException;
22+
import org.metafacture.framework.ObjectReceiver;
23+
import org.metafacture.framework.StreamReceiver;
24+
import org.metafacture.framework.annotations.Description;
25+
import org.metafacture.framework.annotations.In;
26+
import org.metafacture.framework.annotations.Out;
27+
import org.metafacture.framework.helpers.DefaultStreamPipe;
28+
import org.metafacture.mangling.StreamFlattener;
29+
import org.metafacture.triples.StreamToTriples;
30+
import org.metafacture.triples.TripleFilter;
31+
32+
import java.io.IOException;
33+
34+
/**
35+
* Provide a user-friendly way to finds all paths that have values that match
36+
* the given pattern.
37+
*
38+
* @author Tobias Bülte
39+
*/
40+
@Description("Finds all paths that have values that match the given pattern. Allows for regex. These paths can be used in a Fix to address fields.")
41+
@In(StreamReceiver.class)
42+
@Out(String.class)
43+
@FluxCommand("find-fix-paths")
44+
45+
public class FindFixPaths extends DefaultStreamPipe<ObjectReceiver<String>> {
46+
private final Metafix fix;
47+
private String objectPattern;
48+
49+
public FindFixPaths(final String objectPattern) {
50+
this.objectPattern = objectPattern;
51+
try {
52+
this.fix = new Metafix("nothing()");
53+
this.fix.setRepeatedFieldsToEntities(true);
54+
}
55+
catch (final IOException e) {
56+
throw new MetafactureException(e);
57+
}
58+
}
59+
60+
@Override
61+
protected void onSetReceiver() {
62+
final TripleFilter tripleFilter = new TripleFilter();
63+
tripleFilter.setObjectPattern(objectPattern);
64+
fix
65+
.setReceiver(new StreamFlattener())
66+
.setReceiver(new StreamToTriples())
67+
.setReceiver(tripleFilter)
68+
.setReceiver(new ObjectTemplate<>("${p}\\t|\\t${o}"))
69+
.setReceiver(getReceiver());
70+
}
71+
72+
@Override
73+
public void startRecord(final String identifier) {
74+
fix.startRecord(identifier);
75+
}
76+
77+
@Override
78+
public void endRecord() {
79+
fix.endRecord();
80+
}
81+
82+
@Override
83+
public void startEntity(final String name) {
84+
fix.startEntity(name);
85+
}
86+
87+
@Override
88+
public void endEntity() {
89+
fix.endEntity();
90+
}
91+
92+
@Override
93+
public void literal(final String name, final String value) {
94+
fix.literal(name, value);
95+
}
96+
97+
@Override
98+
protected void onCloseStream() {
99+
fix.closeStream();
100+
}
101+
102+
@Override
103+
protected void onResetStream() {
104+
fix.resetStream();
105+
}
106+
}

metafix/src/main/resources/flux-commands.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@
1515
fix org.metafacture.metafix.Metafix
1616
list-fix-paths org.metafacture.metafix.ListFixPaths
1717
list-fix-values org.metafacture.metafix.ListFixValues
18+
find-fix-paths org.metafacture.metafix.FindFixPaths
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2024 Tobias Bülte, 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.ObjectReceiver;
20+
21+
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.api.extension.ExtendWith;
23+
import org.mockito.InOrder;
24+
import org.mockito.Mock;
25+
import org.mockito.Mockito;
26+
import org.mockito.exceptions.base.MockitoAssertionError;
27+
import org.mockito.junit.jupiter.MockitoExtension;
28+
29+
/**
30+
* Tests for class {@link FindFixPaths}.
31+
*
32+
* @author Tobias Bülte
33+
*
34+
*/
35+
@ExtendWith(MockitoExtension.class)
36+
public final class FindFixPathsTest {
37+
38+
private final FindFixPaths finder = new FindFixPaths(".*ETL.*");
39+
40+
@Mock
41+
private ObjectReceiver<String> receiver;
42+
43+
public FindFixPathsTest() {
44+
}
45+
46+
@Test
47+
public void testShouldFindPaths() {
48+
verify(
49+
"a\\t|\\tAn ETL test",
50+
"c.2\\t|\\tETL what?");
51+
}
52+
53+
private void processRecord() {
54+
finder.setReceiver(receiver);
55+
finder.startRecord("");
56+
finder.literal("a", "An ETL test");
57+
finder.literal("b", "");
58+
finder.literal("b", "Dummi");
59+
finder.literal("b", "Dog");
60+
finder.literal("c", "");
61+
finder.literal("c", "ETL what?");
62+
finder.endRecord();
63+
finder.closeStream();
64+
}
65+
66+
private void verify(final String... result) throws MockitoAssertionError {
67+
processRecord();
68+
try {
69+
final InOrder ordered = Mockito.inOrder(receiver);
70+
for (final String r : result) {
71+
ordered.verify(receiver).process(r);
72+
}
73+
ordered.verify(receiver, Mockito.times(2)).closeStream();
74+
ordered.verifyNoMoreInteractions();
75+
Mockito.verifyNoMoreInteractions(receiver);
76+
}
77+
catch (final MockitoAssertionError e) {
78+
System.out.println(Mockito.mockingDetails(receiver).printInvocations());
79+
throw e;
80+
}
81+
}
82+
83+
}

0 commit comments

Comments
 (0)