use of org.glassfish.api.admin.Payload.Inbound in project Payara by payara.
the class PayloadFilesManagerTest method recursiveReplacementTest.
@Test
public void recursiveReplacementTest() throws Exception {
/*
* Populate the target directory with a subdirectory containing a file,
* then replace the subdirectory via a replacement request in a Payload.
*/
final String DIR = "x/";
final String FILE_A_PREFIX = DIR;
final String FILE_A_NAME = "fileA.txt";
final String FILE_B_PREFIX = DIR;
final String FILE_B_NAME = "fileB.txt";
final Set<File> desiredAbsent = new HashSet<File>();
final Set<File> desiredPresent = new HashSet<File>();
final File targetDir = File.createTempFile("tgt", "");
targetDir.delete();
targetDir.mkdir();
final File dir = new File(targetDir, DIR);
dir.mkdir();
final File fileA = new File(dir, FILE_A_NAME);
writeFile(fileA, "This is FileA", "with two lines of content");
final File origDir = File.createTempFile("pfm", "");
origDir.delete();
origDir.mkdir();
final File origSubDir = new File(origDir, DIR);
origSubDir.mkdirs();
final File fileB = new File(origSubDir, FILE_B_NAME);
writeFile(fileB, "This is FileB", "with yet another", "line of content");
try {
desiredPresent.add(new File(targetDir.toURI().resolve(FILE_B_PREFIX + FILE_B_NAME)));
desiredAbsent.add(new File(targetDir.toURI().resolve(FILE_A_PREFIX + FILE_A_NAME)));
new CommonPermTest() {
@Override
protected CommonPermTest init(File targetDir) {
super.init(targetDir);
return this;
}
@Override
protected void addParts(Outbound ob, PayloadFilesManager instance) throws Exception {
ob.requestFileReplacement("application/octet-stream", URI.create(DIR), "test-xfer", null, /* props */
origSubDir, true);
}
@Override
protected void checkResults(Inbound ib, PayloadFilesManager instance) throws Exception {
listDir("After creation, before deletion", myTargetDir);
/*
* Process files.
*/
instance.processParts(ib);
listDir("After deletion", myTargetDir);
final Set<File> unexpectedlyPresent = new HashSet<File>();
for (File f : desiredAbsent) {
if (f.exists()) {
unexpectedlyPresent.add(f);
}
}
assertEquals("Unexpected files remain after replacement request", Collections.EMPTY_SET, unexpectedlyPresent);
final Set<File> unexpectedlyAbsent = new HashSet<File>();
for (File f : desiredPresent) {
if (!f.exists()) {
unexpectedlyAbsent.add(f);
}
}
assertEquals("Unexpected files absent after replacement request", Collections.EMPTY_SET, unexpectedlyAbsent);
}
@Override
protected void cleanup() {
for (File f : desiredAbsent) {
if (f.exists()) {
f.delete();
}
}
PayloadFilesManagerTest.cleanup(origDir);
}
}.init(targetDir).run("replacementTest");
} finally {
if (targetDir != null) {
FileUtils.whack(targetDir);
}
}
}
use of org.glassfish.api.admin.Payload.Inbound in project Payara by payara.
the class PayloadFilesManagerTest method simplePermanentTransferAndRemovalTest.
@Test
public void simplePermanentTransferAndRemovalTest() throws Exception {
final String FILE_A_PREFIX = "";
final String FILE_A_NAME = "fileA.txt";
final String FILE_B_PREFIX = "x/y/z/";
final String FILE_B_NAME = "fileB.txt";
final String FILE_C_PREFIX = FILE_B_PREFIX;
final String FILE_C_NAME = "fileC.txt";
final Set<File> desiredPresent = new HashSet<File>();
final Set<File> desiredAbsent = new HashSet<File>();
/*
* Create a directory into which we'll transfer some small files.
*/
final File origDir = File.createTempFile("pfm", "");
origDir.delete();
File targetDir = null;
try {
/*
* Choose the directory into which we want the PayloadFilesManager to
* deliver the files.
*/
targetDir = File.createTempFile("tgt", "");
targetDir.delete();
targetDir.mkdir();
origDir.mkdir();
final File fileA = new File(origDir, FILE_A_NAME);
writeFile(fileA, "This is File A", "and another line");
desiredPresent.add(new File(targetDir.toURI().resolve(FILE_A_PREFIX + FILE_A_NAME)));
/*
* In this test result, we want file B to be absent after we transfer
* it (with files A and C) and then use a second PayloadFilesManager
* to request B's removal.
*/
final File fileB = new File(origDir, FILE_B_NAME);
desiredAbsent.add(new File(targetDir.toURI().resolve(FILE_B_PREFIX + FILE_B_NAME)));
writeFile(fileB, "Here is File B", "which has an", "additional line");
final File fileC = new File(origDir, FILE_C_NAME);
desiredPresent.add(new File(targetDir.toURI().resolve(FILE_C_PREFIX + FILE_C_NAME)));
writeFile(fileC, "Here is File C", "which has an", "additional line", "even beyond what fileB has");
new CommonPermTest() {
@Override
protected CommonPermTest init(File targetDir) {
super.init(targetDir);
return this;
}
@Override
protected void addParts(Outbound ob, PayloadFilesManager instance) throws Exception {
ob.attachFile("application/octet-stream", URI.create(FILE_A_PREFIX + fileA.getName()), "test-xfer", fileA);
ob.attachFile("text/plain", URI.create(FILE_B_PREFIX + fileB.getName()), "test-xfer", fileB);
ob.attachFile("text/plain", URI.create(FILE_C_PREFIX + fileC.getName()), "test-xfer", fileC);
}
@Override
protected void checkResults(Inbound ib, PayloadFilesManager instance) throws Exception {
/*
* Extract files to where we want them.
*/
instance.processParts(ib);
/*
* Now ask another PayloadFilesManager to remove one of the
* just-extracted files.
*/
Payload.Outbound ob = PayloadImpl.Outbound.newInstance();
ob.requestFileRemoval(URI.create(FILE_B_PREFIX + FILE_B_NAME), "removeTest", /* dataRequestName */
null);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
ob.writeTo(baos);
baos.close();
final PayloadFilesManager remover = new PayloadFilesManager.Perm(instance.getTargetDir(), null, Logger.getAnonymousLogger());
final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
Payload.Inbound removerIB = PayloadImpl.Inbound.newInstance("application/zip", bais);
remover.processParts(removerIB);
final Set<File> missing = new HashSet<File>();
for (File f : desiredPresent) {
if (!f.exists()) {
missing.add(f);
}
}
assertEquals("Unexpected missing files after extraction", Collections.EMPTY_SET, missing);
final Set<File> unexpectedlyPresent = new HashSet<File>();
for (File f : desiredAbsent) {
if (f.exists()) {
unexpectedlyPresent.add(f);
}
}
assertEquals("Unexpected files remain after removal request", Collections.EMPTY_SET, unexpectedlyPresent);
}
@Override
protected void cleanup() {
for (File f : desiredPresent) {
f.delete();
}
PayloadFilesManagerTest.cleanup(origDir);
}
}.init(targetDir).run("simplePermanentTransferAndRemovalTest");
} finally {
if (targetDir != null) {
FileUtils.whack(targetDir);
}
}
}
use of org.glassfish.api.admin.Payload.Inbound in project Payara by payara.
the class PayloadFilesManagerTest method simplePermanentRecursiveTransferDirOnlyTest.
@Test
public void simplePermanentRecursiveTransferDirOnlyTest() throws Exception {
final String DIR = "x/";
final String Y_SUBDIR = "y/";
final String Z_SUBDIR = "z/";
final Set<File> desiredResults = new HashSet<File>();
/*
* Create a directory into which we'll copy some small files.
*/
final File origDir = File.createTempFile("pfm", "");
origDir.delete();
origDir.mkdir();
final File dir = new File(origDir, DIR);
dir.mkdir();
final File ySubdir = new File(dir, Y_SUBDIR);
ySubdir.mkdir();
final File zSubdir = new File(dir, Y_SUBDIR + Z_SUBDIR);
zSubdir.mkdir();
File targetDir = null;
try {
/*
* Choose the directory into which we want the PayloadFilesManager to
* deliver the files.
*/
targetDir = File.createTempFile("tgt", "");
targetDir.delete();
targetDir.mkdir();
desiredResults.add(new File(targetDir.toURI().resolve(DIR)));
desiredResults.add(new File(targetDir.toURI().resolve(DIR + Y_SUBDIR)));
desiredResults.add(new File(targetDir.toURI().resolve(DIR + Y_SUBDIR + Z_SUBDIR)));
/*
* Add the original directory recursively.
*/
new CommonPermTest() {
@Override
protected CommonPermTest init(File targetDir) {
super.init(targetDir);
return this;
}
@Override
protected void addParts(Outbound ob, PayloadFilesManager instance) throws Exception {
ob.attachFile("application/octet-stream", URI.create(DIR), "test-xfer", dir, true);
}
@Override
protected void checkResults(Inbound ib, PayloadFilesManager instance) throws Exception {
/*
* Extract files to where we want them.
*/
instance.processParts(ib);
final Set<File> missing = new HashSet<File>();
for (File f : desiredResults) {
if (!f.exists()) {
missing.add(f);
}
}
assertEquals("Unexpected missing files after extraction", Collections.EMPTY_SET, missing);
}
@Override
protected void cleanup() {
for (File f : desiredResults) {
f.delete();
}
PayloadFilesManagerTest.cleanup(origDir);
}
}.init(targetDir).run("simplePermanentRecursiveTransferDirOnlyTest");
} finally {
if (targetDir != null) {
FileUtils.whack(targetDir);
}
}
}
use of org.glassfish.api.admin.Payload.Inbound in project Payara by payara.
the class CheckpointHelper method loadInbound.
private Inbound loadInbound(File inboundFile) throws IOException {
if (inboundFile == null || !inboundFile.exists()) {
return null;
}
FileInputStream is = new FileInputStream(inboundFile);
Inbound inboundSource = PayloadImpl.Inbound.newInstance("application/zip", is);
return inboundSource;
}
use of org.glassfish.api.admin.Payload.Inbound in project Payara by payara.
the class CheckpointHelper method loadOutbound.
private void loadOutbound(Outbound outbound, File outboundFile) throws IOException {
if (outbound == null || !outboundFile.exists()) {
return;
}
Inbound outboundSource = loadInbound(outboundFile);
Iterator<Part> parts = outboundSource.parts();
File topDir = createTempDir("checkpoint", "");
topDir.deleteOnExit();
while (parts.hasNext()) {
Part part = parts.next();
File sourceFile = File.createTempFile("source", "", topDir);
FileUtils.copy(part.getInputStream(), new FileOutputStream(sourceFile), Long.MAX_VALUE);
outbound.addPart(part.getContentType(), part.getName(), part.getProperties(), new FileInputStream(sourceFile));
}
outbound.resetDirty();
}
Aggregations