Search in sources :

Example 1 with ADMArchiveHandler

use of io.atlasmap.core.ADMArchiveHandler in project atlasmap by atlasmap.

the class NestedCollectionJavaTest method processNestedJavaCollection.

private TargetClass processNestedJavaCollection(List<String> mappingsToProcess) throws AtlasException {
    InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("mappings/atlasmapping-nested-collection-java.json");
    ADMArchiveHandler admHandler = new ADMArchiveHandler(Thread.currentThread().getContextClassLoader());
    admHandler.load(AtlasContextFactory.Format.JSON, in);
    AtlasMapping mapping = admHandler.getMappingDefinition();
    mapping.getMappings().getMapping().removeIf(m -> !mappingsToProcess.contains(((Mapping) m).getId()));
    AtlasContext context = DefaultAtlasContextFactory.getInstance().createContext(mapping);
    AtlasSession session = context.createSession();
    SourceClass sc = newSourceClass();
    session.setSourceDocument("io.atlasmap.itests.core.SourceClass", sc);
    context.process(session);
    assertFalse(session.hasErrors(), TestHelper.printAudit(session));
    return (TargetClass) session.getTargetDocument("io.atlasmap.itests.core.TargetClass");
}
Also used : AtlasMapping(io.atlasmap.v2.AtlasMapping) InputStream(java.io.InputStream) AtlasContext(io.atlasmap.api.AtlasContext) ADMArchiveHandler(io.atlasmap.core.ADMArchiveHandler) AtlasSession(io.atlasmap.api.AtlasSession)

Example 2 with ADMArchiveHandler

use of io.atlasmap.core.ADMArchiveHandler in project atlasmap by atlasmap.

the class NestedCollectionJsonTest method processJsonNestedCollection.

private JsonNode processJsonNestedCollection(List<String> mappingsToProcess, boolean assertNoWarnings) throws AtlasException, IOException, URISyntaxException {
    InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("mappings/atlasmapping-nested-collection-json.json");
    ADMArchiveHandler admHandler = new ADMArchiveHandler(Thread.currentThread().getContextClassLoader());
    admHandler.load(AtlasContextFactory.Format.JSON, in);
    AtlasMapping mapping = admHandler.getMappingDefinition();
    mapping.getMappings().getMapping().removeIf(m -> !mappingsToProcess.contains(((Mapping) m).getId()));
    AtlasContext context = DefaultAtlasContextFactory.getInstance().createContext(mapping);
    AtlasSession session = context.createSession();
    String source = new String(Files.readAllBytes(Paths.get(Thread.currentThread().getContextClassLoader().getResource("mappings/document-nested-collection.json").toURI())));
    session.setSourceDocument("JSONInstanceNestedCollection", source);
    context.process(session);
    assertFalse(session.hasErrors(), TestHelper.printAudit(session));
    if (assertNoWarnings) {
        assertFalse(session.hasWarns(), TestHelper.printAudit(session));
    }
    Object output = session.getTargetDocument("JSONInstanceNestedCollection");
    return mapper.readTree((String) output);
}
Also used : AtlasMapping(io.atlasmap.v2.AtlasMapping) InputStream(java.io.InputStream) AtlasContext(io.atlasmap.api.AtlasContext) ADMArchiveHandler(io.atlasmap.core.ADMArchiveHandler) AtlasSession(io.atlasmap.api.AtlasSession)

Example 3 with ADMArchiveHandler

use of io.atlasmap.core.ADMArchiveHandler in project atlasmap by atlasmap.

the class CsvMappingTest method createContext.

