use of io.atlasmap.v2.Json in project atlasmap by atlasmap.
the class AtlasServiceTest method testProcessMapping2977.
@Test
public void testProcessMapping2977() throws Exception {
Response res = service.processMappingRequest(this.getClass().getClassLoader().getResourceAsStream("mappings/process-mapping-request-2977.json"), generateTestUriInfo("http://localhost:8686/v2/atlas", "http://localhost:8686/v2/atlas/mapping/process"));
ProcessMappingResponse resp = Json.mapper().readValue((byte[]) res.getEntity(), ProcessMappingResponse.class);
assertEquals(0, resp.getAudits().getAudit().size(), printAudit(resp.getAudits()));
Field field = resp.getMapping().getOutputField().get(0);
assertEquals("/ns:XmlOE/ns:Address/ns:addressLine1", field.getPath());
assertEquals("Boston", field.getValue());
}
use of io.atlasmap.v2.Json in project atlasmap by atlasmap.
the class AtlasServiceTest method testGetMapping.
@Test
public void testGetMapping() {
Response resp = service.getMappingRequest(JSON, 3);
assertEquals(204, resp.getStatus());
assertNull(resp.getEntity());
}
use of io.atlasmap.v2.Json in project atlasmap by atlasmap.
the class KafkaConnectInspectionServiceTest method testJsonComplex.
@Test
public void testJsonComplex() throws Exception {
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("json-complex.json");
KafkaConnectDocument doc = service.inspectJson(new String(is.readAllBytes()), options);
assertNotNull(doc);
assertEquals("struct", doc.getName());
assertEquals("/", doc.getPath());
assertEquals(FieldType.COMPLEX, doc.getFieldType());
List<Field> fields = doc.getFields().getField();
assertEquals(2, fields.size());
Field field1 = fields.get(0);
assertEquals("field1", field1.getName());
assertEquals("/field1", field1.getPath());
assertEquals(FieldType.BOOLEAN, field1.getFieldType());
Field field2 = fields.get(1);
assertEquals("field2", field2.getName());
assertEquals("/field2", field2.getPath());
assertEquals(FieldType.STRING, field2.getFieldType());
}
use of io.atlasmap.v2.Json in project atlasmap by atlasmap.
the class DefaultAtlasFieldActionService method createDetailFromProcessor.
private ActionProcessor createDetailFromProcessor(Class<?> clazz, Method method) {
AtlasActionProcessor annotation = method.getAnnotation(AtlasActionProcessor.class);
if (annotation == null) {
return null;
}
if (method.getParameterCount() < 1) {
LOG.debug("Invalid @AtlasActionProcessor method. Expected at least 1 parameter: " + method);
}
Class<? extends Action> actionClazz = null;
if (Action.class.isAssignableFrom(method.getParameterTypes()[0])) {
actionClazz = (Class<? extends Action>) method.getParameterTypes()[0];
} else {
LOG.debug("Invalid @AtlasActionProcessor method. 1st parameter does not subclass " + Action.class.getName() + ": " + method);
}
final Class<?> targetClass = method.getReturnType();
String name = actionResolver.toId(actionClazz);
ActionDetail det = new ActionDetail();
det.setClassName(clazz.getName());
det.setMethod(method.getName());
det.setName(name);
det.setTargetType(toFieldType(targetClass, method.getGenericReturnType()));
if (!clazz.getPackage().getName().equals("io.atlasmap.actions")) {
det.setCustom(true);
}
Type[] genericParameterTypes = method.getGenericParameterTypes();
if (genericParameterTypes.length >= 2) {
Class<?> sourceClass = method.getParameterTypes()[1];
if (annotation.sourceType() != FieldType.NONE) {
det.setSourceType(annotation.sourceType());
} else {
det.setSourceType(toFieldType(sourceClass, method.getGenericParameterTypes()[1]));
}
CollectionType sourceCollection = toFieldCollectionType(sourceClass);
CollectionType targetCollection = toFieldCollectionType(targetClass);
if (sourceCollection != CollectionType.NONE) {
if (targetCollection != CollectionType.NONE) {
det.setMultiplicity(Multiplicity.MANY_TO_MANY);
} else {
det.setMultiplicity(Multiplicity.MANY_TO_ONE);
}
} else if (targetCollection != CollectionType.NONE) {
det.setMultiplicity(Multiplicity.ONE_TO_MANY);
} else {
det.setMultiplicity(Multiplicity.ONE_TO_ONE);
}
} else if (genericParameterTypes.length == 1) {
det.setMultiplicity(Multiplicity.ZERO_TO_ONE);
}
try {
det.setActionSchema(actionClazz);
} catch (Exception e) {
LOG.error(String.format("Could not get json schema for action=%s msg=%s", clazz.getName(), e.getMessage()), e);
}
try {
det.setParameters(detectFieldActionParameters(actionClazz));
} catch (ClassNotFoundException e) {
LOG.error(String.format("Error detecting parameters for field action=%s msg=%s", det.getName(), e.getMessage()), e);
}
Object o = null;
try {
o = Modifier.isStatic(method.getModifiers()) ? clazz.getDeclaredConstructor().newInstance() : null;
} catch (Throwable e) {
LOG.error(String.format("Error creating object instance for action=%s msg=%s", det.getName(), e.getMessage()), e);
}
final Object object = o;
Class<? extends Action> finalActionClazz = actionClazz;
return new ActionProcessor() {
@Override
public ActionDetail getActionDetail() {
return det;
}
@Override
public Class<? extends Action> getActionClass() {
return finalActionClazz;
}
@Override
public Object process(Action action, Object sourceObject) throws AtlasException {
try {
if (det.getMultiplicity() == Multiplicity.ZERO_TO_ONE) {
return method.invoke(object, action);
} else {
sourceObject = convertSourceObject(sourceObject);
return method.invoke(object, action, sourceObject);
}
} catch (Throwable e) {
throw new AtlasException(String.format("Error processing action %s", det.getName()), e);
}
}
private Object convertSourceObject(Object sourceObject) throws AtlasConversionException {
if (sourceObject == null) {
return null;
}
Class<?> paramType;
paramType = method.getParameterTypes()[1];
CollectionType paramCollectionType = toFieldCollectionType(paramType);
CollectionType sourceCollectionType = toFieldCollectionType(sourceObject.getClass());
if (paramCollectionType != CollectionType.NONE) {
List<Object> sourceList;
Type itemType = method.getGenericParameterTypes()[1];
Class<?> itemClass = paramType.isArray() ? paramType.getComponentType() : (Class<?>) ((ParameterizedType) itemType).getActualTypeArguments()[0];
if (sourceCollectionType != CollectionType.NONE) {
if (sourceCollectionType == CollectionType.ARRAY) {
sourceList = Arrays.asList(sourceObject);
} else if (sourceCollectionType == CollectionType.LIST) {
sourceList = (List<Object>) sourceObject;
} else if (sourceCollectionType == CollectionType.MAP) {
sourceList = new ArrayList(((Map) sourceObject).values());
} else {
sourceList = new ArrayList((Collection) sourceObject);
}
} else {
sourceList = Arrays.asList(sourceObject);
}
convertItems(sourceList, itemClass);
if (paramType.isArray()) {
return sourceList.toArray();
} else {
return sourceList;
}
} else if (paramType.isInstance(sourceObject)) {
return sourceObject;
}
return conversionService.convertType(sourceObject, null, paramType, null);
}
};
}
use of io.atlasmap.v2.Json in project atlasmap by atlasmap.
the class DefaultAtlasFieldActionService method createDetailFromFieldActionInfo.
private ActionProcessor createDetailFromFieldActionInfo(final Class<?> clazz, final Method method) {
AtlasFieldActionInfo annotation = method.getAnnotation(AtlasFieldActionInfo.class);
if (annotation == null) {
return null;
}
final ActionDetail det = new ActionDetail();
det.setClassName(clazz.getName());
det.setMethod(method.getName());
det.setName(annotation.name());
det.setSourceType(annotation.sourceType());
det.setTargetType(annotation.targetType());
CollectionType sourceCollection = annotation.sourceCollectionType();
CollectionType targetCollection = annotation.sourceCollectionType();
if (sourceCollection != null && sourceCollection != CollectionType.NONE) {
det.setMultiplicity(Multiplicity.MANY_TO_ONE);
} else if (targetCollection != null && targetCollection != CollectionType.NONE) {
det.setMultiplicity(Multiplicity.ONE_TO_MANY);
} else {
det.setMultiplicity(Multiplicity.ONE_TO_ONE);
}
Class<? extends Action> actionClazz;
try {
actionClazz = (Class<? extends Action>) Class.forName("io.atlasmap.v2." + annotation.name());
} catch (Exception e) {
actionClazz = null;
det.setCustom(true);
}
try {
det.setActionSchema(actionClazz);
} catch (Exception e) {
LOG.error(String.format("Could not get json schema for action=%s msg=%s", annotation.name(), e.getMessage()), e);
}
try {
// TODO https://github.com/atlasmap/atlasmap/issues/538
if (det.isCustom() == null || !det.isCustom()) {
det.setParameters(detectFieldActionParameters(actionClazz));
}
} catch (ClassNotFoundException e) {
LOG.error(String.format("Error detecting parameters for field action=%s msg=%s", annotation.name(), e.getMessage()), e);
}
if (LOG.isTraceEnabled()) {
LOG.trace("Loaded FieldAction: " + det.getName());
}
Class<? extends Action> finalActionClazz = actionClazz;
return new ActionProcessor() {
@Override
public ActionDetail getActionDetail() {
return det;
}
@Override
public Class<? extends Action> getActionClass() {
return finalActionClazz;
}
@Override
public Object process(Action action, Object sourceObject) throws AtlasException {
Object targetObject = null;
try {
Object convertedSourceObject = convertSourceObject(sourceObject);
if (Modifier.isStatic(method.getModifiers())) {
// cf. https://github.com/atlasmap/atlasmap/issues/536
if (det.isCustom() != null && det.isCustom()) {
targetObject = det.getMultiplicity() == Multiplicity.ZERO_TO_ONE ? method.invoke(null) : method.invoke(null, convertedSourceObject);
} else {
targetObject = det.getMultiplicity() == Multiplicity.ZERO_TO_ONE ? method.invoke(null, action) : method.invoke(null, action, convertedSourceObject);
}
} else {
Object object = clazz.getDeclaredConstructor().newInstance();
if (det.isCustom() != null && det.isCustom()) {
targetObject = det.getMultiplicity() == Multiplicity.ZERO_TO_ONE ? method.invoke(object) : method.invoke(object, convertedSourceObject);
} else {
targetObject = det.getMultiplicity() == Multiplicity.ZERO_TO_ONE ? method.invoke(object, action) : method.invoke(object, action, convertedSourceObject);
}
}
} catch (Throwable e) {
throw new AtlasException(String.format("Error processing action %s", det.getName()), e);
}
return targetObject;
}
private Object convertSourceObject(Object sourceObject) throws AtlasConversionException {
Class<?> paramType;
int paramCount = method.getParameterCount();
if (paramCount < 2) {
return null;
}
paramType = method.getParameterTypes()[1];
if (paramType.isInstance(sourceObject)) {
return sourceObject;
}
return conversionService.convertType(sourceObject, null, paramType, null);
}
};
}
Aggregations