@Test public void delete() { PlainSchema firstname = plainSchemaDAO.find("firstname"); plainSchemaDAO.delete(firstname.getKey()); PlainSchema actual = plainSchemaDAO.find("firstname"); assertNull("delete did not work", actual); }
@Test public void issueSYNCOPE418() { PlainSchema schema = entityFactory.newEntity(PlainSchema.class); schema.setKey("http://schemas.examples.org/security/authorization/organizationUnit"); try { plainSchemaDAO.save(schema); fail(); } catch (InvalidEntityException e) { assertTrue(e.hasViolation(EntityViolationType.InvalidName)); } }
@Override public A findByAttrUniqueValue(final String schemaKey, final PlainAttrValue attrUniqueValue) { PlainSchema schema = plainSchemaDAO.find(schemaKey); if (schema == null) { LOG.error("Invalid schema name '{}'", schemaKey); return null; } if (!schema.isUniqueConstraint()) { LOG.error("This schema has not unique constraint: '{}'", schemaKey); return null; } List<A> result = findByAttrValue(schemaKey, attrUniqueValue); return result.isEmpty() ? null : result.iterator().next(); }
@Override @SuppressWarnings("unchecked") public List<A> findByAttrValue(final String schemaKey, final PlainAttrValue attrValue) { PlainSchema schema = plainSchemaDAO.find(schemaKey); if (schema == null) { LOG.error("Invalid schema name '{}'", schemaKey); return Collections.<A>emptyList(); } String entityName = schema.isUniqueConstraint() ? getAnyUtils().plainAttrUniqueValueClass().getName() : getAnyUtils().plainAttrValueClass().getName(); Query query = findByAttrValueQuery(entityName); query.setParameter("schemaKey", schemaKey); query.setParameter("stringValue", attrValue.getStringValue()); query.setParameter( "booleanValue", attrValue.getBooleanValue() == null ? null : ((AbstractPlainAttrValue) attrValue) .getBooleanAsInteger(attrValue.getBooleanValue())); if (attrValue.getDateValue() == null) { query.setParameter("dateValue", null); } else { query.setParameter("dateValue", attrValue.getDateValue(), TemporalType.TIMESTAMP); } query.setParameter("longValue", attrValue.getLongValue()); query.setParameter("doubleValue", attrValue.getDoubleValue()); List<A> result = new ArrayList<>(); for (PlainAttrValue value : (List<PlainAttrValue>) query.getResultList()) { A any = (A) value.getAttr().getOwner(); if (!result.contains(any)) { result.add(any); } } return result; }
@Test(expected = InvalidEntityException.class) public void saveNonValid() { PlainSchema schema = entityFactory.newEntity(PlainSchema.class); schema.setKey("secondaryEmail"); schema.setType(AttrSchemaType.String); schema.setValidatorClass("org.apache.syncope.core.validation.EmailAddressValidator"); schema.setMandatoryCondition("false"); schema.setMultivalue(true); schema.setUniqueConstraint(true); plainSchemaDAO.save(schema); }
@Test public void checkForEnumType() { PlainSchema schema = entityFactory.newEntity(PlainSchema.class); schema.setType(AttrSchemaType.Enum); schema.setKey("color"); try { plainSchemaDAO.save(schema); fail(); } catch (Exception e) { assertNotNull(e); } schema.setEnumerationValues("red" + SyncopeConstants.ENUM_VALUES_SEPARATOR + "yellow"); schema.setEnumerationKeys("1" + SyncopeConstants.ENUM_VALUES_SEPARATOR + "2"); plainSchemaDAO.save(schema); PlainSchema actual = plainSchemaDAO.find(schema.getKey()); assertNotNull(actual); assertNotNull(actual.getEnumerationKeys()); assertFalse(actual.getEnumerationKeys().isEmpty()); }
@Test public void save() { PlainSchema schema = entityFactory.newEntity(PlainSchema.class); schema.setKey("secondaryEmail"); schema.setType(AttrSchemaType.String); schema.setValidatorClass("org.apache.syncope.core.validation.EmailAddressValidator"); schema.setMandatoryCondition("false"); schema.setMultivalue(true); plainSchemaDAO.save(schema); PlainSchema actual = plainSchemaDAO.find("secondaryEmail"); assertNotNull("expected save to work", actual); assertEquals(schema, actual); }
@Test public void issue42() { PlainSchema userId = plainSchemaDAO.find("userId"); Set<MappingItem> beforeUserIdMappings = new HashSet<>(); for (ExternalResource res : resourceDAO.findAll()) { if (res.getProvision(anyTypeDAO.findUser()) != null && res.getProvision(anyTypeDAO.findUser()).getMapping() != null) { for (MappingItem mapItem : res.getProvision(anyTypeDAO.findUser()).getMapping().getItems()) { if (userId.getKey().equals(mapItem.getIntAttrName())) { beforeUserIdMappings.add(mapItem); } } } } ResourceTO resourceTO = new ResourceTO(); resourceTO.setKey("resource-issue42"); resourceTO.setConnector(100L); resourceTO.setEnforceMandatoryCondition(true); ProvisionTO provisionTO = new ProvisionTO(); provisionTO.setAnyType(AnyTypeKind.USER.name()); provisionTO.setObjectClass(ObjectClass.ACCOUNT_NAME); resourceTO.getProvisions().add(provisionTO); MappingTO mapping = new MappingTO(); provisionTO.setMapping(mapping); MappingItemTO item = new MappingItemTO(); item.setIntAttrName("userId"); item.setIntMappingType(IntMappingType.UserPlainSchema); item.setExtAttrName("campo1"); item.setConnObjectKey(true); item.setMandatoryCondition("false"); item.setPurpose(MappingPurpose.BOTH); mapping.setConnObjectKeyItem(item); ExternalResource resource = resourceDataBinder.create(resourceTO); resource = resourceDAO.save(resource); assertNotNull(resource); assertNotNull(resource.getProvision(anyTypeDAO.findUser()).getMapping()); assertEquals(1, resource.getProvision(anyTypeDAO.findUser()).getMapping().getItems().size()); resourceDAO.flush(); ExternalResource actual = resourceDAO.find("resource-issue42"); assertNotNull(actual); assertEquals(resource, actual); userId = plainSchemaDAO.find("userId"); Set<MappingItem> afterUserIdMappings = new HashSet<>(); for (ExternalResource res : resourceDAO.findAll()) { if (res.getProvision(anyTypeDAO.findUser()) != null && res.getProvision(anyTypeDAO.findUser()).getMapping() != null) { for (MappingItem mapItem : res.getProvision(anyTypeDAO.findUser()).getMapping().getItems()) { if (userId.getKey().equals(mapItem.getIntAttrName())) { afterUserIdMappings.add(mapItem); } } } } assertEquals(beforeUserIdMappings.size(), afterUserIdMappings.size() - 1); }
/** * Generate one where clause for each different attribute schema into the derived schema * expression provided. * * @param expression derived schema expression * @param value derived attribute value * @param attrUtils USER / GROUP * @return where clauses to use to build the query */ private Set<String> getWhereClause(final String expression, final String value) { Parser parser = new Parser(new StringReader(expression)); // Schema names List<String> identifiers = new ArrayList<>(); // Literals List<String> literals = new ArrayList<>(); // Get schema names and literals for (Token token = parser.getNextToken(); token != null && StringUtils.isNotBlank(token.toString()); token = parser.getNextToken()) { if (token.kind == ParserConstants.STRING_LITERAL) { literals.add(token.toString().substring(1, token.toString().length() - 1)); } if (token.kind == ParserConstants.IDENTIFIER) { identifiers.add(token.toString()); } } // Sort literals in order to process later literals included into others Collections.sort( literals, new Comparator<String>() { @Override public int compare(final String t, final String t1) { if (t == null && t1 == null) { return 0; } else if (t != null && t1 == null) { return -1; } else if (t == null && t1 != null) { return 1; } else if (t.length() == t1.length()) { return 0; } else if (t.length() > t1.length()) { return -1; } else { return 1; } } }); // Split value on provided literals List<String> attrValues = split(value, literals); if (attrValues.size() != identifiers.size()) { LOG.error("Ambiguous JEXL expression resolution."); throw new IllegalArgumentException("literals and values have different size"); } // clauses to be used with INTERSECTed queries Set<String> clauses = new HashSet<>(); // builder to build the clauses StringBuilder bld = new StringBuilder(); // Contains used identifiers in order to avoid replications Set<String> used = new HashSet<>(); // Create several clauses: one for eanch identifiers for (int i = 0; i < identifiers.size(); i++) { if (!used.contains(identifiers.get(i))) { // verify schema existence and get schema type PlainSchema schema = plainSchemaDAO.find(identifiers.get(i)); if (schema == null) { LOG.error("Invalid schema id '{}'", identifiers.get(i)); throw new IllegalArgumentException("Invalid schema id " + identifiers.get(i)); } // clear builder bld.delete(0, bld.length()); bld.append("("); // set schema name bld.append("s.id = '").append(identifiers.get(i)).append("'"); bld.append(" AND "); bld.append("s.id = a.schema_id").append(" AND "); bld.append("a.id = v.attribute_id"); bld.append(" AND "); // use a value clause different for eanch different schema type switch (schema.getType()) { case Boolean: bld.append("v.booleanValue = '").append(attrValues.get(i)).append("'"); break; case Long: bld.append("v.longValue = ").append(attrValues.get(i)); break; case Double: bld.append("v.doubleValue = ").append(attrValues.get(i)); break; case Date: bld.append("v.dateValue = '").append(attrValues.get(i)).append("'"); break; default: bld.append("v.stringValue = '").append(attrValues.get(i)).append("'"); } bld.append(")"); used.add(identifiers.get(i)); clauses.add(bld.toString()); } } LOG.debug("Generated where clauses {}", clauses); return clauses; }
@Test(expected = InvalidEntityException.class) public void saveInvalidSchema() { PlainSchema schema = entityFactory.newEntity(PlainSchema.class); schema.setKey("username"); plainSchemaDAO.save(schema); }