public AtlasContext createContext(String file, String... mappingIds) throws Exception {
    InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(file);
    ADMArchiveHandler admHandler = new ADMArchiveHandler(Thread.currentThread().getContextClassLoader());
    admHandler.load(AtlasContextFactory.Format.JSON, in);
    AtlasMapping mapping = admHandler.getMappingDefinition();
    List<String> ids = Arrays.asList(mappingIds);
    mapping.getMappings().getMapping().removeIf(m -> {
        if (m instanceof Mapping) {
            return !ids.contains(((Mapping) m).getId());
        } else if (m instanceof Collection) {
            Collection col = (Collection) m;
            col.getMappings().getMapping().removeIf(map -> !ids.contains(((Mapping) map).getId()));
            return col.getMappings().getMapping().isEmpty();
        } else {
            return false;
        }
    });
    AtlasContext context = DefaultAtlasContextFactory.getInstance().createContext(mapping);
    return context;
}
Also used : Arrays(java.util.Arrays) DefaultAtlasContextFactory(io.atlasmap.core.DefaultAtlasContextFactory) Test(org.junit.jupiter.api.Test) Mapping(io.atlasmap.v2.Mapping) List(java.util.List) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) AtlasMapping(io.atlasmap.v2.AtlasMapping) AtlasSession(io.atlasmap.api.AtlasSession) ADMArchiveHandler(io.atlasmap.core.ADMArchiveHandler) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Collection(io.atlasmap.v2.Collection) AtlasContext(io.atlasmap.api.AtlasContext) AtlasContextFactory(io.atlasmap.api.AtlasContextFactory) InputStream(java.io.InputStream) AtlasMapping(io.atlasmap.v2.AtlasMapping) InputStream(java.io.InputStream) AtlasContext(io.atlasmap.api.AtlasContext) Collection(io.atlasmap.v2.Collection) Mapping(io.atlasmap.v2.Mapping) AtlasMapping(io.atlasmap.v2.AtlasMapping) ADMArchiveHandler(io.atlasmap.core.ADMArchiveHandler)

Example 4 with ADMArchiveHandler

use of io.atlasmap.core.ADMArchiveHandler in project atlasmap by atlasmap.

the class E2ETest method test.

