use of io.atlasmap.v2.Mappings in project atlasmap by atlasmap.
the class CamelAtlasmap14Test method test.
@Test
public void test() throws Exception {
URL url = Thread.currentThread().getContextClassLoader().getResource("mappings/issue/camel-atlasmap-14-mapping.json");
AtlasMapping mapping = mappingService.loadMapping(url, AtlasMappingFormat.JSON);
AtlasContext context = DefaultAtlasContextFactory.getInstance().createContext(mapping);
AtlasSession session = context.createSession();
session.setDefaultSourceDocument(generateTwitterStatus());
context.process(session);
assertFalse(TestHelper.printAudit(session), session.hasErrors());
Object output = session.getDefaultTargetDocument();
assertEquals(String.class, output.getClass());
ObjectMapper mapper = new ObjectMapper();
JsonNode outJson = mapper.readTree((String) output);
assertNotNull(outJson.get("TwitterScreenName__c"));
assertEquals("bobvila1982", outJson.get("TwitterScreenName__c").asText());
}
use of io.atlasmap.v2.Mappings in project atlasmap by atlasmap.
the class ConstantPropertyTest method test.
@Test
public void test() throws Exception {
URL url = Thread.currentThread().getContextClassLoader().getResource("mappings/issue/constant-property-mapping.xml");
AtlasMapping mapping = mappingService.loadMapping(url, AtlasMappingFormat.XML);
AtlasContext context = DefaultAtlasContextFactory.getInstance().createContext(mapping);
AtlasSession session = context.createSession();
context.process(session);
assertFalse(TestHelper.printAudit(session), session.hasErrors());
TargetClass output = TargetClass.class.cast(session.getTargetDocument("io.atlasmap.core.issue.TargetClass"));
assertEquals("testValue", output.getTargetName());
assertNotEquals("testPath", output.getTargetFirstName());
assertEquals(777, output.getTargetInteger());
System.setProperty("testProp", "testProp-sysProp");
System.setProperty("PATH", "PATH-sysProp");
context.process(session);
assertFalse(TestHelper.printAudit(session), session.hasErrors());
output = TargetClass.class.cast(session.getTargetDocument("io.atlasmap.core.issue.TargetClass"));
assertEquals("testProp-sysProp", output.getTargetName());
assertEquals("PATH-sysProp", output.getTargetFirstName());
assertEquals(777, output.getTargetInteger());
session.getProperties().put("testProp", "testProp-runtimeProp");
session.getProperties().put("PATH", "PATH-runtimeProp");
context.process(session);
assertFalse(TestHelper.printAudit(session), session.hasErrors());
output = TargetClass.class.cast(session.getTargetDocument("io.atlasmap.core.issue.TargetClass"));
assertEquals("testProp-runtimeProp", output.getTargetName());
assertEquals("PATH-runtimeProp", output.getTargetFirstName());
assertEquals(777, output.getTargetInteger());
}
use of io.atlasmap.v2.Mappings in project atlasmap by atlasmap.
the class DefaultAtlasContext method extractCollectionMappings.
private List<Mapping> extractCollectionMappings(DefaultAtlasSession session, BaseMapping baseMapping) throws AtlasException {
if (LOG.isDebugEnabled()) {
LOG.debug("Generating Source Mappings from mapping: {}", baseMapping);
}
if (!baseMapping.getMappingType().equals(MappingType.COLLECTION)) {
if (LOG.isDebugEnabled()) {
LOG.debug("Mapping is not a collection mapping, not cloning: {}", baseMapping);
}
return Arrays.asList((Mapping) baseMapping);
}
List<Mapping> mappings = new LinkedList<>();
for (BaseMapping m : ((Collection) baseMapping).getMappings().getMapping()) {
Mapping mapping = (Mapping) m;
Field sourceField = mapping.getInputField().get(0);
boolean sourceIsCollection = AtlasPath.isCollection(sourceField.getPath());
if (!sourceIsCollection) {
// just copy it over
if (LOG.isDebugEnabled()) {
LOG.debug("Internal mapping's source field is not a collection, not cloning: {}", mapping);
}
// output object to be created for our copied firstName value
for (Field f : mapping.getOutputField()) {
f.setPath(AtlasPath.overwriteCollectionIndex(f.getPath(), 0));
}
mappings.add(mapping);
continue;
}
AtlasModule module = resolveModule(FieldDirection.SOURCE, sourceField);
int sourceCollectionSize = module.getCollectionSize(session, sourceField);
if (LOG.isDebugEnabled()) {
LOG.debug("Internal mapping's source field is a collection. Cloning it for each item ({} clones): {}", sourceCollectionSize, mapping);
}
for (int i = 0; i < sourceCollectionSize; i++) {
Mapping cloneMapping = (Mapping) AtlasModelFactory.cloneMapping(mapping, false);
for (Field f : mapping.getInputField()) {
Field clonedField = module.cloneField(f);
clonedField.setPath(AtlasPath.overwriteCollectionIndex(clonedField.getPath(), i));
cloneMapping.getInputField().add(clonedField);
}
for (Field f : mapping.getOutputField()) {
Field clonedField = module.cloneField(f);
if (AtlasPath.isCollection(clonedField.getPath())) {
clonedField.setPath(AtlasPath.overwriteCollectionIndex(clonedField.getPath(), i));
}
cloneMapping.getOutputField().add(clonedField);
}
mappings.add(cloneMapping);
}
}
if (LOG.isDebugEnabled()) {
LOG.debug("Generated {} mappings from mapping: {}", mappings.size(), baseMapping);
}
((Collection) baseMapping).getMappings().getMapping().clear();
((Collection) baseMapping).getMappings().getMapping().addAll(mappings);
return mappings;
}
use of io.atlasmap.v2.Mappings in project atlasmap by atlasmap.
the class JsonJsonFlatMappingTest method testCreateJsonJavaFlatFieldMappings.
@Test
public void testCreateJsonJavaFlatFieldMappings() throws Exception {
AtlasMapping atlasMapping = generateJsonJsonFlatMapping();
AtlasMappingService atlasMappingService = new AtlasMappingService(Arrays.asList("io.atlasmap.v2", "io.atlasmap.json.v2"));
File path = new File("target/reference-mappings/jsonToJson");
path.mkdirs();
atlasMappingService.saveMappingAsFile(atlasMapping, new File(path, "atlasmapping-flatprimitive.xml"));
}
use of io.atlasmap.v2.Mappings in project atlasmap by atlasmap.
the class XmlJavaFlatMappingTest method generateXmlJavaFlatMapping.
protected AtlasMapping generateXmlJavaFlatMapping() {
AtlasMapping atlasMapping = AtlasModelFactory.createAtlasMapping();
atlasMapping.setName("XmlJavaFlatMapping");
atlasMapping.getDataSource().add(generateDataSource("atlas:xml?complexType=ns:XmlFlatPrimitiveAttribute", DataSourceType.SOURCE));
atlasMapping.getDataSource().add(generateDataSource("atlas:java?className=io.atlasmap.java.test.TargetFlatPrimitiveClass", DataSourceType.TARGET));
List<BaseMapping> mappings = atlasMapping.getMappings().getMapping();
// Add fieldMappings
for (String fieldName : FLAT_FIELDS) {
Mapping mfm = AtlasModelFactory.createMapping(MappingType.MAP);
mfm.getInputField().add(generateXmlField("/XmlFPA/", fieldName));
mfm.getOutputField().add(generateJavaField(fieldName));
mappings.add(mfm);
}
return atlasMapping;
}
Aggregations