use of org.eclipse.sirius.components.emf.services.IDAdapter in project sirius-components by eclipse-sirius.
the class EMFQueryServiceTests method createResourceWith4Elements.
private Resource createResourceWith4Elements() {
Map<String, EObject> cache = new HashMap<>();
EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage();
// $NON-NLS-1$
UUID ePackageUUID = UUID.nameUUIDFromBytes("ePackage".getBytes());
ePackage.eAdapters().add(new IDAdapter(ePackageUUID));
cache.put(ePackageUUID.toString(), ePackage);
EClass class1 = EcoreFactory.eINSTANCE.createEClass();
// $NON-NLS-1$
UUID class1UUID = UUID.nameUUIDFromBytes("class1".getBytes());
class1.eAdapters().add(new IDAdapter(class1UUID));
cache.put(class1UUID.toString(), ePackage);
EClass class2 = EcoreFactory.eINSTANCE.createEClass();
// $NON-NLS-1$
UUID class2UUID = UUID.nameUUIDFromBytes("class2".getBytes());
class2.eAdapters().add(new IDAdapter(class2UUID));
cache.put(class2UUID.toString(), ePackage);
EClass class3 = EcoreFactory.eINSTANCE.createEClass();
// $NON-NLS-1$
UUID class3UUID = UUID.nameUUIDFromBytes("class3".getBytes());
class3.eAdapters().add(new IDAdapter(class3UUID));
cache.put(class3UUID.toString(), ePackage);
ePackage.getEClassifiers().addAll(List.of(class1, class2, class3));
XMIResource resource = new XMIResourceImpl() {
@Override
protected EObject getEObjectByID(String id) {
return cache.get(id);
}
};
resource.getContents().add(ePackage);
return resource;
}
use of org.eclipse.sirius.components.emf.services.IDAdapter in project sirius-components by eclipse-sirius.
the class EMFQueryServiceTests method testEMFQueryServiceGetObjectById.
@Test
public void testEMFQueryServiceGetObjectById() {
EditingContext editingContext = this.createEditingContext();
IEditingContextEPackageService editingContextEPackageService = new IEditingContextEPackageService() {
@Override
public List<EPackage> getEPackages(String editingContextId) {
return List.of(EcorePackage.eINSTANCE);
}
};
IQueryService queryService = new EMFQueryService(editingContextEPackageService, List.of());
// @formatter:off
EObject eObjectToRetrieve = editingContext.getDomain().getResourceSet().getResources().get(0).getContents().get(0);
Optional<IDAdapter> optionalIDAdapter = eObjectToRetrieve.eAdapters().stream().filter(IDAdapter.class::isInstance).map(IDAdapter.class::cast).findFirst();
// $NON-NLS-1$
String id = optionalIDAdapter.map(IDAdapter::getId).map(Object::toString).orElse("");
// @formatter:on
// $NON-NLS-1$ //$NON-NLS-2$
QueryBasedObjectInput input = new QueryBasedObjectInput(UUID.randomUUID(), "aql:editingContext.getObjectById('" + id + "')", Map.of());
IPayload payload = queryService.execute(editingContext, input);
assertTrue(payload instanceof QueryBasedObjectSuccessPayload);
assertEquals(eObjectToRetrieve, ((QueryBasedObjectSuccessPayload) payload).getResult());
// $NON-NLS-1$ //$NON-NLS-2$
input = new QueryBasedObjectInput(UUID.randomUUID(), "aql:editingContext.getObjectById('" + id + "wrong')", Map.of());
payload = queryService.execute(editingContext, input);
assertTrue(payload instanceof ErrorPayload);
}
Aggregations