@Test
public void test() throws Exception {
    driver.get("http://127.0.0.1:" + port);
    WebDriverWait waitForLoad = new WebDriverWait(driver, Duration.ofSeconds(30));
    waitForLoad.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//article[@aria-label='Properties']")));
    WebElement atlasmapMenuBtn = driver.findElement(By.xpath("//button[@data-testid='atlasmap-menu-button']"));
    atlasmapMenuBtn.click();
    waitForLoad.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@data-testid='import-mappings-button']")));
    WebElement importBtn = driver.findElement(By.xpath("//a[@data-testid='import-mappings-button']"));
    importBtn.click();
    WebElement fileInput = driver.findElement(By.xpath("//div[@id='data-toolbar']//input[@type='file']"));
    String cwd = System.getProperty("user.dir");
    fileInput.sendKeys(cwd + "/src/test/resources/json-schema-source-to-xml-schema-target.adm");
    WebElement confirmBtn = driver.findElement(By.xpath("//button[@data-testid='confirmation-dialog-confirm-button']"));
    confirmBtn.click();
    waitForLoad.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//article[@aria-label='JSONSchemaSource']")));
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e1) {
    }
    // Check custom action param
    WebElement orderBtn = driver.findElement(By.xpath("//button[@id='sources-field-atlas:json:JSONSchemaSource:source:/order-toggle']"));
    orderBtn.click();
    By addressToggle = By.xpath("//button[@id='sources-field-atlas:json:JSONSchemaSource:source:/order/address-toggle']");
    waitForLoad.until(ExpectedConditions.visibilityOfElementLocated(addressToggle));
    WebElement addrBtn = driver.findElement(addressToggle);
    addrBtn.click();
    By addrDivPath = By.xpath("//button[@id='sources-field-atlas:json:JSONSchemaSource:source:/order/address-toggle']/../..");
    waitForLoad.until(ExpectedConditions.visibilityOfElementLocated(addrDivPath));
    WebElement addrDiv = driver.findElement(addrDivPath);
    By cityDivPath = By.xpath(".//button[@data-testid='grip-city-button']/../../../..");
    waitForLoad.until(ExpectedConditions.visibilityOfElementLocated(cityDivPath));
    WebElement cityDiv = addrDiv.findElement(cityDivPath);
    Actions action = new Actions(driver);
    action.moveToElement(cityDiv).perform();
    waitForLoad.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//button[@data-testid='show-mapping-details-button']")));
    WebElement showDetailsBtn = cityDiv.findElement(By.xpath(".//button[@data-testid='show-mapping-details-button']"));
    showDetailsBtn.click();
    WebElement detailsCity = waitForLoad.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@role='dialog']//div[@aria-labelledby='mapping-field-city']")));
    assertNotNull(detailsCity);
    WebElement customActionParamInput = driver.findElement(By.id("user-field-action-io.atlasmap.service.my.MyFieldActionsModel-transformation-0"));
    assertEquals("testparam", customActionParamInput.getAttribute("value"));
    // Check custom source class mapping
    WebElement customClassDoc = driver.findElement(By.xpath("//article[@aria-label='MyFieldActionsModel']"));
    WebElement paramDiv = customClassDoc.findElement(By.xpath(".//button[@data-testid='grip-param-button']/../../../.."));
    action = new Actions(driver);
    action.moveToElement(paramDiv).perform();
    waitForLoad.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@data-testid='show-mapping-details-button']")));
    showDetailsBtn = paramDiv.findElement(By.xpath(".//button[@data-testid='show-mapping-details-button']"));
    showDetailsBtn.click();
    WebElement detailsPhotoUrl = waitForLoad.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@data-testid='column-mapping-details-area']" + "//div[@data-testid='mapping-field-photoUrl']")));
    assertNotNull(detailsPhotoUrl);
    atlasmapMenuBtn = driver.findElement(By.xpath("//button[@data-testid='atlasmap-menu-button']"));
    atlasmapMenuBtn.click();
    waitForLoad.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[@data-testid='export-mappings-button']")));
    WebElement exportBtn = driver.findElement(By.xpath("//a[@data-testid='export-mappings-button']"));
    exportBtn.click();
    WebElement dialogDiv = driver.findElement(By.xpath("//div[@data-testid='export-catalog-dialog']/.."));
    WebElement exportInput = dialogDiv.findElement(By.id("filename"));
    String exportAdmFileName = UUID.randomUUID().toString() + "-exported.adm";
    exportInput.clear();
    exportInput.sendKeys(exportAdmFileName);
    confirmBtn = dialogDiv.findElement(By.xpath(".//button[@data-testid='confirmation-dialog-confirm-button']"));
    WatchService watcher = FileSystems.getDefault().newWatchService();
    Executors.newSingleThreadExecutor().execute(() -> {
        long start = System.currentTimeMillis();
        while ((System.currentTimeMillis() - start) < 300000) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                break;
            }
        }
        try {
            watcher.close();
        } catch (Exception e) {
            fail("Failed to close file watcher");
        }
    });
    Path dirPath = Paths.get(DLDIR);
    dirPath.register(watcher, StandardWatchEventKinds.ENTRY_CREATE);
    confirmBtn.click();
    WatchKey key = watcher.take();
    while (key.isValid()) {
        List<WatchEvent<?>> events = key.pollEvents();
        if (events.isEmpty()) {
            Thread.sleep(1000);
            continue;
        }
        for (WatchEvent<?> event : events) {
            if (!StandardWatchEventKinds.ENTRY_CREATE.name().equals(event.kind().name())) {
                continue;
            }
            ;
            Path eventPath = (Path) event.context();
            LOG.info("File '{}' is created", eventPath.getFileName().toString());
            if (!exportAdmFileName.equals(eventPath.getFileName().toString())) {
                continue;
            }
            ADMArchiveHandler handler = new ADMArchiveHandler(getClass().getClassLoader());
            handler.setLibraryDirectory(Paths.get(DLDIR + File.separator + "lib"));
            handler.load(Paths.get(DLDIR + File.separator + exportAdmFileName));
            assertEquals("UI.0", handler.getMappingDefinition().getName());
            DataSourceMetadata sourceMeta = handler.getDataSourceMetadata(true, "JSONSchemaSource");
            assertEquals(true, sourceMeta.getIsSource());
            assertEquals("JSONSchemaSource", sourceMeta.getName());
            assertEquals("JSON", sourceMeta.getDataSourceType());
            DataSourceMetadata targetMeta = handler.getDataSourceMetadata(false, "XMLSchemaSource");
            assertEquals(false, targetMeta.getIsSource());
            assertEquals("XMLSchemaSource", targetMeta.getName());
            assertEquals("XML", targetMeta.getDataSourceType());
            return;
        }
        ;
    }
    fail("exported.adm was not created");
}
Also used : Path(java.nio.file.Path) Actions(org.openqa.selenium.interactions.Actions) WatchKey(java.nio.file.WatchKey) WebElement(org.openqa.selenium.WebElement) DataSourceMetadata(io.atlasmap.v2.DataSourceMetadata) By(org.openqa.selenium.By) WebDriverWait(org.openqa.selenium.support.ui.WebDriverWait) WatchEvent(java.nio.file.WatchEvent) ADMArchiveHandler(io.atlasmap.core.ADMArchiveHandler) WatchService(java.nio.file.WatchService) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with ADMArchiveHandler

