-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'issue-133' into 'master'
Resolves: Issue-133 Closes #133 See merge request ghsc/hazdev/pdl!138
- Loading branch information
Showing
3 changed files
with
141 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"contents": [ | ||
{ | ||
"length": 157, | ||
"modified": "2021-02-09T16:10:03.000Z", | ||
"path": "", | ||
"sha256": "hkomWf290QUmWGV7jOfxhqOu8kS45NglYAQkIUKOyGI=", | ||
"type": "text/plain", | ||
"url": "data:text/plain;base64,OyBFTU06IHNlbmRIZWFydGJlYXQuZXJiIHVwZGF0ZWQgMjAxOC0wOS0wNQpoZWFydGJlYXRfaGVhZF91cmw9J2h0dHBzOi8vZ2l0aHViLmNvbS91c2dzL3BkbC9ldGMvZXhhbXBsZXMvaGVhcnRiZWF0L3NlbmRIZWFydGJlYXQnOwpoZWFydGJlYXRfcGRsX3ZlcnNpb249Jyc7Cg==" | ||
} | ||
], | ||
"geometry": null, | ||
"id": { | ||
"code": "lenore", | ||
"source": "prod01-pdl01.cr.usgs.gov", | ||
"type": "heartbeat", | ||
"updateTime": "2021-02-09T16:10:03.529Z" | ||
}, | ||
"links": [], | ||
"properties": { | ||
"original-signature": "MC4CFQCV4gz1rEKKwRfq9VqQJh+ghPFYpAIVAKfqy8jzF62OVMXP5UdrJC7pf632", | ||
"pdl-client-version": "Version 2.5.1 2020-06-25", | ||
"original-signature-version": "v1" | ||
}, | ||
"signature": "nVsadUpcmRYqICPtQ1oYfrReN0E0ILyYiZ4c/IBaG12W+Pr6nx+k3bu6jt2oUVuayOanHi0SLsbEqpvFdwLxicMt689mV5hdwGgHPWQ5Z163LXchSSFtELWdzR/b4vAlW0ehieZPNx6YOelPBNKMk0kvwcrlO6t9dcNKA26t1yl5OpRkjVqjHwZ3mw4hRYIcXvNkwP+PGYhCGiiqao4FAysuyvr94TGek3FJKj1pxiit9HRfAWQXi9aIhKt7hcSbmymovIufROaw/0XLJWErHMKI33EzRbIWKvwaoVpGsPGzI6z+grrCSdF1gIlf96+q+RgHDenQrR5D6a4b8DbX8HkneV7GqxxI9yS3NDf2w2cRBsFDGkKwG8yBhYVNHg3MT/8RoYJAX/QzB12P4gBwn+K1Zm0jEOtmtxMOjhEBrz/3ey06B3+++39PGq8oZpZoAlai08KftcgSjL4E387rFLMMnSP6J89UWBmTIDfj1xk6rf/FLHr3ET1NmgaF70HBexMGoi2+pTc4KxEc9qQQre45t083IH7PvaX32Yw3x8bnZqGFpiuRdVRW1pxYKdCieBOrfs/eRX9PyZpuvipiNOB7ieQNqMPoJ8UjkyadParPLDSfmJmgqU+0WlQ18C2NUf8PbSF3HiNOhltV7Xph7CrFTSnciLxiJO26gtLYKHI=", | ||
"signatureVersion": "v2", | ||
"status": "UPDATE", | ||
"type": "Feature" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/test/java/gov/usgs/earthquake/product/io/DirectoryProductSourceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package gov.usgs.earthquake.product.io; | ||
|
||
import gov.usgs.earthquake.product.Content; | ||
import gov.usgs.earthquake.product.FileContent; | ||
import gov.usgs.earthquake.product.Product; | ||
import gov.usgs.util.StreamUtils; | ||
|
||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.InputStream; | ||
|
||
import javax.json.Json; | ||
|
||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class DirectoryProductSourceTest { | ||
private static String JSON_EXAMPLE_PRODUCT = "etc/test_products/json/inline_content_example.json"; | ||
private static String STORAGE_BASE_PATH = "product-digest-test-storage"; | ||
|
||
/** | ||
* DirectoryProductSource attempts to convert URLContent to FileContent as part | ||
* of its `streamTo` method. This is typically fine, but if the URLContent is | ||
* for the empty path, this causes problems because a file does not exist on | ||
* disk and leads to FileNotFoundException downstream. | ||
* | ||
* This test ensures URLContent referencing the empty path is not converted to | ||
* FileContent. In this case, the URLContent _should_ typically reference a data | ||
* URL. | ||
*/ | ||
@Test | ||
public void directoryProductSource() { | ||
try (InputStream fin = new FileInputStream(JSON_EXAMPLE_PRODUCT); | ||
InputStream in = StreamUtils.getInputStream(fin);) { | ||
File storageDirectory = new File(STORAGE_BASE_PATH); | ||
|
||
// Create product from JSON file and stream to directory [as XML] | ||
Product jsonProduct = new JsonProduct().getProduct(Json.createReader(in).readObject()); | ||
DirectoryProductHandler outputHandler = new DirectoryProductHandler(storageDirectory); | ||
ObjectProductSource outputSource = new ObjectProductSource(jsonProduct); | ||
outputSource.streamTo(outputHandler); | ||
|
||
// Create product from directory | ||
ObjectProductHandler inputHandler = new ObjectProductHandler(); | ||
DirectoryProductSource inputSource = new DirectoryProductSource(storageDirectory); | ||
inputSource.streamTo(inputHandler); | ||
Product directoryProduct = inputHandler.getProduct(); | ||
|
||
// Ensure the inline (empty path) content is not converted to file content | ||
Content content = directoryProduct.getContents().get(""); | ||
Assert.assertNotNull(content); | ||
Assert.assertTrue("Empty path content not of type FileContent", !(content instanceof FileContent)); | ||
} catch (Exception e) { | ||
Assert.fail(e.getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* Clean up the temporary storage directory after test(s). | ||
* | ||
*/ | ||
@After | ||
public void cleanup() { | ||
// Cleanup tmp storage directory | ||
File storageDirectory = new File(STORAGE_BASE_PATH); | ||
if (storageDirectory.isDirectory()) { | ||
deleteDirectory(storageDirectory); | ||
} | ||
} | ||
|
||
/** | ||
* Helper method to recursively delete a directory (or file). | ||
* | ||
* @param file The directory (or file) to delete. | ||
*/ | ||
private boolean deleteDirectory(File directory) { | ||
File[] files = directory.listFiles(); | ||
|
||
if (files != null) { | ||
for (File file : files) { | ||
deleteDirectory(file); | ||
} | ||
} | ||
|
||
return directory.delete(); | ||
} | ||
} |