Revision 33519
Added by Marek Horst almost 10 years ago
modules/icm-iis-core/tags/icm-iis-core-1.0.0/deploy.info | ||
---|---|---|
1 |
[ |
|
2 |
{ |
|
3 |
"type_source": "SVN", |
|
4 |
"goal": "package -U -T 4C source:jar", |
|
5 |
"url": "http://svn-public.driver.research-infrastructures.eu/driver/dnet40/modules/icm-iis-core/trunk/", |
|
6 |
"deploy_repository": "dnet4-snapshots", |
|
7 |
"version": "4", |
|
8 |
"mail": "m.horst@icm.edu.pl", |
|
9 |
"deploy_repository_url": "http://maven.research-infrastructures.eu/nexus/content/repositories/dnet4-snapshots", |
|
10 |
"name": "icm-iis-core" |
|
11 |
}, |
|
12 |
{ |
|
13 |
"type_source": "SVN", |
|
14 |
"goal": "clean verify -U -e -X", |
|
15 |
"url": "http://svn-public.driver.research-infrastructures.eu/driver/dnet40/modules/icm-iis-core/trunk/", |
|
16 |
"nightly" : "true", |
|
17 |
"cron" : "H H * * *", |
|
18 |
"version": "4", |
|
19 |
"mail": "m.horst@icm.edu.pl", |
|
20 |
"name": "icm-iis-core-embedded-integration-test" |
|
21 |
} |
|
22 |
] |
modules/icm-iis-core/tags/icm-iis-core-1.0.0/src/test/java/eu/dnetlib/iis/core/OozieTestsIOUtils.java | ||
---|---|---|
1 |
package eu.dnetlib.iis.core; |
|
2 |
|
|
3 |
import java.io.DataInputStream; |
|
4 |
import java.io.DataOutputStream; |
|
5 |
import java.io.File; |
|
6 |
import java.io.FileInputStream; |
|
7 |
import java.io.FileOutputStream; |
|
8 |
import java.io.IOException; |
|
9 |
import java.io.InputStream; |
|
10 |
import java.io.OutputStream; |
|
11 |
|
|
12 |
import org.apache.hadoop.conf.Configuration; |
|
13 |
import org.apache.hadoop.fs.FileSystem; |
|
14 |
import org.apache.hadoop.fs.Path; |
|
15 |
import org.apache.oozie.util.IOUtils; |
|
16 |
|
|
17 |
/** |
|
18 |
* Provides Oozie-related IO methods |
|
19 |
* @author Mateusz Kobos |
|
20 |
* |
|
21 |
*/ |
|
22 |
public class OozieTestsIOUtils { |
|
23 |
private FileSystem fs; |
|
24 |
private final static String confStorageDirProperty = "hadoop.log.dir"; |
|
25 |
private final static String confStorageFileName = "shared_configuration.conf"; |
|
26 |
|
|
27 |
public OozieTestsIOUtils(FileSystem fs) { |
|
28 |
this.fs = fs; |
|
29 |
} |
|
30 |
|
|
31 |
public void copyResourceToHDFS(String resourcePath, Path destination) |
|
32 |
throws IOException { |
|
33 |
InputStream in = IOUtils.getResourceAsStream(resourcePath, -1); |
|
34 |
OutputStream out = fs.create(destination); |
|
35 |
IOUtils.copyStream(in, out); |
|
36 |
} |
|
37 |
|
|
38 |
public void copyLocalToHDFS(File source, Path destination) |
|
39 |
throws IOException { |
|
40 |
Path srcPath = new Path(source.getAbsolutePath()); |
|
41 |
fs.copyFromLocalFile(srcPath, destination); |
|
42 |
} |
|
43 |
|
|
44 |
// public void copyHDFSToLocal(Path source, File destination) |
|
45 |
// throws IOException{ |
|
46 |
// InputStream in = fs.open(source); |
|
47 |
// OutputStream out = new FileOutputStream(destination); |
|
48 |
// IOUtils.copyStream(in, out); |
|
49 |
// } |
|
50 |
|
|
51 |
/** Saves the configuration in a local working directory created by |
|
52 |
* Oozie test case for given test method. This method should be called |
|
53 |
* from the inside of an Oozie test case method, otherwise its functioning |
|
54 |
* is undefined. The saved information can be retrieved by calling |
|
55 |
* {@link loadConfiguration()}. |
|
56 |
* |
|
57 |
* @param conf configuration that should be retrieved from the inside of |
|
58 |
* the Oozie test case method by calling {@link createJobConf()} |
|
59 |
* @throws IOException |
|
60 |
*/ |
|
61 |
public static void saveConfiguration(Configuration conf) throws IOException{ |
|
62 |
File f = new File(System.getProperty(confStorageDirProperty), |
|
63 |
confStorageFileName); |
|
64 |
DataOutputStream out = new DataOutputStream(new FileOutputStream(f)); |
|
65 |
conf.write(out); |
|
66 |
out.close(); |
|
67 |
} |
|
68 |
|
|
69 |
/** A counterpart of the {@link saveConfiguration()} method. It should be |
|
70 |
* called from the inside of a code run on the Hadoop created |
|
71 |
* by Oozie test case. |
|
72 |
* |
|
73 |
* @return |
|
74 |
* @throws IOException |
|
75 |
*/ |
|
76 |
public static Configuration loadConfiguration() throws IOException{ |
|
77 |
File f = new File(System.getProperty(confStorageDirProperty), |
|
78 |
confStorageFileName); |
|
79 |
DataInputStream in = new DataInputStream(new FileInputStream(f)); |
|
80 |
Configuration conf = new Configuration(); |
|
81 |
conf.readFields(in); |
|
82 |
in.close(); |
|
83 |
return conf; |
|
84 |
} |
|
85 |
|
|
86 |
} |
modules/icm-iis-core/tags/icm-iis-core-1.0.0/src/test/java/eu/dnetlib/iis/core/java/CmdLineParserForProcessRunParametersTest.java | ||
---|---|---|
1 |
package eu.dnetlib.iis.core.java; |
|
2 |
|
|
3 |
import static org.junit.Assert.assertEquals; |
|
4 |
|
|
5 |
import java.net.URISyntaxException; |
|
6 |
import java.util.HashMap; |
|
7 |
import java.util.Map; |
|
8 |
|
|
9 |
import org.apache.commons.cli.CommandLine; |
|
10 |
import org.apache.hadoop.fs.Path; |
|
11 |
import org.junit.Test; |
|
12 |
|
|
13 |
import eu.dnetlib.iis.core.java.CmdLineParserException; |
|
14 |
import eu.dnetlib.iis.core.java.CmdLineParserForProcessRunParameters; |
|
15 |
import eu.dnetlib.iis.core.java.PortBindings; |
|
16 |
import eu.dnetlib.iis.core.java.Ports; |
|
17 |
import eu.dnetlib.iis.core.java.ProcessParameters; |
|
18 |
import eu.dnetlib.iis.core.java.porttype.AnyPortType; |
|
19 |
import eu.dnetlib.iis.core.java.porttype.PortType; |
|
20 |
|
|
21 |
/** |
|
22 |
* |
|
23 |
* @author Mateusz Kobos |
|
24 |
* |
|
25 |
*/ |
|
26 |
public class CmdLineParserForProcessRunParametersTest{ |
|
27 |
|
|
28 |
@Test |
|
29 |
public void testBasic() throws URISyntaxException { |
|
30 |
CmdLineParserForProcessRunParameters parser = |
|
31 |
new CmdLineParserForProcessRunParameters(); |
|
32 |
String[] args = new String[]{ |
|
33 |
"-Iperson=/users/joe/person_input", |
|
34 |
"-Idocument=/users/joe/doc_input", |
|
35 |
"-Omerged=/users/joe/merged_out", |
|
36 |
"-SworkingDir=/users/joe/working_dir", |
|
37 |
"-Page=33"}; |
|
38 |
Ports ports = createStandardPorts(); |
|
39 |
CommandLine cmdLine = CmdLineParser.parse(args); |
|
40 |
ProcessParameters actual = parser.run(cmdLine, ports); |
|
41 |
ProcessParameters expected = createStandardCmdLineParams(); |
|
42 |
|
|
43 |
assertEquals(expected, actual); |
|
44 |
} |
|
45 |
|
|
46 |
@Test(expected=CmdLineParserException.class) |
|
47 |
public void testTooManyPorts() { |
|
48 |
CmdLineParserForProcessRunParameters parser = |
|
49 |
new CmdLineParserForProcessRunParameters(); |
|
50 |
String[] args = new String[]{ |
|
51 |
"-Iperson=/users/joe/person_input", |
|
52 |
"-Idocument=/users/joe/doc_input", |
|
53 |
"-Omerged=/users/joe/merged_out", |
|
54 |
"-Oother=/users/joe/other_out", |
|
55 |
"-SworkingDir=/users/joe/working_dir"}; |
|
56 |
Ports ports = createStandardPorts(); |
|
57 |
CommandLine cmdLine = CmdLineParser.parse(args); |
|
58 |
parser.run(cmdLine, ports); |
|
59 |
} |
|
60 |
|
|
61 |
@Test(expected=CmdLineParserException.class) |
|
62 |
public void testTooFewPorts() { |
|
63 |
CmdLineParserForProcessRunParameters parser = |
|
64 |
new CmdLineParserForProcessRunParameters(); |
|
65 |
String[] args = new String[]{ |
|
66 |
"-Iperson=/users/joe/person_input", |
|
67 |
"-Omerged=/users/joe/merged_out", |
|
68 |
"-SworkingDir=/users/joe/working_dir"}; |
|
69 |
Ports ports = createStandardPorts(); |
|
70 |
CommandLine cmdLine = CmdLineParser.parse(args); |
|
71 |
parser.run(cmdLine, ports); |
|
72 |
} |
|
73 |
|
|
74 |
@Test(expected=CmdLineParserException.class) |
|
75 |
public void testMissingWorkingDir() throws URISyntaxException{ |
|
76 |
CmdLineParserForProcessRunParameters parser = |
|
77 |
new CmdLineParserForProcessRunParameters(); |
|
78 |
String[] args = new String[]{ |
|
79 |
"-Iperson=/users/joe/person_input", |
|
80 |
"-Idocument=/users/joe/doc_input", |
|
81 |
"-Omerged=/users/joe/merged_out"}; |
|
82 |
Ports ports = createStandardPorts(); |
|
83 |
CommandLine cmdLine = CmdLineParser.parse(args); |
|
84 |
parser.run(cmdLine, ports); |
|
85 |
} |
|
86 |
|
|
87 |
private static Ports createStandardPorts(){ |
|
88 |
HashMap<String, PortType> inputPorts = |
|
89 |
new HashMap<String, PortType>(); |
|
90 |
inputPorts.put("person", new AnyPortType()); |
|
91 |
inputPorts.put("document", new AnyPortType()); |
|
92 |
HashMap<String, PortType> outputPorts = |
|
93 |
new HashMap<String, PortType>(); |
|
94 |
outputPorts.put("merged", new AnyPortType()); |
|
95 |
return new Ports(inputPorts, outputPorts); |
|
96 |
} |
|
97 |
|
|
98 |
private static ProcessParameters createStandardCmdLineParams() |
|
99 |
throws URISyntaxException{ |
|
100 |
HashMap<String, Path> inputBinding = new HashMap<String, Path>(); |
|
101 |
inputBinding.put("person", |
|
102 |
new Path("/users/joe/person_input")); |
|
103 |
inputBinding.put("document", |
|
104 |
new Path("/users/joe/doc_input")); |
|
105 |
HashMap<String, Path> outputBinding = new HashMap<String, Path>(); |
|
106 |
outputBinding.put("merged", |
|
107 |
new Path("/users/joe/merged_out")); |
|
108 |
PortBindings expectedBindings = |
|
109 |
new PortBindings(inputBinding, outputBinding); |
|
110 |
Path expectedWorkingDir = |
|
111 |
new Path("/users/joe/working_dir"); |
|
112 |
Map<String, String> params = new HashMap<String, String>(); |
|
113 |
params.put("age", "33"); |
|
114 |
ProcessParameters cmdLineParams = new ProcessParameters( |
|
115 |
expectedBindings, expectedWorkingDir, params); |
|
116 |
return cmdLineParams; |
|
117 |
} |
|
118 |
} |
modules/icm-iis-core/tags/icm-iis-core-1.0.0/src/test/java/eu/dnetlib/iis/core/java/io/JsonStreamWriterTest.java | ||
---|---|---|
1 |
package eu.dnetlib.iis.core.java.io; |
|
2 |
|
|
3 |
import java.io.ByteArrayOutputStream; |
|
4 |
import java.io.IOException; |
|
5 |
import java.io.InputStream; |
|
6 |
import java.io.StringWriter; |
|
7 |
import java.util.List; |
|
8 |
|
|
9 |
import org.apache.commons.io.IOUtils; |
|
10 |
import org.junit.Test; |
|
11 |
import junit.framework.Assert; |
|
12 |
|
|
13 |
import eu.dnetlib.iis.core.schemas.standardexamples.Document; |
|
14 |
|
|
15 |
/** |
|
16 |
* @author Mateusz Kobos |
|
17 |
*/ |
|
18 |
public class JsonStreamWriterTest { |
|
19 |
|
|
20 |
@Test |
|
21 |
public void basicTest() throws IOException{ |
|
22 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|
23 |
JsonStreamWriter<Document> writer = |
|
24 |
new JsonStreamWriter<Document>(Document.SCHEMA$, out); |
|
25 |
List<Document> documents = DataStoreExamples.getDocument(); |
|
26 |
for(Document d: documents){ |
|
27 |
writer.write(d); |
|
28 |
} |
|
29 |
writer.close(); |
|
30 |
String actual = out.toString(); |
|
31 |
InputStream in = Thread.currentThread().getContextClassLoader() |
|
32 |
.getResourceAsStream("eu/dnetlib/iis/core/java/io/document.json"); |
|
33 |
String expected = toString(in); |
|
34 |
Assert.assertEquals(expected, actual); |
|
35 |
} |
|
36 |
|
|
37 |
private static String toString(InputStream in) throws IOException{ |
|
38 |
StringWriter writer = new StringWriter(); |
|
39 |
IOUtils.copy(in, writer); |
|
40 |
return writer.toString(); |
|
41 |
} |
|
42 |
} |
modules/icm-iis-core/tags/icm-iis-core-1.0.0/src/test/java/eu/dnetlib/iis/core/java/io/DataStoreTest.java | ||
---|---|---|
1 |
package eu.dnetlib.iis.core.java.io; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
import java.io.IOException; |
|
5 |
import java.util.ArrayList; |
|
6 |
import java.util.Arrays; |
|
7 |
import java.util.List; |
|
8 |
import java.util.NoSuchElementException; |
|
9 |
|
|
10 |
import org.apache.avro.Schema; |
|
11 |
import org.apache.avro.file.DataFileWriter; |
|
12 |
import org.apache.avro.generic.GenericContainer; |
|
13 |
import org.apache.commons.io.FileUtils; |
|
14 |
import org.junit.After; |
|
15 |
import org.junit.Assert; |
|
16 |
import org.junit.Before; |
|
17 |
import org.junit.Test; |
|
18 |
|
|
19 |
import com.google.common.io.Files; |
|
20 |
|
|
21 |
import eu.dnetlib.iis.core.TestsIOUtils; |
|
22 |
import eu.dnetlib.iis.core.schemas.standardexamples.Document; |
|
23 |
import eu.dnetlib.iis.core.schemas.standardexamples.DocumentWithoutTitle; |
|
24 |
import eu.dnetlib.iis.core.java.io.AvroDataStoreReader; |
|
25 |
import eu.dnetlib.iis.core.java.io.DataStore; |
|
26 |
import eu.dnetlib.iis.core.java.io.FileSystemPath; |
|
27 |
|
|
28 |
|
|
29 |
/** |
|
30 |
* @author Mateusz Kobos |
|
31 |
*/ |
|
32 |
public class DataStoreTest { |
|
33 |
|
|
34 |
private File tempDir = null; |
|
35 |
|
|
36 |
@Before |
|
37 |
public void setUp() throws IOException{ |
|
38 |
tempDir = Files.createTempDir(); |
|
39 |
} |
|
40 |
|
|
41 |
@After |
|
42 |
public void tearDown() throws IOException{ |
|
43 |
FileUtils.deleteDirectory(tempDir); |
|
44 |
} |
|
45 |
|
|
46 |
@Test |
|
47 |
public void testSingleFile() throws IOException { |
|
48 |
List<Document> documents = DataStoreExamples.getDocument(); |
|
49 |
FileSystemPath path = new FileSystemPath(new File(tempDir, "doc")); |
|
50 |
DataStore.create(documents, path); |
|
51 |
TestsIOUtils.assertEqualSets(documents, |
|
52 |
new AvroDataStoreReader<Document>(path)); |
|
53 |
} |
|
54 |
|
|
55 |
@Test |
|
56 |
public void testReaderSchema() throws IOException { |
|
57 |
List<Document> documents = DataStoreExamples.getDocument(); |
|
58 |
FileSystemPath path = new FileSystemPath(new File(tempDir, "doc")); |
|
59 |
DataStore.create(documents, path); |
|
60 |
List<DocumentWithoutTitle> documentsWithoutTitle = |
|
61 |
DataStoreExamples.getDocumentWithoutTitle(); |
|
62 |
TestsIOUtils.assertEqualSets(documentsWithoutTitle, |
|
63 |
new AvroDataStoreReader<DocumentWithoutTitle>( |
|
64 |
path, DocumentWithoutTitle.SCHEMA$)); |
|
65 |
} |
|
66 |
|
|
67 |
@Test |
|
68 |
public void testClose() throws IOException{ |
|
69 |
List<Document> documents = DataStoreExamples.getDocument(); |
|
70 |
FileSystemPath path = new FileSystemPath(new File(tempDir, "doc")); |
|
71 |
DataStore.create(documents, path); |
|
72 |
AvroDataStoreReader<Document> reader = |
|
73 |
new AvroDataStoreReader<Document>(path); |
|
74 |
Assert.assertEquals(documents.get(0), reader.next()); |
|
75 |
Assert.assertEquals(documents.get(1), reader.next()); |
|
76 |
reader.close(); |
|
77 |
try{ |
|
78 |
reader.next(); |
|
79 |
Assert.fail("Didn't throw an exception when it supposed to"); |
|
80 |
} catch(NoSuchElementException ex){ |
|
81 |
} |
|
82 |
} |
|
83 |
|
|
84 |
@Test |
|
85 |
public void testManyFiles() throws IOException{ |
|
86 |
List<Document> documents = DataStoreExamples.getDocument(); |
|
87 |
createSingleFile(documents.subList(0, 3), |
|
88 |
new FileSystemPath(new File(tempDir, "doc0")), |
|
89 |
Document.SCHEMA$); |
|
90 |
createSingleFile(documents.subList(3, 4), |
|
91 |
new FileSystemPath(new File(tempDir, "doc1")), |
|
92 |
Document.SCHEMA$); |
|
93 |
|
|
94 |
TestsIOUtils.assertEqualSets(documents, |
|
95 |
new AvroDataStoreReader<Document>( |
|
96 |
new FileSystemPath(tempDir))); |
|
97 |
} |
|
98 |
|
|
99 |
@Test |
|
100 |
public void testEmptyFiles() throws IOException{ |
|
101 |
List<Document> documents = DataStoreExamples.getDocument(); |
|
102 |
|
|
103 |
createSingleFile(documents.subList(0, 3), |
|
104 |
new FileSystemPath(new File(tempDir, "doc0")), |
|
105 |
Document.SCHEMA$); |
|
106 |
|
|
107 |
createSingleFile(Arrays.<Document>asList(), |
|
108 |
new FileSystemPath(new File(tempDir, "doc881")), |
|
109 |
Document.SCHEMA$); |
|
110 |
|
|
111 |
createSingleFile(documents.subList(3, 4), |
|
112 |
new FileSystemPath(new File(tempDir, "doc22")), |
|
113 |
Document.SCHEMA$); |
|
114 |
|
|
115 |
createSingleFile(Arrays.<Document>asList(), |
|
116 |
new FileSystemPath(new File(tempDir, "doc4")), |
|
117 |
Document.SCHEMA$); |
|
118 |
|
|
119 |
TestsIOUtils.assertEqualSets(documents, |
|
120 |
new AvroDataStoreReader<Document>( |
|
121 |
new FileSystemPath(tempDir))); |
|
122 |
} |
|
123 |
|
|
124 |
private static <T extends GenericContainer> void createSingleFile( |
|
125 |
List<T> elements, FileSystemPath path, Schema schema) |
|
126 |
throws IOException{ |
|
127 |
DataFileWriter<T> writer = DataStore.createSingleFile(path, schema); |
|
128 |
for(T i: elements){ |
|
129 |
writer.append(i); |
|
130 |
} |
|
131 |
writer.close(); |
|
132 |
} |
|
133 |
} |
|
134 |
|
|
135 |
class DataStoreExamples{ |
|
136 |
static String bookTitleExtra = "An extraordinary book"; |
|
137 |
static String bookTitleBasics = "Basics of the basics"; |
|
138 |
static String bookTitleMoreInteresting = "Even more of interesting stuff"; |
|
139 |
static String bookTitleEscapeCodes = "2.2. Stellar Rotation Hertszprung\u2013Russell diagram precludes detecting\n6.2. Conclusions\n"; |
|
140 |
|
|
141 |
/** |
|
142 |
* Sample data on some documents |
|
143 |
*/ |
|
144 |
public static List<Document> getDocument(){ |
|
145 |
ArrayList<Document> list = new ArrayList<Document>(); |
|
146 |
list.add(new Document(20, bookTitleExtra, |
|
147 |
new ArrayList<Integer>(Arrays.asList(1, 20)))); |
|
148 |
list.add(new Document(1, bookTitleBasics, |
|
149 |
new ArrayList<Integer>(Arrays.asList(10, 6, 1)))); |
|
150 |
list.add(new Document(2, null, |
|
151 |
new ArrayList<Integer>())); |
|
152 |
list.add(new Document(6, bookTitleMoreInteresting, |
|
153 |
new ArrayList<Integer>(Arrays.asList(1, 6)))); |
|
154 |
return list; |
|
155 |
} |
|
156 |
|
|
157 |
/** |
|
158 |
* The same as the {@link getDocument} but does not include document title |
|
159 |
*/ |
|
160 |
public static List<DocumentWithoutTitle> getDocumentWithoutTitle(){ |
|
161 |
ArrayList<DocumentWithoutTitle> list = new ArrayList<DocumentWithoutTitle>(); |
|
162 |
list.add(new DocumentWithoutTitle(20, |
|
163 |
new ArrayList<Integer>(Arrays.asList(1, 20)))); |
|
164 |
list.add(new DocumentWithoutTitle(1, |
|
165 |
new ArrayList<Integer>(Arrays.asList(10, 6, 1)))); |
|
166 |
list.add(new DocumentWithoutTitle(2, |
|
167 |
new ArrayList<Integer>())); |
|
168 |
list.add(new DocumentWithoutTitle(6, |
|
169 |
new ArrayList<Integer>(Arrays.asList(1, 6)))); |
|
170 |
return list; |
|
171 |
} |
|
172 |
|
|
173 |
public static List<Document> getDocumentWithUnicodeEscapeCodes(){ |
|
174 |
ArrayList<Document> list = new ArrayList<Document>(); |
|
175 |
list.add(new Document(20, bookTitleEscapeCodes, |
|
176 |
new ArrayList<Integer>(Arrays.asList(1, 20)))); |
|
177 |
return list; |
|
178 |
} |
|
179 |
} |
modules/icm-iis-core/tags/icm-iis-core-1.0.0/src/test/java/eu/dnetlib/iis/core/java/io/JsonStreamReaderTest.java | ||
---|---|---|
1 |
package eu.dnetlib.iis.core.java.io; |
|
2 |
|
|
3 |
import java.io.IOException; |
|
4 |
import java.io.InputStream; |
|
5 |
import java.util.ArrayList; |
|
6 |
import java.util.List; |
|
7 |
|
|
8 |
import org.junit.Test; |
|
9 |
|
|
10 |
import eu.dnetlib.iis.core.TestsIOUtils; |
|
11 |
import eu.dnetlib.iis.core.schemas.standardexamples.Document; |
|
12 |
|
|
13 |
/** |
|
14 |
* @author Mateusz Kobos |
|
15 |
*/ |
|
16 |
public class JsonStreamReaderTest { |
|
17 |
|
|
18 |
@Test |
|
19 |
public void basicTest() throws IOException{ |
|
20 |
InputStream in = Thread.currentThread().getContextClassLoader() |
|
21 |
.getResourceAsStream("eu/dnetlib/iis/core/java/io/document.json"); |
|
22 |
CloseableIterator<Document> reader = new JsonStreamReader<Document>( |
|
23 |
Document.SCHEMA$, in, Document.class); |
|
24 |
List<Document> expected = DataStoreExamples.getDocument(); |
|
25 |
List<Document> actual = new ArrayList<Document>(); |
|
26 |
while(reader.hasNext()){ |
|
27 |
Object record = reader.next(); |
|
28 |
Document read = (Document) record; |
|
29 |
actual.add(read); |
|
30 |
} |
|
31 |
TestsIOUtils.assertEqualSets(expected, actual); |
|
32 |
} |
|
33 |
} |
modules/icm-iis-core/tags/icm-iis-core-1.0.0/src/test/java/eu/dnetlib/iis/core/java/io/JsonUtilsTest.java | ||
---|---|---|
1 |
package eu.dnetlib.iis.core.java.io; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
import java.io.IOException; |
|
5 |
import java.io.InputStream; |
|
6 |
import java.util.List; |
|
7 |
|
|
8 |
import junit.framework.Assert; |
|
9 |
|
|
10 |
import org.apache.avro.Schema; |
|
11 |
import org.apache.commons.io.FileUtils; |
|
12 |
import org.apache.commons.io.IOUtils; |
|
13 |
import org.junit.After; |
|
14 |
import org.junit.Before; |
|
15 |
import org.junit.Test; |
|
16 |
|
|
17 |
import com.google.common.io.Files; |
|
18 |
|
|
19 |
import eu.dnetlib.iis.core.TestsIOUtils; |
|
20 |
import eu.dnetlib.iis.core.schemas.standardexamples.Document; |
|
21 |
|
|
22 |
/** |
|
23 |
* |
|
24 |
* @author Mateusz Kobos |
|
25 |
* |
|
26 |
*/ |
|
27 |
public class JsonUtilsTest { |
|
28 |
private File tempDir = null; |
|
29 |
|
|
30 |
@Before |
|
31 |
public void setUp() throws IOException{ |
|
32 |
tempDir = Files.createTempDir(); |
|
33 |
} |
|
34 |
|
|
35 |
@After |
|
36 |
public void tearDown() throws IOException{ |
|
37 |
FileUtils.deleteDirectory(tempDir); |
|
38 |
} |
|
39 |
|
|
40 |
@Test |
|
41 |
public void testConvertToDataStoreSimple() throws IOException{ |
|
42 |
checkConvertToDataStore(DataStoreExamples.getDocument(), |
|
43 |
"eu/dnetlib/iis/core/java/io/document.json", Document.SCHEMA$); |
|
44 |
} |
|
45 |
|
|
46 |
@Test |
|
47 |
public void testConvertToDataStoreWithEscapeCodes() throws IOException{ |
|
48 |
checkConvertToDataStore(DataStoreExamples.getDocumentWithUnicodeEscapeCodes(), |
|
49 |
"eu/dnetlib/iis/core/java/io/document_with_unicode_escape_codes.json", |
|
50 |
Document.SCHEMA$); |
|
51 |
} |
|
52 |
|
|
53 |
private <T> void checkConvertToDataStore(List<T> expectedRecords, |
|
54 |
String actualResourcePath, Schema actualSchema) throws IOException{ |
|
55 |
InputStream in = Thread.currentThread().getContextClassLoader() |
|
56 |
.getResourceAsStream(actualResourcePath); |
|
57 |
FileSystemPath outPath = new FileSystemPath(new File(tempDir, "record")); |
|
58 |
JsonUtils.convertToDataStore(actualSchema, in, outPath); |
|
59 |
TestsIOUtils.assertEqualSets(expectedRecords, |
|
60 |
new AvroDataStoreReader<T>(outPath)); |
|
61 |
} |
|
62 |
|
|
63 |
@Test |
|
64 |
public void testConvertToListWithUnicodeEscapeCodes() throws IOException{ |
|
65 |
List<Document> actual = JsonUtils.convertToList( |
|
66 |
"eu/dnetlib/iis/core/java/io/document_with_unicode_escape_codes.json", |
|
67 |
Document.SCHEMA$, Document.class); |
|
68 |
List<Document> expected = DataStoreExamples.getDocumentWithUnicodeEscapeCodes(); |
|
69 |
TestsIOUtils.assertEqualSets(expected, actual); |
|
70 |
} |
|
71 |
|
|
72 |
@Test |
|
73 |
public void testPrettyPrint() throws IOException{ |
|
74 |
String uglyJson = getStringFromResourceFile( |
|
75 |
"eu/dnetlib/iis/core/java/io/json_pretty_print/ugly_json.json"); |
|
76 |
String actual = JsonUtils.toPrettyJSON(uglyJson); |
|
77 |
String expected = getStringFromResourceFile( |
|
78 |
"eu/dnetlib/iis/core/java/io/json_pretty_print/expected_pretty_json.json"); |
|
79 |
Assert.assertEquals(expected, actual); |
|
80 |
} |
|
81 |
|
|
82 |
private String getStringFromResourceFile(String resourcePath) throws IOException{ |
|
83 |
InputStream in = Thread.currentThread().getContextClassLoader() |
|
84 |
.getResourceAsStream(resourcePath); |
|
85 |
return IOUtils.toString(in, "UTF-8"); |
|
86 |
} |
|
87 |
} |
modules/icm-iis-core/tags/icm-iis-core-1.0.0/src/test/java/eu/dnetlib/iis/core/java/CmdLineParserForProcessConstructionTest.java | ||
---|---|---|
1 |
package eu.dnetlib.iis.core.java; |
|
2 |
|
|
3 |
import static org.junit.Assert.assertEquals; |
|
4 |
|
|
5 |
import java.util.Map; |
|
6 |
|
|
7 |
import org.apache.commons.cli.CommandLine; |
|
8 |
import org.junit.Test; |
|
9 |
|
|
10 |
import eu.dnetlib.iis.core.java.CmdLineParserForProcessConstruction; |
|
11 |
import eu.dnetlib.iis.core.java.HadoopContext; |
|
12 |
import eu.dnetlib.iis.core.java.PortBindings; |
|
13 |
import eu.dnetlib.iis.core.java.Process; |
|
14 |
import eu.dnetlib.iis.core.java.porttype.PortType; |
|
15 |
|
|
16 |
/** |
|
17 |
* |
|
18 |
* @author Mateusz Kobos |
|
19 |
* |
|
20 |
*/ |
|
21 |
class DummyProcess implements Process { |
|
22 |
|
|
23 |
public DummyProcess(){ |
|
24 |
} |
|
25 |
|
|
26 |
@Override |
|
27 |
public Map<String, PortType> getInputPorts() { |
|
28 |
return null; |
|
29 |
} |
|
30 |
|
|
31 |
@Override |
|
32 |
public Map<String, PortType> getOutputPorts(){ |
|
33 |
return null; |
|
34 |
} |
|
35 |
|
|
36 |
@Override |
|
37 |
public void run(PortBindings portBindings, HadoopContext context, |
|
38 |
Map<String, String> parameters){ |
|
39 |
} |
|
40 |
} |
|
41 |
|
|
42 |
class DummyProcessWithParametersConstructor implements Process{ |
|
43 |
public String[] params; |
|
44 |
|
|
45 |
|
|
46 |
public DummyProcessWithParametersConstructor(String[] params){ |
|
47 |
this.params = params; |
|
48 |
} |
|
49 |
|
|
50 |
@Override |
|
51 |
public Map<String, PortType> getInputPorts() { |
|
52 |
return null; |
|
53 |
} |
|
54 |
|
|
55 |
@Override |
|
56 |
public Map<String, PortType> getOutputPorts() { |
|
57 |
return null; |
|
58 |
} |
|
59 |
|
|
60 |
@Override |
|
61 |
public void run(PortBindings portBindings, HadoopContext context, |
|
62 |
Map<String, String> parameters) throws Exception { |
|
63 |
} |
|
64 |
|
|
65 |
} |
|
66 |
|
|
67 |
public class CmdLineParserForProcessConstructionTest{ |
|
68 |
|
|
69 |
@Test |
|
70 |
public void testTrivial() { |
|
71 |
String[] args = new String[]{ |
|
72 |
"eu.dnetlib.iis.core.java.DummyProcess"}; |
|
73 |
CmdLineParserForProcessConstruction cmdLineParser = |
|
74 |
new CmdLineParserForProcessConstruction(); |
|
75 |
CommandLine cmdLine = CmdLineParser.parse(args); |
|
76 |
Process actual = cmdLineParser.run(cmdLine); |
|
77 |
assertEquals(DummyProcess.class, actual.getClass()); |
|
78 |
} |
|
79 |
|
|
80 |
@Test |
|
81 |
public void testWithSomeParameters() { |
|
82 |
String[] args = new String[]{ |
|
83 |
"eu.dnetlib.iis.core.java.DummyProcess", |
|
84 |
"-Iperson=hdfs://localhost:8020/users/joe/person_input", |
|
85 |
"-Idocument=hdfs://localhost:8020/users/joe/doc_input", |
|
86 |
"-Omerged=hdfs://localhost:8020/users/joe/merged_out", |
|
87 |
"-SworkingDir=hdfs://localhost:8020/users/joe/working_dir", |
|
88 |
"-SclassName=java.util.String"}; |
|
89 |
CmdLineParserForProcessConstruction cmdLineParser = |
|
90 |
new CmdLineParserForProcessConstruction(); |
|
91 |
CommandLine cmdLine = CmdLineParser.parse(args); |
|
92 |
Process actual = cmdLineParser.run(cmdLine); |
|
93 |
assertEquals(DummyProcess.class, actual.getClass()); |
|
94 |
} |
|
95 |
|
|
96 |
@Test |
|
97 |
public void testWithSomeParametersWithParametersConstructor() { |
|
98 |
String[] constructorParams = new String[]{ |
|
99 |
"some string", "some other string"}; |
|
100 |
String[] args = new String[]{ |
|
101 |
"eu.dnetlib.iis.core.java.DummyProcessWithParametersConstructor", |
|
102 |
"-C" + constructorParams[0], |
|
103 |
"-C" + constructorParams[1], |
|
104 |
"-Iperson=hdfs://localhost:8020/users/joe/person_input", |
|
105 |
"-Idocument=hdfs://localhost:8020/users/joe/doc_input", |
|
106 |
"-Omerged=hdfs://localhost:8020/users/joe/merged_out", |
|
107 |
"-SworkingDir=hdfs://localhost:8020/users/joe/working_dir", |
|
108 |
"-SclassName=java.util.String"}; |
|
109 |
CmdLineParserForProcessConstruction cmdLineParser = |
|
110 |
new CmdLineParserForProcessConstruction(); |
|
111 |
CommandLine cmdLine = CmdLineParser.parse(args); |
|
112 |
DummyProcessWithParametersConstructor actual = |
|
113 |
(DummyProcessWithParametersConstructor) cmdLineParser.run(cmdLine); |
|
114 |
assertEquals(DummyProcessWithParametersConstructor.class, |
|
115 |
actual.getClass()); |
|
116 |
for(int i = 0; i < constructorParams.length; i++){ |
|
117 |
assertEquals(constructorParams[i], actual.params[i]); |
|
118 |
} |
|
119 |
|
|
120 |
} |
|
121 |
} |
modules/icm-iis-core/tags/icm-iis-core-1.0.0/src/test/java/eu/dnetlib/iis/core/java/ProcessWrapperForTests.java | ||
---|---|---|
1 |
package eu.dnetlib.iis.core.java; |
|
2 |
|
|
3 |
import org.apache.hadoop.conf.Configuration; |
|
4 |
|
|
5 |
import eu.dnetlib.iis.core.OozieTestsIOUtils; |
|
6 |
import eu.dnetlib.iis.core.java.ProcessWrapper; |
|
7 |
|
|
8 |
/** |
|
9 |
* A version of the {@link ProcessWrapper} which uses the Configuration |
|
10 |
* object created by the Oozie unit test framework. This is used in the |
|
11 |
* XML workflow definition in the tests instead of {@link ProcessWrapper}. |
|
12 |
* It makes referencing the underlying file system by the java Process run |
|
13 |
* during the tests possible. |
|
14 |
* @author Mateusz Kobos |
|
15 |
* |
|
16 |
*/ |
|
17 |
public class ProcessWrapperForTests extends ProcessWrapper { |
|
18 |
public Configuration getConfiguration() throws Exception { |
|
19 |
return OozieTestsIOUtils.loadConfiguration(); |
|
20 |
} |
|
21 |
|
|
22 |
public static void main(String[] args) throws Exception { |
|
23 |
ProcessWrapper wrapper = new ProcessWrapperForTests(); |
|
24 |
wrapper.run(args); |
|
25 |
} |
|
26 |
} |
modules/icm-iis-core/tags/icm-iis-core-1.0.0/src/test/java/eu/dnetlib/iis/core/java/jsonworkflownodes/ProducerAndConsumerTest.java | ||
---|---|---|
1 |
package eu.dnetlib.iis.core.java.jsonworkflownodes; |
|
2 |
|
|
3 |
import java.util.Map; |
|
4 |
|
|
5 |
import junit.framework.Assert; |
|
6 |
|
|
7 |
import org.junit.Test; |
|
8 |
|
|
9 |
import eu.dnetlib.iis.core.common.AvroUtils; |
|
10 |
import eu.dnetlib.iis.core.java.porttype.AvroPortType; |
|
11 |
import eu.dnetlib.iis.core.java.porttype.PortType; |
|
12 |
|
|
13 |
public class ProducerAndConsumerTest{ |
|
14 |
@Test |
|
15 |
public void testPortCreationInProducer(){ |
|
16 |
checkPortsCreator(new PortsCreator() { |
|
17 |
@Override |
|
18 |
public Map<String, PortType> getPorts(String[] specificationStrings){ |
|
19 |
Producer producer = |
|
20 |
new Producer(specificationStrings); |
|
21 |
Assert.assertEquals(0, producer.getInputPorts().size()); |
|
22 |
return producer.getOutputPorts(); |
|
23 |
} |
|
24 |
}); |
|
25 |
} |
|
26 |
|
|
27 |
@Test |
|
28 |
public void testPortCreationInTestingConsumer(){ |
|
29 |
checkPortsCreator(new PortsCreator() { |
|
30 |
@Override |
|
31 |
public Map<String, PortType> getPorts(String[] specificationStrings){ |
|
32 |
TestingConsumer consumer = |
|
33 |
new TestingConsumer(specificationStrings); |
|
34 |
Assert.assertEquals(0, consumer.getOutputPorts().size()); |
|
35 |
return consumer.getInputPorts(); |
|
36 |
} |
|
37 |
}); |
|
38 |
} |
|
39 |
|
|
40 |
private void checkPortsCreator(PortsCreator creator) { |
|
41 |
PortSpec[] specs = new PortSpec[] { |
|
42 |
new PortSpec( |
|
43 |
"document", |
|
44 |
"eu.dnetlib.iis.core.schemas.standardexamples.Document", |
|
45 |
"fake/path/document.json"), |
|
46 |
new PortSpec("person", |
|
47 |
"eu.dnetlib.iis.core.schemas.standardexamples.Person", |
|
48 |
"fake/path/person.json") }; |
|
49 |
String[] specsStr = new String[specs.length]; |
|
50 |
for (int i = 0; i < specs.length; i++) { |
|
51 |
specsStr[i] = String.format("{%s, %s, %s}", specs[i].name, |
|
52 |
specs[i].schemaPath, specs[i].jsonPath); |
|
53 |
} |
|
54 |
Map<String, PortType> outs = creator.getPorts(specsStr); |
|
55 |
Assert.assertEquals(specs.length, outs.size()); |
|
56 |
for (int i = 0; i < specs.length; i++) { |
|
57 |
AvroPortType actual = (AvroPortType) outs.get(specs[i].name); |
|
58 |
AvroPortType expected = new AvroPortType( |
|
59 |
AvroUtils.toSchema(specs[i].schemaPath)); |
|
60 |
Assert.assertEquals(expected, actual); |
|
61 |
} |
|
62 |
} |
|
63 |
} |
|
64 |
|
|
65 |
interface PortsCreator{ |
|
66 |
Map<String, PortType> getPorts(String[] specificationStrings); |
|
67 |
} |
|
68 |
|
|
69 |
class PortSpec{ |
|
70 |
public String name; |
|
71 |
public String schemaPath; |
|
72 |
public String jsonPath; |
|
73 |
|
|
74 |
public PortSpec(String name, String schemaPath, String jsonPath){ |
|
75 |
this.name = name; |
|
76 |
this.schemaPath = schemaPath; |
|
77 |
this.jsonPath = jsonPath; |
|
78 |
} |
|
79 |
} |
modules/icm-iis-core/tags/icm-iis-core-1.0.0/src/test/java/eu/dnetlib/iis/core/java/jsonworkflownodes/package-info.java | ||
---|---|---|
1 |
/** The package contains producer and consumer workflow nodes that are |
|
2 |
* configured using JSON format to respectively produce and consume |
|
3 |
* data stores. |
|
4 |
*/ |
|
5 |
package eu.dnetlib.iis.core.java.jsonworkflownodes; |
modules/icm-iis-core/tags/icm-iis-core-1.0.0/src/test/java/eu/dnetlib/iis/core/java/jsonworkflownodes/TestingConsumer.java | ||
---|---|---|
1 |
package eu.dnetlib.iis.core.java.jsonworkflownodes; |
|
2 |
|
|
3 |
import java.io.IOException; |
|
4 |
import java.util.HashMap; |
|
5 |
import java.util.List; |
|
6 |
import java.util.Map; |
|
7 |
|
|
8 |
import org.apache.avro.specific.SpecificRecord; |
|
9 |
import org.apache.hadoop.fs.FileSystem; |
|
10 |
import org.apache.hadoop.fs.Path; |
|
11 |
|
|
12 |
import eu.dnetlib.iis.core.TestsIOUtils; |
|
13 |
import eu.dnetlib.iis.core.java.HadoopContext; |
|
14 |
import eu.dnetlib.iis.core.java.PortBindings; |
|
15 |
import eu.dnetlib.iis.core.java.Process; |
|
16 |
import eu.dnetlib.iis.core.java.io.DataStore; |
|
17 |
import eu.dnetlib.iis.core.java.io.FileSystemPath; |
|
18 |
import eu.dnetlib.iis.core.java.io.JsonUtils; |
|
19 |
import eu.dnetlib.iis.core.java.jsonworkflownodes.PortSpecifications.SpecificationValues; |
|
20 |
import eu.dnetlib.iis.core.java.porttype.PortType; |
|
21 |
|
|
22 |
/** |
|
23 |
* Reads the data stores from specified input ports and compares them with |
|
24 |
* expected JSON-encoded data stores. If There is a mismatch, |
|
25 |
* an exception is thrown. |
|
26 |
* |
|
27 |
* @author Mateusz Kobos |
|
28 |
*/ |
|
29 |
public class TestingConsumer implements Process { |
|
30 |
private final PortSpecifications inputSpecs; |
|
31 |
|
|
32 |
/** |
|
33 |
* @param inputSpecifications specifications of input. Each element of |
|
34 |
* the array corresponds to a single specification. Single specification |
|
35 |
* conforms to the following template: |
|
36 |
* "{input port name, schema reference, path to JSON file in resources |
|
37 |
* corresponding to the expected input data store}", |
|
38 |
* e.g. "{person, eu.dnetlib.iis.core.examples.schemas.documentandauthor.Person, |
|
39 |
* eu/dnetlib/iis/core/examples/person.json}" |
|
40 |
*/ |
|
41 |
public TestingConsumer(String[] inputSpecifications){ |
|
42 |
inputSpecs = new PortSpecifications(inputSpecifications); |
|
43 |
} |
|
44 |
|
|
45 |
@Override |
|
46 |
public Map<String, PortType> getInputPorts() { |
|
47 |
return inputSpecs.getPortTypes(); |
|
48 |
} |
|
49 |
|
|
50 |
@Override |
|
51 |
public Map<String, PortType> getOutputPorts() { |
|
52 |
return new HashMap<String, PortType>(); |
|
53 |
} |
|
54 |
|
|
55 |
@Override |
|
56 |
public void run(PortBindings portBindings, HadoopContext context, |
|
57 |
Map<String, String> parameters) throws Exception { |
|
58 |
Map<String, Path> input = portBindings.getInput(); |
|
59 |
FileSystem fs = FileSystem.get(context.getConfiguration()); |
|
60 |
for(Map.Entry<String, Path> e: input.entrySet()){ |
|
61 |
SpecificationValues specs = inputSpecs.get(e.getKey()); |
|
62 |
check(new FileSystemPath(fs, e.getValue()), specs); |
|
63 |
} |
|
64 |
} |
|
65 |
|
|
66 |
private static void check(FileSystemPath actualPath, SpecificationValues specs) throws IOException{ |
|
67 |
List<SpecificRecord> expected = JsonUtils.convertToList( |
|
68 |
specs.jsonFilePath, specs.schema, SpecificRecord.class); |
|
69 |
List<SpecificRecord> actual = DataStore.read(actualPath, specs.schema); |
|
70 |
TestsIOUtils.assertEqualSets(expected, actual, true); |
|
71 |
} |
|
72 |
} |
modules/icm-iis-core/tags/icm-iis-core-1.0.0/src/test/java/eu/dnetlib/iis/core/WorkflowConfiguration.java | ||
---|---|---|
1 |
package eu.dnetlib.iis.core; |
|
2 |
|
|
3 |
import java.util.Properties; |
|
4 |
|
|
5 |
import org.apache.oozie.client.WorkflowJob; |
|
6 |
|
|
7 |
/** |
|
8 |
* Configuration of the testing workflow |
|
9 |
* @author Mateusz Kobos |
|
10 |
*/ |
|
11 |
public class WorkflowConfiguration { |
|
12 |
|
|
13 |
/** I had to change the duration of the wait from the original |
|
14 |
* 15 seconds because the original time wasn't enough for my |
|
15 |
* computer to execute the Oozie workflow. To be more precise, |
|
16 |
* the test ended with the following failure message: |
|
17 |
* junit.framework.AssertionFailedError: expected:<SUCCEEDED> but was:<RUNNING> |
|
18 |
*/ |
|
19 |
public static final int defaultTimeoutInSeconds = 360; |
|
20 |
public static final WorkflowJob.Status defaultExpectedFinishStatus = |
|
21 |
WorkflowJob.Status.SUCCEEDED; |
|
22 |
public static final Properties defaultJobProperties = null; |
|
23 |
|
|
24 |
private Properties jobProps = defaultJobProperties; |
|
25 |
private int timeoutInSeconds = defaultTimeoutInSeconds; |
|
26 |
private WorkflowJob.Status expectedFinishStatus = defaultExpectedFinishStatus; |
|
27 |
|
|
28 |
public WorkflowConfiguration(){ |
|
29 |
} |
|
30 |
|
|
31 |
/** |
|
32 |
* See {@link #setJobProps} for description |
|
33 |
* @return |
|
34 |
*/ |
|
35 |
public Properties getJobProps() { |
|
36 |
return jobProps; |
|
37 |
} |
|
38 |
/** |
|
39 |
* @param jobProps job properties |
|
40 |
*/ |
|
41 |
public WorkflowConfiguration setJobProps(Properties jobProps) { |
|
42 |
this.jobProps = jobProps; |
|
43 |
return this; |
|
44 |
} |
|
45 |
|
|
46 |
/** |
|
47 |
* See {@link #setTimeoutInSeconds} for description |
|
48 |
*/ |
|
49 |
public int getTimeoutInSeconds() { |
|
50 |
return timeoutInSeconds; |
|
51 |
} |
|
52 |
/** |
|
53 |
* @param timeoutInSeconds timeout in seconds. Workflow will be killed |
|
54 |
* if timeout is exceeded |
|
55 |
*/ |
|
56 |
public WorkflowConfiguration setTimeoutInSeconds(int timeoutInSeconds) { |
|
57 |
this.timeoutInSeconds = timeoutInSeconds; |
|
58 |
return this; |
|
59 |
} |
|
60 |
|
|
61 |
/** |
|
62 |
* See {@link #setExpectedFinishStatus} for description |
|
63 |
* @return |
|
64 |
*/ |
|
65 |
public WorkflowJob.Status getExpectedFinishStatus() { |
|
66 |
return expectedFinishStatus; |
|
67 |
} |
|
68 |
/** |
|
69 |
* @param expectedFinishStatus expected status of the workflow after its |
|
70 |
* finish |
|
71 |
*/ |
|
72 |
public WorkflowConfiguration setExpectedFinishStatus(WorkflowJob.Status expectedFinishStatus) { |
|
73 |
this.expectedFinishStatus = expectedFinishStatus; |
|
74 |
return this; |
|
75 |
} |
|
76 |
} |
modules/icm-iis-core/tags/icm-iis-core-1.0.0/src/test/java/eu/dnetlib/iis/core/StringReplacer.java | ||
---|---|---|
1 |
package eu.dnetlib.iis.core; |
|
2 |
|
|
3 |
import java.io.BufferedReader; |
|
4 |
import java.io.File; |
|
5 |
import java.io.FileReader; |
|
6 |
import java.io.FileWriter; |
|
7 |
import java.io.IOException; |
|
8 |
import java.io.PrintWriter; |
|
9 |
import java.util.ArrayList; |
|
10 |
import java.util.List; |
|
11 |
|
|
12 |
/** |
|
13 |
* Replace strings conforming to given regexes "on the flight", i.e. while copying |
|
14 |
* file from one place to another. |
|
15 |
* |
|
16 |
* This is used in order to change the Oozie's "workflow.xml" so that it |
|
17 |
* conforms to the requirements of the MiniOozie testing engine. |
|
18 |
* |
|
19 |
* @author Mateusz Kobos |
|
20 |
* |
|
21 |
*/ |
|
22 |
public class StringReplacer { |
|
23 |
private static final List<StringReplacement> replacements = getReplacements(); |
|
24 |
|
|
25 |
private static List<StringReplacement> getReplacements(){ |
|
26 |
List<StringReplacement> list = new ArrayList<StringReplacement>(); |
|
27 |
|
|
28 |
/** We have to use a special wrapper for Java workflow nodes in order |
|
29 |
* to make them use the special mock HDFS used during the tests. |
|
30 |
*/ |
|
31 |
list.add(new StringReplacement( |
|
32 |
"ProcessWrapper", |
|
33 |
"ProcessWrapperForTests")); |
|
34 |
|
|
35 |
/** The paths to Avro schemas stored in HDFS are understood differently |
|
36 |
* when running on real cluster and during the tests. When running |
|
37 |
* during the tests, a file system prefix "hdfs://" is absent, which |
|
38 |
* confuses the "avro-json" library that allows for reading and writing |
|
39 |
* Avro files in Hadoop Streaming workflow node. See the code of |
|
40 |
* {@code com.cloudera.science.avro.common.SchemaLoader.loadFromUrl()} |
|
41 |
* method for details where this confusion comes from. |
|
42 |
* |
|
43 |
* Here, we're adding a prefix which is the address of the name |
|
44 |
* node which is something that "avro-json" library expects. |
|
45 |
*/ |
|
46 |
list.add(new StringReplacement( |
|
47 |
"<value>(\\s*\\$\\{wf:appPath\\(\\)\\}.*\\.avsc\\s*)</value>", |
|
48 |
"<value>\\${nameNode}$1</value>")); |
|
49 |
|
|
50 |
return list; |
|
51 |
} |
|
52 |
|
|
53 |
public void replace(File in, File out) throws IOException { |
|
54 |
BufferedReader reader = null; |
|
55 |
PrintWriter writer = null; |
|
56 |
try { |
|
57 |
reader = new BufferedReader(new FileReader(in)); |
|
58 |
writer = new PrintWriter(new FileWriter(out)); |
|
59 |
String line = null; |
|
60 |
while ((line = reader.readLine()) != null){ |
|
61 |
String newLine = line; |
|
62 |
for(StringReplacement replacement: replacements){ |
|
63 |
newLine = replacement.replaceAll(newLine); |
|
64 |
} |
|
65 |
writer.println(newLine); |
|
66 |
} |
|
67 |
} finally { |
|
68 |
if(reader != null){ |
|
69 |
reader.close(); |
|
70 |
} |
|
71 |
if (writer != null){ |
|
72 |
writer.close(); |
|
73 |
} |
|
74 |
} |
|
75 |
} |
|
76 |
} |
|
77 |
|
|
78 |
class StringReplacement{ |
|
79 |
private String regex; |
|
80 |
private String replacement; |
|
81 |
|
|
82 |
public StringReplacement(String regex, String replacement) { |
|
83 |
this.regex = regex; |
|
84 |
this.replacement = replacement; |
|
85 |
} |
|
86 |
|
|
87 |
public String replaceAll(String str){ |
|
88 |
return str.replaceAll(regex, replacement); |
|
89 |
} |
|
90 |
} |
modules/icm-iis-core/tags/icm-iis-core-1.0.0/src/test/java/eu/dnetlib/iis/core/javamapreduce/hack/SchemaSetterTest.java | ||
---|---|---|
1 |
package eu.dnetlib.iis.core.javamapreduce.hack; |
|
2 |
|
|
3 |
import junit.framework.Assert; |
|
4 |
|
|
5 |
import org.apache.avro.Schema; |
|
6 |
import org.apache.avro.mapred.Pair; |
|
7 |
import org.apache.hadoop.conf.Configuration; |
|
8 |
import org.junit.Test; |
|
9 |
|
|
10 |
import eu.dnetlib.iis.core.schemas.standardexamples.Document; |
|
11 |
import eu.dnetlib.iis.core.schemas.standardexamples.Person; |
|
12 |
import eu.dnetlib.iis.core.schemas.standardexamples.personwithdocuments.PersonWithDocuments; |
|
13 |
import eu.dnetlib.iis.core.javamapreduce.hack.oldapi.SchemaSetter; |
|
14 |
|
|
15 |
public class SchemaSetterTest { |
|
16 |
|
|
17 |
@Test |
|
18 |
public void testBasic() { |
|
19 |
Configuration conf = new Configuration(); |
|
20 |
conf.set(SchemaSetter.inputClassName, |
|
21 |
"eu.dnetlib.iis.core.schemas.standardexamples.Document"); |
|
22 |
conf.set(SchemaSetter.mapOutputKeyClassName, |
|
23 |
SchemaSetter.primitiveTypePrefix+"DOUBLE"); |
|
24 |
conf.set(SchemaSetter.mapOutputValueClassName, |
|
25 |
"eu.dnetlib.iis.core.schemas.standardexamples.Person"); |
|
26 |
conf.set(SchemaSetter.outputClassName, |
|
27 |
"eu.dnetlib.iis.core.schemas.standardexamples.personwithdocuments.PersonWithDocuments"); |
|
28 |
SchemaSetter.set(conf); |
|
29 |
Assert.assertEquals(Document.SCHEMA$.toString(), |
|
30 |
conf.get(SchemaSetter.avroInput)); |
|
31 |
Assert.assertEquals(Pair.getPairSchema( |
|
32 |
Schema.create(Schema.Type.DOUBLE), |
|
33 |
Person.SCHEMA$).toString(), |
|
34 |
conf.get(SchemaSetter.avroMapOutput)); |
|
35 |
Assert.assertEquals(PersonWithDocuments.SCHEMA$.toString(), |
|
36 |
conf.get(SchemaSetter.avroOutput)); |
|
37 |
|
|
38 |
} |
|
39 |
|
|
40 |
} |
modules/icm-iis-core/tags/icm-iis-core-1.0.0/src/test/java/eu/dnetlib/iis/core/common/AvroToolsTests.java | ||
---|---|---|
1 |
package eu.dnetlib.iis.core.common; |
|
2 |
|
|
3 |
import static org.junit.Assert.*; |
|
4 |
|
|
5 |
import org.junit.Test; |
|
6 |
|
|
7 |
public class AvroToolsTests { |
|
8 |
|
|
9 |
@Test |
|
10 |
public void testToSchemaPrimitiveType() { |
|
11 |
checkSchema("\"string\"", "org.apache.avro.Schema.Type.STRING"); |
|
12 |
} |
|
13 |
|
|
14 |
@Test |
|
15 |
public void testToSchemaClassName(){ |
|
16 |
checkSchema("{\"type\":\"record\",\"name\":\"Person\",\"namespace\":\"eu.dnetlib.iis.core.schemas.standardexamples\",\"fields\":[{\"name\":\"id\",\"type\":\"int\"},{\"name\":\"name\",\"type\":\"string\"},{\"name\":\"age\",\"type\":\"int\"}]}", |
|
17 |
"eu.dnetlib.iis.core.schemas.standardexamples.Person"); |
|
18 |
} |
|
19 |
|
|
20 |
private static void checkSchema(String expected, String className){ |
|
21 |
String actual = AvroUtils.toSchema(className).toString(); |
|
22 |
assertEquals(expected, actual); |
|
23 |
} |
|
24 |
|
|
25 |
} |
modules/icm-iis-core/tags/icm-iis-core-1.0.0/src/test/java/eu/dnetlib/iis/core/AbstractWorkflowTestCase.java | ||
---|---|---|
1 |
package eu.dnetlib.iis.core; |
|
2 |
|
|
3 |
import java.io.IOException; |
|
4 |
import java.util.Properties; |
|
5 |
|
|
6 |
import org.apache.commons.logging.LogFactory; |
|
7 |
import org.apache.hadoop.fs.Path; |
|
8 |
import org.apache.hadoop.mapred.JobConf; |
|
9 |
import org.apache.oozie.client.*; |
|
10 |
import org.apache.oozie.local.LocalOozie; |
|
11 |
import org.apache.oozie.service.XLogService; |
|
12 |
import org.apache.oozie.test.MiniOozieTestCase; |
|
13 |
import org.apache.oozie.util.XLog; |
|
14 |
import org.junit.After; |
|
15 |
import org.junit.Before; |
|
16 |
|
|
17 |
|
|
18 |
/** |
|
19 |
* A basis to be used as a base class of all test case classes that run |
|
20 |
* Oozie workflows. |
|
21 |
* <p/> |
|
22 |
* ATTENTION: In order for your test case class that has this class as a parent |
|
23 |
* to run properly, the Maven project which contains this class has to have |
|
24 |
* a directory called "core" in its root directory with appropriate contents. |
|
25 |
* See the "core" directory in the "icm-iis-core-examples" to see what's |
|
26 |
* the required content of this directory. |
|
27 |
* You can copy this "core" directory to your project. |
|
28 |
* <p/> |
|
29 |
* <p/> |
|
30 |
* If something goes wrong with your test and you want to see the Hadoop logs |
|
31 |
* to check what's the matter, you can look into directory |
|
32 |
* <p/> |
|
33 |
* target/test-data/oozietests/NAME_OF_YOUR_TEST_CLASS/NAME_OF_YOUR_TEST_METHOD/userlogs |
|
34 |
* <p/> |
|
35 |
* and its subdirectories. |
|
36 |
* <p/> |
|
37 |
* Unfortunately, I don't know where the files stored in virtual HDFS really |
|
38 |
* are (such an information would also be helpful during debugging). |
|
39 |
* |
|
40 |
* @author Mateusz Kobos |
|
41 |
* |
|
42 |
*/ |
|
43 |
public abstract class AbstractWorkflowTestCase extends MiniOozieTestCase { |
|
44 |
|
|
45 |
private static final char WORKFLOW_LOCATION_SEPARATOR = '/'; |
|
46 |
|
|
47 |
private static final String JOB_PROPERTIES_FILE_NAME = "job.properties"; |
|
48 |
|
|
49 |
@Override |
|
50 |
@Before |
|
51 |
protected void setUp() throws Exception { |
|
52 |
System.setProperty(XLogService.LOG4J_FILE, "oozie-log4j.properties"); |
|
53 |
log = new XLog(LogFactory.getLog(getClass())); |
|
54 |
super.setUp(); |
|
55 |
} |
|
56 |
|
|
57 |
@Override |
|
58 |
@After |
|
59 |
protected void tearDown() throws Exception { |
|
60 |
super.tearDown(); |
|
61 |
} |
|
62 |
|
|
63 |
/** |
|
64 |
* See {@link #runWorkflow(String resourcesOozieAppLocalPath, WorkflowConfiguration config)} |
|
65 |
* method for description of parameters |
|
66 |
*/ |
|
67 |
public RemoteOozieAppManager runWorkflow(String resourcesOozieAppLocalPath) |
|
68 |
throws IOException, OozieClientException{ |
|
69 |
return runWorkflow(resourcesOozieAppLocalPath, new WorkflowConfiguration()); |
|
70 |
} |
|
71 |
|
|
72 |
/** |
|
73 |
* Run the workflow |
|
74 |
* @param resourcesOozieAppLocalPath path to the directory containing the Oozie |
|
75 |
* application. This directory contains the main {@code workflow.xml} file |
|
76 |
* @param config optional configuration of the workflow |
|
77 |
*/ |
|
78 |
public RemoteOozieAppManager runWorkflow( |
|
79 |
String resourcesOozieAppLocalPath, WorkflowConfiguration config) |
|
80 |
throws IOException, OozieClientException{ |
|
81 |
return runWorkflow(resourcesOozieAppLocalPath, config, false); |
|
82 |
} |
|
83 |
|
|
84 |
/** |
|
85 |
* Run the workflow |
|
86 |
* @param resourcesOozieAppLocalPath path to the directory containing the Oozie |
|
87 |
* application. This directory contains the main {@code workflow.xml} file |
|
88 |
* @param config optional configuration of the workflow |
|
89 |
* @param skipPredefinedJobProperties skips reading predefined job.properties if any set |
|
90 |
*/ |
|
91 |
public RemoteOozieAppManager runWorkflow( |
|
92 |
String resourcesOozieAppLocalPath, WorkflowConfiguration config, |
|
93 |
boolean skipPredefinedJobProperties) |
|
94 |
throws IOException, OozieClientException{ |
|
95 |
// File oozieAppLocalPath = |
|
96 |
// new File(Thread.currentThread().getContextClassLoader() |
|
97 |
// .getResource(resourcesOozieAppLocalPath).getPath()); |
|
98 |
RemoteOozieAppManager appManager = RemoteOozieAppManager.fromPrimedClassDir( |
|
99 |
getFileSystem(), getFsTestCaseDir(), resourcesOozieAppLocalPath); |
|
100 |
Properties props = createWorkflowConfiguration( |
|
101 |
createJobConf(), getTestUser(), appManager.getOozieAppPath(), |
|
102 |
appManager.getWorkingDir(), |
|
103 |
skipPredefinedJobProperties?null: |
|
104 |
loadPredefinedJobProperties(resourcesOozieAppLocalPath), |
|
105 |
config.getJobProps()); |
|
106 |
runWorkflowBasedOnConfiguration(props, config.getTimeoutInSeconds(), |
|
107 |
config.getExpectedFinishStatus()); |
|
108 |
return appManager; |
|
109 |
|
|
110 |
} |
|
111 |
|
|
112 |
/** |
|
113 |
* Loads job properties defined along with the workflow in job.properties file. |
|
114 |
* @param resourcesOozieAppLocalPath |
|
115 |
* @return properties read from predefined job.properties file, null when file not found |
|
116 |
*/ |
|
117 |
private Properties loadPredefinedJobProperties(String resourcesOozieAppLocalPath) { |
|
118 |
if (resourcesOozieAppLocalPath!=null && |
|
119 |
resourcesOozieAppLocalPath.contains(""+WORKFLOW_LOCATION_SEPARATOR)) { |
|
120 |
try { |
|
121 |
String parentWorkflowDir = resourcesOozieAppLocalPath.substring(0, |
|
122 |
resourcesOozieAppLocalPath.lastIndexOf(WORKFLOW_LOCATION_SEPARATOR)); |
|
123 |
Properties resultProperties = new Properties(); |
|
124 |
resultProperties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream( |
|
125 |
parentWorkflowDir + WORKFLOW_LOCATION_SEPARATOR + JOB_PROPERTIES_FILE_NAME)); |
|
126 |
return resultProperties; |
|
127 |
} catch (Exception e) { |
|
128 |
log.warn("unable to load job.properties for workflow location " + |
|
129 |
resourcesOozieAppLocalPath); |
|
130 |
} |
|
131 |
} |
|
132 |
// fallback |
|
133 |
return null; |
|
134 |
} |
|
135 |
|
|
136 |
/** |
|
137 |
* Run the workflow |
|
138 |
* @deprecated Replaced by {@link #runWorkflow(String)} |
|
139 |
*/ |
|
140 |
@Deprecated |
|
141 |
public void runWorkflow(Path oozieAppPath, Path workingDir) |
|
142 |
throws OozieClientException, IOException{ |
|
143 |
runWorkflow(oozieAppPath, workingDir, |
|
144 |
WorkflowConfiguration.defaultJobProperties); |
|
145 |
} |
|
146 |
|
|
147 |
/** |
|
148 |
* Run the workflow |
|
149 |
* @deprecated Replaced by {@link #runWorkflow(String, WorkflowConfiguration)} |
|
150 |
*/ |
|
151 |
@Deprecated |
|
152 |
public void runWorkflow(Path oozieAppPath, Path workingDir, |
|
153 |
Properties jobProperties) |
|
154 |
throws OozieClientException, IOException{ |
|
155 |
WorkflowConfiguration config = |
|
156 |
new WorkflowConfiguration().setJobProps(jobProperties); |
|
157 |
Properties props = createWorkflowConfiguration( |
|
158 |
createJobConf(), getTestUser(), oozieAppPath, |
|
159 |
workingDir, null, config.getJobProps()); |
|
160 |
runWorkflowBasedOnConfiguration(props, config.getTimeoutInSeconds(), |
|
161 |
config.getExpectedFinishStatus()); |
|
162 |
} |
|
163 |
|
|
164 |
private static Properties createWorkflowConfiguration( |
|
165 |
JobConf jobConf, String testUser, |
|
166 |
Path oozieAppPath, Path workingDir, |
|
167 |
Properties defaultJobProps, Properties runtimeJobProps) |
|
168 |
throws IOException{ |
|
169 |
OozieTestsIOUtils.saveConfiguration(jobConf); |
|
170 |
|
|
171 |
final OozieClient wc = LocalOozie.getClient(); |
|
172 |
|
|
173 |
Properties conf = wc.createConfiguration(); |
|
174 |
/** Standard settings*/ |
|
175 |
conf.setProperty(OozieClient.APP_PATH, oozieAppPath.toString()); |
|
176 |
conf.setProperty(OozieClient.USER_NAME, testUser); |
|
177 |
conf.setProperty("jobTracker", jobConf.get("mapred.job.tracker")); |
|
178 |
conf.setProperty("nameNode", jobConf.get("fs.default.name")); |
|
179 |
conf.setProperty("queueName", "default"); |
|
180 |
/** Custom settings */ |
|
181 |
conf.setProperty("workingDir", workingDir.toString()); |
|
182 |
conf.setProperty("minioozieTestRun", "true"); |
|
183 |
if (defaultJobProps!=null) { |
|
184 |
conf.putAll(defaultJobProps); |
|
185 |
} |
|
186 |
if (runtimeJobProps!=null) { |
|
187 |
conf.putAll(runtimeJobProps); |
|
188 |
} |
|
189 |
return conf; |
|
190 |
} |
|
191 |
|
|
192 |
/** |
|
193 |
* Execute workflow and checks if it ran correctly. |
|
194 |
* @param configuration |
|
195 |
* @param timeoutInSeconds |
|
196 |
* @throws OozieClientException |
|
197 |
*/ |
|
198 |
private void runWorkflowBasedOnConfiguration(Properties configuration, |
|
199 |
int timeoutInSeconds, WorkflowJob.Status expectedFinishStatus) |
|
200 |
throws OozieClientException{ |
|
201 |
final OozieClient wc = LocalOozie.getClient(); |
|
202 |
final String jobId = wc.submit(configuration); |
|
203 |
assertNotNull(jobId); |
|
204 |
WorkflowJob wf = wc.getJobInfo(jobId); |
|
205 |
assertNotNull(wf); |
|
206 |
assertEquals(WorkflowJob.Status.PREP, wf.getStatus()); |
|
207 |
wc.start(jobId); |
|
208 |
|
|
209 |
waitFor(timeoutInSeconds * 1000, new Predicate() { |
|
210 |
public boolean evaluate() throws Exception { |
|
211 |
WorkflowJob wf = wc.getJobInfo(jobId); |
|
212 |
return (wf.getEndTime() != null); |
|
213 |
} |
|
214 |
}); |
|
215 |
|
|
216 |
wf = wc.getJobInfo(jobId); |
|
217 |
|
|
218 |
printErrors(wc, wf); |
|
219 |
|
|
220 |
assertNotNull(wf); |
|
221 |
assertEquals(expectedFinishStatus, wf.getStatus()); |
|
222 |
} |
|
223 |
|
|
224 |
private void printErrors(OozieClient wc, WorkflowJob wf) { |
|
225 |
/** |
|
226 |
* We can safely ignore the exceptions thrown during |
|
227 |
* printing error messages |
|
228 |
*/ |
|
229 |
try { |
|
230 |
for (WorkflowAction wa : wf.getActions()) { |
|
231 |
String errorMessage = wa.getErrorMessage() == null ? "" : wa.getErrorMessage(); |
|
232 |
log.info("Workflow action {0} {1}", wa.getId(), errorMessage); |
|
233 |
if ("sub-workflow".equals(wa.getType()) && wa.getExternalId() != null) { |
|
234 |
WorkflowJob subwf = wc.getJobInfo(wa.getExternalId()); |
|
235 |
printErrors(wc, subwf); |
|
236 |
} |
|
237 |
} |
|
238 |
} catch (OozieClientException ex) {} |
|
239 |
} |
|
240 |
} |
modules/icm-iis-core/tags/icm-iis-core-1.0.0/src/test/java/eu/dnetlib/iis/core/TestsIOUtils.java | ||
---|---|---|
1 |
package eu.dnetlib.iis.core; |
|
2 |
|
|
3 |
import java.io.File; |
|
4 |
import java.io.FileInputStream; |
|
5 |
import java.io.FileNotFoundException; |
|
6 |
import java.io.IOException; |
|
7 |
import java.io.InputStream; |
|
8 |
import java.util.ArrayList; |
|
9 |
import java.util.Arrays; |
|
10 |
import java.util.Iterator; |
|
11 |
import java.util.List; |
|
12 |
|
|
13 |
import junit.framework.Assert; |
|
14 |
import eu.dnetlib.iis.core.java.io.JsonUtils; |
|
15 |
|
|
16 |
/** |
|
17 |
* Utility class for tests dealing with IO |
|
18 |
* |
|
19 |
* @author Mateusz Kobos |
|
20 |
* |
|
21 |
*/ |
|
22 |
public class TestsIOUtils { |
|
23 |
|
|
24 |
private final static int filesCompareBufferSize = 1024; |
|
25 |
private final static int dumpRecordsCountlimit = 100; |
|
26 |
|
|
27 |
/** |
|
28 |
* {@code expected} and {@code actual} are equal if they have the same |
|
29 |
* elements, possibly in different order. |
|
30 |
*/ |
|
31 |
public static <T> void assertEqualSets(List<T> expected, Iterable<T> actual) { |
|
32 |
assertEqualSets(expected, actual.iterator()); |
|
33 |
} |
|
34 |
|
|
35 |
/** |
|
36 |
* {@code expected} and {@code actual} are equal if they have the same |
|
37 |
* elements, possibly in different order. |
|
38 |
*/ |
|
39 |
public static <T> void assertEqualSets(List<T> expected, Iterator<T> actual) { |
|
40 |
List<T> actualList = new ArrayList<T>(); |
|
41 |
while(actual.hasNext()) { |
|
42 |
actualList.add(actual.next()); |
|
43 |
} |
|
44 |
assertEqualSets(expected, actualList); |
|
45 |
} |
|
46 |
|
|
47 |
/** |
|
48 |
* {@code expected} and {@code actual} are equal if they have the same |
|
49 |
* elements, possibly in different order. |
|
50 |
*/ |
|
51 |
public static <T> void assertEqualSets( |
|
52 |
List<T> expected, List<T> actual) { |
|
53 |
assertEqualSets(expected, actual, false); |
|
54 |
} |
|
55 |
|
|
56 |
/** |
|
57 |
* {@code expected} and {@code actual} are equal if they have the same |
|
58 |
* elements, possibly in different order. |
|
59 |
* @param toPrettyJson if the sets are not equal, an error message with |
|
60 |
* elements of both lists is printed. If this parameter is set to |
|
61 |
* {@code true}, the string representation of each element is treated as |
|
62 |
* JSON and printed in a pretty way. |
|
63 |
*/ |
|
64 |
public static <T> void assertEqualSets( |
|
65 |
List<T> expected, List<T> actual, boolean toPrettyJson) { |
|
66 |
Assert.assertEquals(String.format("The number of the "+ |
|
67 |
"expected elements (%s) is not equal to the number "+ |
|
68 |
"of the actual elements (%s).\n\n%s", |
Also available in: Unified diff
[maven-release-plugin] copy for tag icm-iis-core-1.0.0