use of io.atlasmap.core.ADMArchiveHandler in project atlasmap by atlasmap.

the class AtlasService method getMappingRequest.

/**
 * Retrieve a mapping file saved on the server.
 * @param mappingFormat file type
 * @param mappingDefinitionId mapping definition ID
 * @return file
 */
@GET
@Path("/mapping/{mappingFormat}/{mappingDefinitionId}")
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML, MediaType.APPLICATION_OCTET_STREAM })
@Operation(summary = "Get Mapping", description = "Retrieve a mapping file saved on the server")
@ApiResponses({ @ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = AtlasMapping.class)), description = "Return a mapping file content"), @ApiResponse(responseCode = "204", description = "Mapping file was not found"), @ApiResponse(responseCode = "500", description = "Mapping file access error") })
public Response getMappingRequest(@Parameter(description = "Mapping Format") @PathParam("mappingFormat") MappingFileType mappingFormat, @Parameter(description = "Mapping ID") @PathParam("mappingDefinitionId") Integer mappingDefinitionId) {
    LOG.debug("getMappingRequest: {} '{}'", mappingFormat, mappingDefinitionId);
    ADMArchiveHandler admHandler = loadExplodedMappingDirectory(mappingDefinitionId);
    switch(mappingFormat) {
        case JSON:
            byte[] serialized = null;
            try {
                serialized = admHandler.getMappingDefinitionBytes();
            } catch (Exception e) {
                LOG.error("Error retrieving mapping definition file for ID:" + mappingDefinitionId, e);
                throw new WebApplicationException(e.getMessage(), e, Status.INTERNAL_SERVER_ERROR);
            }
            if (LOG.isDebugEnabled() && serialized != null) {
                LOG.debug(new String(serialized));
            }
            if (serialized == null) {
                LOG.debug("Mapping definition not found for ID:{}", mappingDefinitionId);
                return Response.noContent().build();
            }
            return Response.ok().entity(serialized).build();
        case GZ:
            try {
                if (admHandler.getGzippedADMDigestBytes() == null) {
                    LOG.debug("ADM Digest file not found for ID:{}", mappingDefinitionId);
                    return Response.noContent().build();
                }
                return Response.ok().entity(admHandler.getGzippedADMDigestBytes()).build();
            } catch (Exception e) {
                LOG.error("Error getting compressed ADM digest file.\n" + e.getMessage(), e);
                throw new WebApplicationException(e.getMessage(), e, Status.INTERNAL_SERVER_ERROR);
            }
        case ZIP:
            try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
                admHandler.setIgnoreLibrary(false);
                admHandler.setLibraryDirectory(Paths.get(this.libFolder));
                admHandler.export(out);
                return Response.ok().entity(out.toByteArray()).build();
            } catch (Exception e) {
                LOG.error("Error getting ADM archive file.\n" + e.getMessage(), e);
                throw new WebApplicationException(e.getMessage(), e, Status.INTERNAL_SERVER_ERROR);
            }
        default:
            throw new WebApplicationException("Unrecognized mapping format: " + mappingFormat, Status.INTERNAL_SERVER_ERROR);
    }
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) ADMArchiveHandler(io.atlasmap.core.ADMArchiveHandler) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WebApplicationException(javax.ws.rs.WebApplicationException) AtlasException(io.atlasmap.api.AtlasException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) Operation(io.swagger.v3.oas.annotations.Operation) ApiResponses(io.swagger.v3.oas.annotations.responses.ApiResponses)

Aggregations

ADMArchiveHandler (io.atlasmap.core.ADMArchiveHandler)11 AtlasMapping (io.atlasmap.v2.AtlasMapping)6 AtlasContext (io.atlasmap.api.AtlasContext)5 AtlasSession (io.atlasmap.api.AtlasSession)5 InputStream (java.io.InputStream)5 AtlasException (io.atlasmap.api.AtlasException)4 Operation (io.swagger.v3.oas.annotations.Operation)4 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)4 Path (javax.ws.rs.Path)4 Produces (javax.ws.rs.Produces)4 WebApplicationException (javax.ws.rs.WebApplicationException)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 IOException (java.io.IOException)3 UriBuilder (javax.ws.rs.core.UriBuilder)3 Test (org.junit.jupiter.api.Test)3 RequestBody (io.swagger.v3.oas.annotations.parameters.RequestBody)2 Consumes (javax.ws.rs.Consumes)2 GET (javax.ws.rs.GET)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)1