use of org.apache.syncope.core.persistence.api.entity.resource.ExternalResource in project syncope by apache.
the class ResourceTest method issue243.
@Test
public void issue243() {
ExternalResource csv = resourceDAO.find("resource-csv");
assertNotNull(csv);
int origMapItems = csv.getProvision(anyTypeDAO.findUser()).get().getMapping().getItems().size();
MappingItem newMapItem = entityFactory.newEntity(MappingItem.class);
newMapItem.setIntAttrName("TEST");
newMapItem.setExtAttrName("TEST");
newMapItem.setPurpose(MappingPurpose.PROPAGATION);
csv.getProvision(anyTypeDAO.findUser()).get().getMapping().add(newMapItem);
resourceDAO.save(csv);
resourceDAO.flush();
csv = resourceDAO.find("resource-csv");
assertNotNull(csv);
assertEquals(origMapItems + 1, csv.getProvision(anyTypeDAO.findUser()).get().getMapping().getItems().size());
}
use of org.apache.syncope.core.persistence.api.entity.resource.ExternalResource in project syncope by apache.
the class ResourceTest method save.
@Test
public void save() {
ExternalResource resource = entityFactory.newEntity(ExternalResource.class);
resource.setKey("ws-target-resource-save");
// specify the connector
ConnInstance connector = connInstanceDAO.find("88a7a819-dab5-46b4-9b90-0b9769eabdb8");
assertNotNull(connector);
resource.setConnector(connector);
Provision provision = entityFactory.newEntity(Provision.class);
provision.setAnyType(anyTypeDAO.findUser());
provision.setObjectClass(ObjectClass.ACCOUNT);
provision.setResource(resource);
resource.add(provision);
Mapping mapping = entityFactory.newEntity(Mapping.class);
mapping.setProvision(provision);
provision.setMapping(mapping);
// specify mappings
for (int i = 0; i < 3; i++) {
MappingItem item = entityFactory.newEntity(MappingItem.class);
item.setExtAttrName("test" + i);
item.setIntAttrName("nonexistent" + i);
item.setMandatoryCondition("false");
item.setPurpose(MappingPurpose.PULL);
mapping.add(item);
item.setMapping(mapping);
}
MappingItem connObjectKey = entityFactory.newEntity(MappingItem.class);
connObjectKey.setExtAttrName("username");
connObjectKey.setIntAttrName("username");
connObjectKey.setPurpose(MappingPurpose.PROPAGATION);
mapping.setConnObjectKeyItem(connObjectKey);
connObjectKey.setMapping(mapping);
// map a derived attribute
MappingItem derived = entityFactory.newEntity(MappingItem.class);
derived.setConnObjectKey(false);
derived.setExtAttrName("fullname");
derived.setIntAttrName("cn");
derived.setPurpose(MappingPurpose.PROPAGATION);
mapping.add(derived);
derived.setMapping(mapping);
// save the resource
ExternalResource actual = resourceDAO.save(resource);
assertNotNull(actual);
assertNotNull(actual.getProvision(anyTypeDAO.findUser()).get().getMapping());
resourceDAO.flush();
resourceDAO.detach(actual);
connInstanceDAO.detach(connector);
// assign the new resource to an user
User user = userDAO.findByUsername("rossini");
assertNotNull(user);
user.add(actual);
resourceDAO.flush();
// retrieve resource
resource = resourceDAO.find(actual.getKey());
assertNotNull(resource);
resourceDAO.refresh(resource);
// check connector
connector = connInstanceDAO.find("88a7a819-dab5-46b4-9b90-0b9769eabdb8");
assertNotNull(connector);
assertNotNull(connector.getResources());
assertNotNull(resource.getConnector());
assertTrue(resource.getConnector().equals(connector));
// check mappings
List<? extends MappingItem> items = resource.getProvision(anyTypeDAO.findUser()).get().getMapping().getItems();
assertNotNull(items);
assertEquals(5, items.size());
// check user
user = userDAO.findByUsername("rossini");
assertNotNull(user);
assertNotNull(user.getResources());
assertTrue(user.getResources().contains(actual));
}
use of org.apache.syncope.core.persistence.api.entity.resource.ExternalResource in project syncope by apache.
the class ResourceTest method delete.
@Test
public void delete() {
ExternalResource resource = resourceDAO.find("resource-testdb");
assertNotNull(resource);
// -------------------------------------
// Get originally associated connector
// -------------------------------------
ConnInstance connector = resource.getConnector();
assertNotNull(connector);
// -------------------------------------
// -------------------------------------
// Get originally associated users
// -------------------------------------
List<User> users = userDAO.findByResource(resource);
assertNotNull(users);
Set<String> userKeys = users.stream().map(Entity::getKey).collect(Collectors.toSet());
// -------------------------------------
// Get tasks
List<PropagationTask> propagationTasks = taskDAO.findAll(TaskType.PROPAGATION, resource, null, null, null, -1, -1, Collections.<OrderByClause>emptyList());
assertFalse(propagationTasks.isEmpty());
// delete resource
resourceDAO.delete(resource.getKey());
// close the transaction
resourceDAO.flush();
// resource must be removed
ExternalResource actual = resourceDAO.find("resource-testdb");
assertNull(actual);
// resource must be not referenced any more from users
userKeys.stream().map(key -> userDAO.find(key)).map(actualUser -> {
assertNotNull(actualUser);
return actualUser;
}).forEachOrdered((actualUser) -> {
userDAO.findAllResources(actualUser).forEach(res -> assertFalse(res.getKey().equalsIgnoreCase(resource.getKey())));
});
// resource must be not referenced any more from the connector
ConnInstance actualConnector = connInstanceDAO.find(connector.getKey());
assertNotNull(actualConnector);
actualConnector.getResources().forEach(res -> assertFalse(res.getKey().equalsIgnoreCase(resource.getKey())));
// there must be no tasks
propagationTasks.forEach(task -> assertNull(taskDAO.find(task.getKey())));
}
use of org.apache.syncope.core.persistence.api.entity.resource.ExternalResource in project syncope by apache.
the class AbstractAnyDataBinder method fill.
@SuppressWarnings({ "unchecked", "rawtypes" })
protected void fill(final Any any, final AnyTO anyTO, final AnyUtils anyUtils, final SyncopeClientCompositeException scce) {
// 0. aux classes
any.getAuxClasses().clear();
anyTO.getAuxClasses().stream().map(className -> anyTypeClassDAO.find(className)).forEachOrdered(auxClass -> {
if (auxClass == null) {
LOG.debug("Invalid " + AnyTypeClass.class.getSimpleName() + " {}, ignoring...", auxClass);
} else {
any.add(auxClass);
}
});
// 1. attributes
SyncopeClientException invalidValues = SyncopeClientException.build(ClientExceptionType.InvalidValues);
anyTO.getPlainAttrs().stream().filter(attrTO -> !attrTO.getValues().isEmpty()).forEach(attrTO -> {
PlainSchema schema = getPlainSchema(attrTO.getSchema());
if (schema != null) {
PlainAttr<?> attr = (PlainAttr<?>) any.getPlainAttr(schema.getKey()).orElse(null);
if (attr == null) {
attr = anyUtils.newPlainAttr();
((PlainAttr) attr).setOwner(any);
attr.setSchema(schema);
}
fillAttr(attrTO.getValues(), anyUtils, schema, attr, invalidValues);
if (attr.getValuesAsStrings().isEmpty()) {
attr.setOwner(null);
} else {
any.add(attr);
}
}
});
if (!invalidValues.isEmpty()) {
scce.addException(invalidValues);
}
SyncopeClientException requiredValuesMissing = checkMandatory(any, anyUtils);
if (!requiredValuesMissing.isEmpty()) {
scce.addException(requiredValuesMissing);
}
// 2. resources
anyTO.getResources().forEach(resourceKey -> {
ExternalResource resource = resourceDAO.find(resourceKey);
if (resource == null) {
LOG.debug("Invalid " + ExternalResource.class.getSimpleName() + " {}, ignoring...", resourceKey);
} else {
any.add(resource);
}
});
requiredValuesMissing = checkMandatoryOnResources(any, anyUtils.getAllResources(any));
if (!requiredValuesMissing.isEmpty()) {
scce.addException(requiredValuesMissing);
}
}
use of org.apache.syncope.core.persistence.api.entity.resource.ExternalResource in project syncope by apache.
the class VirAttrHandlerImpl method getValues.
private Map<VirSchema, List<String>> getValues(final Any<?> any, final Set<VirSchema> schemas) {
Set<ExternalResource> ownedResources = anyUtilsFactory.getInstance(any).getAllResources(any);
Map<VirSchema, List<String>> result = new HashMap<>();
Map<Provision, Set<VirSchema>> toRead = new HashMap<>();
for (VirSchema schema : schemas) {
if (ownedResources.contains(schema.getProvision().getResource())) {
VirAttrCacheValue virAttrCacheValue = virAttrCache.get(any.getType().getKey(), any.getKey(), schema.getKey());
if (virAttrCache.isValidEntry(virAttrCacheValue)) {
LOG.debug("Values for {} found in cache: {}", schema, virAttrCacheValue);
result.put(schema, virAttrCacheValue.getValues());
} else {
Set<VirSchema> schemasToRead = toRead.get(schema.getProvision());
if (schemasToRead == null) {
schemasToRead = new HashSet<>();
toRead.put(schema.getProvision(), schemasToRead);
}
schemasToRead.add(schema);
}
} else {
LOG.debug("Not considering {} since {} is not assigned to {}", schema, any, schema.getProvision().getResource());
}
}
for (Map.Entry<Provision, Set<VirSchema>> entry : toRead.entrySet()) {
LOG.debug("About to read from {}: {}", entry.getKey(), entry.getValue());
Optional<MappingItem> connObjectKeyItem = MappingUtils.getConnObjectKeyItem(entry.getKey());
String connObjectKeyValue = connObjectKeyItem.isPresent() ? mappingManager.getConnObjectKeyValue(any, entry.getKey()).orElse(null) : null;
if (!connObjectKeyItem.isPresent() || connObjectKeyValue == null) {
LOG.error("No ConnObjectKey or value found for {}, ignoring...", entry.getKey());
} else {
Set<MappingItem> linkingMappingItems = new HashSet<>();
linkingMappingItems.add(connObjectKeyItem.get());
linkingMappingItems.addAll(entry.getValue().stream().map(schema -> schema.asLinkingMappingItem()).collect(Collectors.toSet()));
Connector connector = connFactory.getConnector(entry.getKey().getResource());
try {
ConnectorObject connectorObject = connector.getObject(entry.getKey().getObjectClass(), AttributeBuilder.build(connObjectKeyItem.get().getExtAttrName(), connObjectKeyValue), MappingUtils.buildOperationOptions(linkingMappingItems.iterator()));
if (connectorObject == null) {
LOG.debug("No read from {} with filter '{} == {}'", entry.getKey(), connObjectKeyItem.get().getExtAttrName(), connObjectKeyValue);
} else {
entry.getValue().forEach(schema -> {
Attribute attr = connectorObject.getAttributeByName(schema.getExtAttrName());
if (attr != null) {
VirAttrCacheValue virAttrCacheValue = new VirAttrCacheValue();
virAttrCacheValue.setValues(attr.getValue());
virAttrCache.put(any.getType().getKey(), any.getKey(), schema.getKey(), virAttrCacheValue);
LOG.debug("Values for {} set in cache: {}", schema, virAttrCacheValue);
result.put(schema, virAttrCacheValue.getValues());
}
});
}
} catch (Exception e) {
LOG.error("Error reading from {}", entry.getKey(), e);
}
}
}
return result;
}
Aggregations