Search in sources :

Example 1 with MockAppConfigurer

use of co.cask.cdap.app.MockAppConfigurer in project cdap by caskdata.

the class IntegrationTestManager method deployApplication.

@Override
@SuppressWarnings("unchecked")
public ApplicationManager deployApplication(NamespaceId namespace, Class<? extends Application> applicationClz, @Nullable Config configObject, File... bundleEmbeddedJars) {
    // See if the application class comes from file or jar.
    // If it's from JAR, no need to trace dependency since it should already be in an application jar.
    URL appClassURL = applicationClz.getClassLoader().getResource(applicationClz.getName().replace('.', '/') + ".class");
    // Should never happen, otherwise the ClassLoader is broken
    Preconditions.checkNotNull(appClassURL, "Cannot find class %s from the classloader", applicationClz);
    String appConfig = "";
    Type configType = Artifacts.getConfigType(applicationClz);
    try {
        if (configObject != null) {
            appConfig = GSON.toJson(configObject);
        } else {
            configObject = (Config) TypeToken.of(configType).getRawType().newInstance();
        }
        // Create and deploy application jar
        File appJarFile = new File(tmpFolder, String.format("%s-1.0.0-SNAPSHOT.jar", applicationClz.getSimpleName()));
        try {
            if ("jar".equals(appClassURL.getProtocol())) {
                copyJarFile(appClassURL, appJarFile);
            } else {
                Location appJar = AppJarHelper.createDeploymentJar(locationFactory, applicationClz, new Manifest(), CLASS_ACCEPTOR, bundleEmbeddedJars);
                Files.copy(Locations.newInputSupplier(appJar), appJarFile);
            }
            applicationClient.deploy(namespace, appJarFile, appConfig);
        } finally {
            if (!appJarFile.delete()) {
                LOG.warn("Failed to delete temporary app jar {}", appJarFile.getAbsolutePath());
            }
        }
        // Extracts application id from the application class
        Application application = applicationClz.newInstance();
        MockAppConfigurer configurer = new MockAppConfigurer(application);
        application.configure(configurer, new DefaultApplicationContext<>(configObject));
        String applicationId = configurer.getName();
        return new RemoteApplicationManager(namespace.app(applicationId), clientConfig, restClient);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : Type(java.lang.reflect.Type) RemoteApplicationManager(co.cask.cdap.test.remote.RemoteApplicationManager) MockAppConfigurer(co.cask.cdap.app.MockAppConfigurer) Manifest(java.util.jar.Manifest) File(java.io.File) Application(co.cask.cdap.api.app.Application) URL(java.net.URL) IOException(java.io.IOException) Location(org.apache.twill.filesystem.Location)

Example 2 with MockAppConfigurer

use of co.cask.cdap.app.MockAppConfigurer in project cdap by caskdata.

the class UnitTestManager method deployApplication.

@Override
public ApplicationManager deployApplication(NamespaceId namespace, Class<? extends Application> applicationClz, @Nullable Config configObject, File... bundleEmbeddedJars) {
    Preconditions.checkNotNull(applicationClz, "Application class cannot be null.");
    Type configType = Artifacts.getConfigType(applicationClz);
    try {
        ArtifactId artifactId = new ArtifactId(namespace.getNamespace(), applicationClz.getSimpleName(), "1.0-SNAPSHOT");
        addAppArtifact(artifactId, applicationClz);
        if (configObject == null) {
            configObject = (Config) TypeToken.of(configType).getRawType().newInstance();
        }
        Application app = applicationClz.newInstance();
        MockAppConfigurer configurer = new MockAppConfigurer(app);
        app.configure(configurer, new DefaultApplicationContext<>(configObject));
        ApplicationId applicationId = new ApplicationId(namespace.getNamespace(), configurer.getName());
        ArtifactSummary artifactSummary = new ArtifactSummary(artifactId.getArtifact(), artifactId.getVersion());
        appFabricClient.deployApplication(Id.Application.fromEntityId(applicationId), new AppRequest(artifactSummary, configObject));
        return appManagerFactory.create(applicationId);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : Type(java.lang.reflect.Type) MockAppConfigurer(co.cask.cdap.app.MockAppConfigurer) ArtifactSummary(co.cask.cdap.api.artifact.ArtifactSummary) ArtifactId(co.cask.cdap.proto.id.ArtifactId) ApplicationId(co.cask.cdap.proto.id.ApplicationId) Application(co.cask.cdap.api.app.Application) TransactionFailureException(org.apache.tephra.TransactionFailureException) IOException(java.io.IOException) AppRequest(co.cask.cdap.proto.artifact.AppRequest)

Aggregations

Application (co.cask.cdap.api.app.Application)2 MockAppConfigurer (co.cask.cdap.app.MockAppConfigurer)2 IOException (java.io.IOException)2 Type (java.lang.reflect.Type)2 ArtifactSummary (co.cask.cdap.api.artifact.ArtifactSummary)1 AppRequest (co.cask.cdap.proto.artifact.AppRequest)1 ApplicationId (co.cask.cdap.proto.id.ApplicationId)1 ArtifactId (co.cask.cdap.proto.id.ArtifactId)1 RemoteApplicationManager (co.cask.cdap.test.remote.RemoteApplicationManager)1 File (java.io.File)1 URL (java.net.URL)1 Manifest (java.util.jar.Manifest)1 TransactionFailureException (org.apache.tephra.TransactionFailureException)1 Location (org.apache.twill.filesystem.Location)1