public void save() {
ExternalResource resource = new ExternalResource();
resource.setName("ws-target-resource-save");
// specify the connector
ConnInstance connector = connInstanceDAO.find(100L);
assertNotNull("connector not found", connector);
resource.setConnector(connector);
UMapping mapping = new UMapping();
mapping.setResource(resource);
resource.setUmapping(mapping);
// specify mappings
for (int i = 0; i < 3; i++) {
UMappingItem item = new UMappingItem();
item.setExtAttrName("test" + i);
item.setIntAttrName("nonexistent" + i);
item.setIntMappingType(IntMappingType.UserSchema);
item.setMandatoryCondition("false");
item.setPurpose(MappingPurpose.SYNCHRONIZATION);
mapping.addItem(item);
item.setMapping(mapping);
}
UMappingItem accountId = new UMappingItem();
accountId.setExtAttrName("username");
accountId.setIntAttrName("username");
accountId.setIntMappingType(IntMappingType.UserId);
accountId.setPurpose(MappingPurpose.PROPAGATION);
mapping.setAccountIdItem(accountId);
accountId.setMapping(mapping);
// map a derived attribute
UMappingItem derived = new UMappingItem();
derived.setAccountid(false);
derived.setExtAttrName("fullname");
derived.setIntAttrName("cn");
derived.setIntMappingType(IntMappingType.UserDerivedSchema);
derived.setPurpose(MappingPurpose.PROPAGATION);
mapping.addItem(derived);
derived.setMapping(mapping);
// save the resource
ExternalResource actual = resourceDAO.save(resource);
assertNotNull(actual);
assertNotNull(actual.getUmapping());
resourceDAO.flush();
resourceDAO.detach(actual);
resourceDAO.detach(connector);
// assign the new resource to an user
SyncopeUser user = userDAO.find(1L);
assertNotNull("user not found", user);
user.addResource(actual);
resourceDAO.flush();
// retrieve resource
resource = resourceDAO.find(actual.getName());
assertNotNull(resource);
// check connector
connector = connInstanceDAO.find(100L);
assertNotNull(connector);
assertNotNull(connector.getResources());
assertTrue(connector.getResources().contains(resource));
assertNotNull(resource.getConnector());
assertTrue(resource.getConnector().equals(connector));
// check mappings