Search in sources :

Example 1 with MockAppConfigurer

use of io.cdap.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);
    RemoteApplicationManager manager;
    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-%s.jar", applicationClz.getSimpleName(), VERSION));
        try {
            if ("jar".equals(appClassURL.getProtocol())) {
                copyJarFile(appClassURL, appJarFile);
            } else {
                Manifest manifest = new Manifest();
                manifest.getMainAttributes().put(ManifestFields.BUNDLE_VERSION, VERSION);
                Location appJar = AppJarHelper.createDeploymentJar(locationFactory, applicationClz, manifest, CLASS_ACCEPTOR, bundleEmbeddedJars);
                try (InputStream input = appJar.getInputStream()) {
                    Files.copy(input, appJarFile.toPath());
                }
            }
            // 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();
            // Upload artifact for application
            ContentProvider<InputStream> artifactStream = locationFactory.create(appJarFile.toURI())::getInputStream;
            artifactClient.add(namespace, applicationClz.getSimpleName(), artifactStream, VERSION);
            List<ArtifactSummary> deployedArtifacts = artifactClient.list(namespace);
            assert deployedArtifacts.size() > 0;
            // Deploy application
            ArtifactSummary summary = new ArtifactSummary(applicationClz.getSimpleName(), VERSION);
            AppRequest<?> request = new AppRequest<>(summary, appConfig);
            ApplicationId id = namespace.app(applicationId);
            applicationClient.deploy(id, request);
            manager = new RemoteApplicationManager(namespace.app(applicationId), clientConfig, restClient);
        } finally {
            if (!appJarFile.delete()) {
                LOG.warn("Failed to delete temporary app jar {}", appJarFile.getAbsolutePath());
            }
        }
        return manager;
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : RemoteApplicationManager(io.cdap.cdap.test.remote.RemoteApplicationManager) MockAppConfigurer(io.cdap.cdap.app.MockAppConfigurer) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Manifest(java.util.jar.Manifest) URL(java.net.URL) IOException(java.io.IOException) AppRequest(io.cdap.cdap.proto.artifact.AppRequest) Type(java.lang.reflect.Type) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) File(java.io.File) Application(io.cdap.cdap.api.app.Application) Location(org.apache.twill.filesystem.Location)

Example 2 with MockAppConfigurer

use of io.cdap.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) throws AccessException {
    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, new Manifest(), bundleEmbeddedJars);
        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 (AccessException e) {
        throw e;
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : Type(java.lang.reflect.Type) MockAppConfigurer(io.cdap.cdap.app.MockAppConfigurer) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) AccessException(io.cdap.cdap.api.security.AccessException) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) Manifest(java.util.jar.Manifest) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) Application(io.cdap.cdap.api.app.Application) TransactionFailureException(org.apache.tephra.TransactionFailureException) AccessException(io.cdap.cdap.api.security.AccessException) IOException(java.io.IOException) AppRequest(io.cdap.cdap.proto.artifact.AppRequest)

Aggregations

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