use of io.atlasmap.v2.Json in project atlasmap by atlasmap.
the class JsonFieldReader method read.
@Override
public Field read(AtlasInternalSession session) throws AtlasException {
Field field = session.head().getSourceField();
if (rootNode == null) {
AtlasUtil.addAudit(session, field, String.format("Cannot read a field '%s' of JSON document '%s', document is null", field.getPath(), field.getDocId()), AuditStatus.ERROR, null);
return field;
}
AtlasPath path = new AtlasPath(field.getPath());
List<Field> fields = getJsonFieldsForPath(session, rootNode, field, path, 0);
if (path.hasCollection() && !path.isIndexedCollection()) {
FieldGroup fieldGroup = AtlasModelFactory.createFieldGroupFrom(field, true);
fieldGroup.getField().addAll(fields);
session.head().setSourceField(fieldGroup);
return fieldGroup;
} else if (fields.size() == 1) {
field.setValue(fields.get(0).getValue());
return field;
} else {
return field;
}
}
use of io.atlasmap.v2.Json in project atlasmap by atlasmap.
the class JavaModule method populateTargetField.
@Override
public void populateTargetField(AtlasInternalSession session) throws AtlasException {
Field sourceField = session.head().getSourceField();
Field targetField = session.head().getTargetField();
AtlasPath path = new AtlasPath(targetField.getPath());
FieldGroup targetFieldGroup = null;
if (path.hasCollection() && !path.isIndexedCollection()) {
targetFieldGroup = AtlasModelFactory.createFieldGroupFrom(targetField, true);
session.head().setTargetField(targetFieldGroup);
}
JavaFieldWriter writer = session.getFieldWriter(getDocId(), JavaFieldWriter.class);
if (targetFieldGroup == null) {
if (sourceField instanceof FieldGroup) {
List<Field> subFields = ((FieldGroup) sourceField).getField();
if (subFields == null || subFields.size() == 0) {
return;
}
Integer index = targetField.getIndex();
if (index != null) {
if (subFields.size() > index) {
sourceField = subFields.get(index);
} else {
AtlasUtil.addAudit(session, getDocId(), String.format("The number of source fields (%s) is smaller than target index (%s) - ignoring", subFields.size(), index), AuditStatus.WARN, null);
return;
}
} else {
// The last one wins for compatibility
sourceField = subFields.get(subFields.size() - 1);
}
session.head().setSourceField(sourceField);
}
/* Lazy parent instantiation, unless specific mapping defined for a complex type (Example json -> java)
Only instantiate the parent if there is a child value to avoid null src class -> empty dst class mapping
This will ensure null src class maps to null destination class
*/
Object parentObject = null;
if (null != sourceField.getValue() || (null == sourceField.getValue() && targetField.getFieldType() == FieldType.COMPLEX && !(targetField instanceof JavaEnumField))) {
parentObject = writer.prepareParentObject(session);
}
if (parentObject != null) {
writer.populateTargetFieldValue(session, parentObject);
writer.enqueueFieldAndParent(targetField, parentObject);
}
} else if (sourceField instanceof FieldGroup) {
if (((FieldGroup) sourceField).getField().size() == 0) {
Object parentObject = writer.prepareParentObject(session);
if (parentObject != null) {
writer.enqueueFieldAndParent(targetFieldGroup, parentObject);
}
}
Field previousTargetSubField = null;
for (int i = 0; i < ((FieldGroup) sourceField).getField().size(); i++) {
Field sourceSubField = ((FieldGroup) sourceField).getField().get(i);
Field targetSubField = targetField instanceof JavaEnumField ? new JavaEnumField() : new JavaField();
AtlasJavaModelFactory.copyField(targetField, targetSubField, false);
getCollectionHelper().copyCollectionIndexes(sourceField, sourceSubField, targetSubField, previousTargetSubField);
previousTargetSubField = targetSubField;
targetFieldGroup.getField().add(targetSubField);
session.head().setSourceField(sourceSubField);
session.head().setTargetField(targetSubField);
Object parentObject = writer.prepareParentObject(session);
if (parentObject != null) {
writer.populateTargetFieldValue(session, parentObject);
writer.enqueueFieldAndParent(targetSubField, parentObject);
}
}
session.head().setSourceField(sourceField);
session.head().setTargetField(targetFieldGroup);
} else {
Field targetSubField = targetField instanceof JavaEnumField ? new JavaEnumField() : new JavaField();
AtlasJavaModelFactory.copyField(targetField, targetSubField, false);
path.setVacantCollectionIndex(0);
targetSubField.setPath(path.toString());
targetFieldGroup.getField().add(targetSubField);
session.head().setTargetField(targetSubField);
Object parentObject = writer.prepareParentObject(session);
if (parentObject != null) {
writer.populateTargetFieldValue(session, parentObject);
writer.enqueueFieldAndParent(targetSubField, parentObject);
}
session.head().setTargetField(targetFieldGroup);
}
if (LOG.isDebugEnabled()) {
LOG.debug("{}: processTargetFieldMapping completed: SourceField:[docId={}, path={}, type={}, value={}], TargetField:[docId={}, path={}, type={}, value={}]", getDocId(), sourceField.getDocId(), sourceField.getPath(), sourceField.getFieldType(), sourceField.getValue(), targetField.getDocId(), targetField.getPath(), targetField.getFieldType(), targetField.getValue());
}
}
use of io.atlasmap.v2.Json 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");
}
use of io.atlasmap.v2.Json in project syndesis-qe by syndesisio.
the class UiComplexSteps method dbToDbIntegrationWithPeriodMs.
@Given("^db to db \"([^\"]*)\" integration with period (\\d+) ms$")
public void dbToDbIntegrationWithPeriodMs(String integrationName, int ms) throws IOException {
final Connection dbConnection = connectionsEndpoint.get(getDbConnectionId());
final Connector dbConnector = connectorsEndpoint.get("sql");
final String sqlStartQuery = "SELECT * FROM CONTACT";
final String sqlFinishQuery = "INSERT INTO TODO(task, completed) VALUES (:#TASK, 2)";
final String datamapperTemplate = "db-db.json";
// 1. @Then("^create start DB periodic sql invocation action step with query \"([^\"]*)\" and period \"([^\"]*)\" ms")
final Action dbAction1 = TestUtils.findConnectorAction(dbConnector, "sql-start-connector");
final Map<String, String> properties1 = TestUtils.map("query", sqlStartQuery, "schedulerPeriod", ms);
final ConnectorDescriptor connectorDescriptor1 = getConnectorDescriptor(dbAction1, properties1, dbConnection.getId().get());
// to be reported: period is not part of .json step (when checked via browser).
final Step dbStep1 = new Step.Builder().stepKind(StepKind.endpoint).id(UUID.randomUUID().toString()).connection(dbConnection).action(dbAction1).configuredProperties(properties1).build();
steps.getStepDefinitions().add(new StepDefinition(dbStep1, connectorDescriptor1));
// 2.A @And("start mapper definition with name: \"([^\"]*)\"")
String mapperName = "mapping 1";
final Step mapperStep = new Step.Builder().stepKind(StepKind.mapper).name(mapperName).build();
steps.getStepDefinitions().add(new StepDefinition(mapperStep, new DataMapperDefinition()));
// 2.B @Then("MAP using Step (\\d+) and field \"([^\"]*)\" to \"([^\"]*)\"")
int fromStep = 1;
String fromField = "first_name";
String toField = "TASK";
DataMapperStepDefinition newDmStep = new DataMapperStepDefinition();
newDmStep.setFromStep(fromStep);
newDmStep.setInputFields(Arrays.asList(fromField));
newDmStep.setOutputFields(Arrays.asList(toField));
newDmStep.setMappingType(MappingType.MAP);
newDmStep.setStrategy(null);
steps.getLastStepDefinition().getDataMapperDefinition().get().getDataMapperStepDefinition().add(newDmStep);
// 3. @Then("^create finish DB invoke sql action step with query \"([^\"]*)\"")
final Action dbAction2 = TestUtils.findConnectorAction(dbConnector, "sql-connector");
final Map<String, String> properties2 = TestUtils.map("query", sqlFinishQuery);
final ConnectorDescriptor connectorDescriptor2 = getConnectorDescriptor(dbAction2, properties2, dbConnection.getId().get());
final Step dbStep2 = new Step.Builder().stepKind(StepKind.endpoint).id(UUID.randomUUID().toString()).connection(dbConnection).action(dbAction2).configuredProperties(properties2).build();
steps.getStepDefinitions().add(new StepDefinition(dbStep2, connectorDescriptor2));
// 4. @When("^create integration with name: \"([^\"]*)\"")
processMapperSteps();
Integration integration = new Integration.Builder().steps(steps.getSteps()).name(integrationName).description("Awkward UI integration.").build();
log.info("Creating integration {}", integration.getName());
String integrationId = integrationsEndpoint.create(integration).getId().get();
log.info("Publish integration with ID: {}", integrationId);
publishIntegration(integrationId);
log.debug("Flushing used steps");
steps.flushStepDefinitions();
// 5. @Then("^wait for integration with name: \"([^\"]*)\" to become active")
final List<Integration> integrations = integrationsEndpoint.list().stream().filter(item -> item.getName().equals(integrationName)).collect(Collectors.toList());
final long start = System.currentTimeMillis();
// wait for activation
log.info("Waiting until integration \"{}\" becomes active. This may take a while...", integrationName);
integrationOverviewEndpoint = new IntegrationOverviewEndpoint(integrationId);
final IntegrationOverview integrationOverview = integrationOverviewEndpoint.getOverview();
final boolean activated = TestUtils.waitForPublishing(integrationOverviewEndpoint, integrationOverview, TimeUnit.MINUTES, 10);
Assertions.assertThat(activated).isEqualTo(true);
log.info("Integration pod has been started. It took {}s to build the integration.", TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - start));
}
use of io.atlasmap.v2.Json in project syndesis-qe by syndesisio.
the class AtlasMapperGenerator method createDataSource.
/**
* Used to generate data source elements for atlasMapping. There are three types of them: Java, Json, XML.
*
* TODO(tplevko): update also for XML
*
* @param dataShape
* @param step
* @param dataSourceType
* @return
*/
private DataSource createDataSource(DataShape dataShape, StepDefinition step, DataSourceType dataSourceType) {
DataShapeKinds dataShapeKind = dataShape.getKind();
DataSource source = null;
if (dataShapeKind.toString().contains("json")) {
source = new JsonDataSource();
source.setUri("atlas:" + "json:" + step.getStep().getId().get());
} else if (dataShapeKind.toString().contains("java")) {
source = new DataSource();
source.setUri("atlas:" + "java:" + step.getStep().getId().get() + "?className=" + dataShape.getType());
} else if (dataShapeKind.toString().contains("xml")) {
source = new XmlDataSource();
// TODO(tplevko): find out how should look the XML datasource definition
}
source.setId(step.getStep().getId().get());
source.setDataSourceType(dataSourceType);
return source;
}
Aggregations