Skip to content
This repository has been archived by the owner on Jul 3, 2018. It is now read-only.

Commit

Permalink
#2 successful test of dummy mechanism in sandbox
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-conway committed Jun 3, 2016
1 parent 1f1b4fa commit f8e47c2
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ public class PublishActionDescriptor {
*/
private String publishSourceAbsolutePath = "";

/**
* This is the path of any intermediary collection created as part of the
* publish operation. This will be set by the publish mechanism itself as a
* sort of 're-direct'. It may be a collection, or a file, if the
* pre-validate creates a bundle of some type.
* <p/>
* The publish mechanism will call the pre-validate. If an intermediary is
* created, that will be passed back in the {@link PublishResult} and set
* subsequently set in the {@link PublishActionDescriptor} passed to the
* actual publish method. The publish method will honor this intermediary
* path.
*/
private String publishIntermediaryAbsolutePath = "";

/**
* Map of free-form string parameters that may pass information to the
* publish mechanism to tune the publish behavior
Expand Down Expand Up @@ -76,6 +90,10 @@ public String toString() {
builder.append("publishSourceAbsolutePath=")
.append(publishSourceAbsolutePath).append(", ");
}
if (publishIntermediaryAbsolutePath != null) {
builder.append("publishIntermediaryAbsolutePath=")
.append(publishIntermediaryAbsolutePath).append(", ");
}
if (publishProperties != null) {
builder.append("publishProperties=").append(
toString(publishProperties.entrySet(), maxLen));
Expand All @@ -99,4 +117,20 @@ private String toString(Collection<?> collection, int maxLen) {
return builder.toString();
}

/**
* @return the publishIntermediaryAbsolutePath
*/
public String getPublishIntermediaryAbsolutePath() {
return publishIntermediaryAbsolutePath;
}

/**
* @param publishIntermediaryAbsolutePath
* the publishIntermediaryAbsolutePath to set
*/
public void setPublishIntermediaryAbsolutePath(
String publishIntermediaryAbsolutePath) {
this.publishIntermediaryAbsolutePath = publishIntermediaryAbsolutePath;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class PublishResult {
* The publish system will pass this intermediate DIP location between the
* result of the validate and the publication step.
*/
private String intermediateDipAbsolutePath;
private String intermediateDipAbsolutePath = "";

private PublishStatusEnum publishStatus;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,59 @@
/**
*
*
*/
package org.iplantc.de.publish.sandbox;

import java.util.List;

import org.iplantc.de.publish.discovery.PublisherDiscoveryConfiguration;
import org.iplantc.de.publish.discovery.PublisherDiscoveryService;
import org.iplantc.de.publish.mechanism.api.PublishActionDescriptor;
import org.iplantc.de.publish.mechanism.api.PublishContext;
import org.iplantc.de.publish.mechanism.api.PublishMechanism;
import org.iplantc.de.publish.mechanism.api.PublishResult;
import org.iplantc.de.publish.mechanism.api.PublisherPluginDescription;
import org.iplantc.de.publish.mechanism.api.exception.PublicationException;
import org.iplantc.de.publish.mechanism.api.exception.PublicationRuntimeException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Black box host of publishing mechanisms for testing and validation purposes
*
*
* @author Mike Conway - DICE
*
*/
public class PublisherEmulator {

/**
* provided configuration to tune emulator
*/
private SandboxConfiguration sandboxConfiguration;
/**
* service that will load plugin jars for publishers
*/
private PublisherDiscoveryService publisherDiscoveryService;
private List<PublisherPluginDescription> publisherPluginDescriptions;
private PublishContext publishContext;

public static final Logger log = LoggerFactory
.getLogger(PublisherEmulator.class);

/**
* @return the publishContext
*/
public PublishContext getPublishContext() {
return publishContext;
}

/**
* @param publishContext
* the publishContext to set
*/
public void setPublishContext(final PublishContext publishContext) {
this.publishContext = publishContext;
}

/**
* Default (no values) constructor, note that sandboxConfiguration must be
* provided, and init() must be called
Expand All @@ -47,17 +74,101 @@ public SandboxConfiguration getSandboxConfiguration() {
* the sandboxConfiguration to set
*/
public void setSandboxConfiguration(
SandboxConfiguration sandboxConfiguration) {
final SandboxConfiguration sandboxConfiguration) {
this.sandboxConfiguration = sandboxConfiguration;
}

public PublishResult triggerPublicationLifecycle(String mechanismName,
PublishActionDescriptor publishActionDescriptor)
throws PublicationException {

log.info("triggerPublicationLifecycle()");
if (publishActionDescriptor == null) {
throw new IllegalArgumentException("null publishActionDescriptor");
}
if (mechanismName == null || mechanismName.isEmpty()) {
throw new IllegalArgumentException("null mechanismName");
}
validateInit();

log.info("looking for mechanism:{}", mechanismName);

PublisherPluginDescription requestedPlugin = null;

for (PublisherPluginDescription description : publisherPluginDescriptions) {
if (description.getPublisherName().equals(mechanismName)) {
requestedPlugin = description;
break;
}
}

if (requestedPlugin == null) {
log.error("unable to find plugin with name:{}", mechanismName);
throw new PublicationException("unknown plugin requested");
}

try {
PublishMechanism publishMechanism = (PublishMechanism) requestedPlugin
.getPublisherClass().newInstance();

PublishResult result = publishMechanism.preValidate(
publishActionDescriptor, publishContext);
if (result.getResponseCode() != PublishResult.PUBLISH_RESULT_NORMAL) {
log.warn("invalid response:{}", result);
log.warn("for publication request:{}", publishActionDescriptor);
return result;

}

log.info("passed pre-validate...now do actual publish...");

// TODO: clarify this switching of publish 'target' if prevalidate
// creates an intermediary

if (!result.getIntermediateDipAbsolutePath().isEmpty()) {
log.info("redirecting to intermediate DIP:{}",
result.getIntermediateDipAbsolutePath());
publishActionDescriptor
.setPublishIntermediaryAbsolutePath(result
.getIntermediateDipAbsolutePath());
}

result = publishMechanism.publish(publishActionDescriptor,
publishContext);
log.info("result of publication:{}", result);
return result;

} catch (InstantiationException | IllegalAccessException e) {
log.error("cannot create plugin instance from class:{}",
requestedPlugin);
throw new PublicationRuntimeException(
"cannot create plugin instance", e);
}

}

/**
* Initialize publisher based on (required) sandbox configuration
*
*
* @throws PublicationException
*/
public void init() throws PublicationException {
log.info("init()");
validateInit();

PublisherDiscoveryConfiguration config = new PublisherDiscoveryConfiguration();
config.setJarFilePluginDir(sandboxConfiguration.getJarFilePluginDir());
publisherDiscoveryService = new PublisherDiscoveryService();
publisherDiscoveryService.setPublisherDiscoveryConfiguration(config);
log.info("initializing plugin discovery service to load plugins");
publisherDiscoveryService.init();
publisherPluginDescriptions = publisherDiscoveryService
.listPublisherDescriptions();
log.info("found plugins:{}", publisherPluginDescriptions);

}

private void validateInit() throws PublicationException {
if (sandboxConfiguration == null) {
throw new PublicationException(
"init() cannot be called, no provided sandboxConfiguration");
Expand All @@ -69,16 +180,10 @@ public void init() throws PublicationException {
"no jar file plugin directory specified");
}

PublisherDiscoveryConfiguration config = new PublisherDiscoveryConfiguration();
config.setJarFilePluginDir(sandboxConfiguration.getJarFilePluginDir());
publisherDiscoveryService = new PublisherDiscoveryService();
publisherDiscoveryService.setPublisherDiscoveryConfiguration(config);
log.info("initializing plugin discovery service to load plugins");
publisherDiscoveryService.init();
this.publisherPluginDescriptions = publisherDiscoveryService
.listPublisherDescriptions();
log.info("found plugins:{}", publisherPluginDescriptions);

if (publishContext == null) {
log.error("cannot init, no publish context set");
throw new PublicationException("cannot init, no publish context");
}
}

public List<PublisherPluginDescription> listLoadedPlugins()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,15 @@

import junit.framework.Assert;

import org.iplantc.de.publish.mechanism.api.PublishActionDescriptor;
import org.iplantc.de.publish.mechanism.api.PublishContext;
import org.iplantc.de.publish.mechanism.api.PublishResult;
import org.iplantc.de.publish.mechanism.api.PublisherPluginDescription;
import org.iplantc.de.publish.mechanism.api.exception.PublicationException;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.mockito.Mockito;

/**
* @author Mike Conway - DICE
Expand Down Expand Up @@ -45,6 +49,9 @@ public void testInitWithConfig() throws PublicationException {
.setJarFilePluginDir(testingProperties
.getProperty(PublisherTestingProperties.TEST_PUBLISHER_DIR_PROPERTY));
PublisherEmulator emulator = new PublisherEmulator();
PublishContext publishContext = Mockito.mock(PublishContext.class);

emulator.setPublishContext(publishContext);
emulator.setSandboxConfiguration(sandboxConfiguration);
emulator.init();

Expand All @@ -57,6 +64,9 @@ public void testListPublicationDescriptions() throws PublicationException {
.setJarFilePluginDir(testingProperties
.getProperty(PublisherTestingProperties.TEST_PUBLISHER_DIR_PROPERTY));
PublisherEmulator emulator = new PublisherEmulator();
PublishContext publishContext = Mockito.mock(PublishContext.class);

emulator.setPublishContext(publishContext);
emulator.setSandboxConfiguration(sandboxConfiguration);
emulator.init();
List<PublisherPluginDescription> descriptions = emulator
Expand All @@ -65,4 +75,24 @@ public void testListPublicationDescriptions() throws PublicationException {

}

@Test
public void testPublish() throws PublicationException {
SandboxConfiguration sandboxConfiguration = new SandboxConfiguration();
sandboxConfiguration
.setJarFilePluginDir(testingProperties
.getProperty(PublisherTestingProperties.TEST_PUBLISHER_DIR_PROPERTY));
PublisherEmulator emulator = new PublisherEmulator();
PublishContext publishContext = Mockito.mock(PublishContext.class);

emulator.setPublishContext(publishContext);
emulator.setSandboxConfiguration(sandboxConfiguration);
emulator.init();
PublishActionDescriptor descriptor = new PublishActionDescriptor();
descriptor.setPublishSourceAbsolutePath("/a/path");
PublishResult result = emulator.triggerPublicationLifecycle(
PublisherTestingProperties.DUMMY_PUBLISHER, descriptor);
Assert.assertNotNull("null result", result);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
public class PublisherTestingProperties {

public static final String TEST_PUBLISHER_DIR_PROPERTY = "driver.jar.dir";
public static final String DUMMY_PUBLISHER = "Dummy Logging";

/**
*
Expand Down

0 comments on commit f8e47c2

Please sign in to comment.