public <T> T getAttributeValue(String attrName, Class<T> type) { Set<T> values = getAttributeValues(attrName, type); if (values == null || values.isEmpty()) { return null; } if (values.size()>1) { throw new IllegalArgumentException("Attempt to fetch single value from a multi-valued attribute "+attrName); } return values.iterator().next(); }
private <T extends DummyObject> Object getField(T dummyObject, String fieldName) { if (fieldName.equals(Uid.NAME)) { return dummyObject.getId(); } if (fieldName.equals(Name.NAME)) { return dummyObject.getName(); } return dummyObject.getAttributeValue(fieldName); }
public void addAttributeValue(String name, Object value) throws SchemaViolationException, ConnectException, FileNotFoundException, SchemaViolationException, ConflictException, InterruptedException { Collection<Object> values = new ArrayList<>(1); values.add(value); addAttributeValues(name, values); }
private void applyAuxiliaryObjectClassDelta(DummyObject dummyObject, AttributeDelta delta) { List<String> replaceValues = getReplaceValues(delta, String.class); if (replaceValues != null) { dummyObject.replaceAuxiliaryObjectClassNames(replaceValues); } List<String> addValues = getAddValues(delta, String.class); if (addValues != null) { dummyObject.addAuxiliaryObjectClassNames(addValues); } List<String> deleteValues = getRemoveValues(delta, String.class); if (deleteValues != null) { dummyObject.deleteAuxiliaryObjectClassNames(deleteValues); } }
builder.setUid(dummyObject.getName()); } else if (configuration.getUidMode().equals(DummyConfiguration.UID_MODE_UUID)) { builder.setUid(dummyObject.getId()); } else { throw new IllegalStateException("Unknown UID mode "+configuration.getUidMode()); builder.addAttribute(Name.NAME, dummyObject.getName()); for (String name : dummyObject.getAttributeNames()) { DummyAttributeDefinition attrDef = dummyObject.getAttributeDefinition(name); if (attrDef == null) { throw new InvalidAttributeValueException("Unknown account attribute '"+name+"'"); Set<Object> values = dummyObject.getAttributeValues(name, Object.class); if (configuration.isVaryLetterCase()) { name = varyLetterCase(name); builder.addAttribute(OperationalAttributes.ENABLE_NAME, dummyObject.isEnabled()); if (dummyObject.getValidFrom() != null && (attributesToGet == null || attributesToGet.contains(OperationalAttributes.ENABLE_DATE_NAME))) { builder.addAttribute(OperationalAttributes.ENABLE_DATE_NAME, convertToLong(dummyObject.getValidFrom())); if (dummyObject.getValidTo() != null && (attributesToGet == null || attributesToGet.contains(OperationalAttributes.DISABLE_DATE_NAME))) { builder.addAttribute(OperationalAttributes.DISABLE_DATE_NAME, convertToLong(dummyObject.getValidTo())); if (!dummyObject.getAuxiliaryObjectClassNames().isEmpty()) { builder.addAttribute(PredefinedAttributes.AUXILIARY_OBJECT_CLASS_NAME, dummyObject.getAuxiliaryObjectClassNames());
protected void assertNoDummyAttribute(DummyObject object, String attributeName) { Set<Object> attributeValues = object.getAttributeValues(attributeName, Object.class); assertNotNull("Unexpected attribute "+attributeName+" in "+object.getShortTypeName()+" "+object+": "+attributeValues, attributeValues); }
String normalName = normalize(newObject.getName()); if (normalName != null && forbiddenNames != null && forbiddenNames.contains(normalName)) { throw new ObjectAlreadyExistsException(normalName + " is forbidden to use as an object name"); newObject.setId(newId); if (allObjects.containsKey(newId)) { throw new IllegalStateException("The hell is frozen over. The impossible has happened. ID "+newId+" already exists ("+ type.getSimpleName()+" with identifier "+normalName+")"); newObject.addAttributeValue(DummyAccount.ATTR_INTERNAL_ID, new Random().nextInt()); newObject.setResource(this); map.put(mapKey, newObject); allObjects.put(newId, newObject); DummyDelta delta = new DummyDelta(syncToken, type, newId, newObject.getName(), DummyDeltaType.ADD); deltas.add(delta); return newObject.getName();
replaceValues = valuesTransformer.apply(replaceValues); dummyObject.replaceAttributeValues(attributeName, (Collection)replaceValues); addValues = valuesTransformer.apply(addValues); dummyObject.addAttributeValues(attributeName, (Collection)addValues); deleteValues = valuesTransformer.apply(deleteValues); dummyObject.removeAttributeValues(attributeName, (Collection)deleteValues);
public DummyObjectAsserter<D,R> assertName(String expected) { assertEquals("Wrong name in "+desc(), expected, getDummyObjectAssertExists().getName()); return this; }
private synchronized <T extends DummyObject> void deleteObjectByName(Class<T> type, Map<String,T> map, String name) throws ObjectDoesNotExistException, ConnectException, FileNotFoundException, SchemaViolationException, ConflictException, InterruptedException { checkBlockOperations(); recordWriteOperation("delete"); breakIt(deleteBreakMode, "delete"); delayOperation(); String normalName = normalize(name); T existingObject; if (!enforceUniqueName) { throw new IllegalStateException("Whoops! got into deleteObjectByName without enforceUniqueName"); } if (map.containsKey(normalName)) { existingObject = map.get(normalName); map.remove(normalName); allObjects.remove(existingObject.getId()); } else { throw new ObjectDoesNotExistException(type.getSimpleName()+" with name '"+normalName+"' does not exist"); } if (syncStyle != DummySyncStyle.NONE) { int syncToken = nextSyncToken(); DummyDelta delta = new DummyDelta(syncToken, type, existingObject.getId(), name, DummyDeltaType.DELETE); deltas.add(delta); } }
public DummyAttributeDefinition getAttributeDefinition(String attrName) { DummyAttributeDefinition def = getObjectClassNoExceptions().getAttributeDefinition(attrName); if (def != null) { return def; } for (String auxClassName : getAuxiliaryObjectClassNames()) { DummyObjectClass auxObjectClass = resource.getAuxiliaryObjectClassMap().get(auxClassName); if (auxObjectClass == null) { throw new IllegalStateException("Auxiliary object class " + auxClassName + " couldn't be found"); } def = auxObjectClass.getAttributeDefinition(attrName); if (def != null) { return def; } } return null; }
protected void checkSchema(String attrName, Collection<Object> values, String operationName) throws SchemaViolationException { if (resource == null || !resource.isEnforceSchema()) { return; } DummyObjectClass accountObjectClass; try { accountObjectClass = getObjectClass(); } catch (Exception e) { // No not enforce schema if the schema is broken (simulated) return; } if (accountObjectClass == null) { // Nothing to check return; } DummyAttributeDefinition attributeDefinition = getAttributeDefinition(attrName); if (attributeDefinition == null) { throw new SchemaViolationException("Attribute "+attrName+" is not defined in resource schema"); } if (attributeDefinition.isRequired() && (values == null || values.isEmpty())) { throw new SchemaViolationException(operationName + " of required attribute "+attrName+" results in no values"); } if (!attributeDefinition.isMulti() && values != null && values.size() > 1) { throw new SchemaViolationException(operationName + " of single-valued attribute "+attrName+" results in "+values.size()+" values"); } }
public String getAttributeValue(String attrName) { return getAttributeValue(attrName,String.class); }
public void replaceAuxiliaryObjectClassNames(List<?> values) { auxiliaryObjectClassNames.clear(); addAuxiliaryObjectClassNames(values); }
public void removeAttributeValue(String name, Object value) throws SchemaViolationException, ConnectException, FileNotFoundException, SchemaViolationException, ConflictException, InterruptedException { Collection<Object> values = new ArrayList<>(); values.add(value); removeAttributeValues(name, values); }
public boolean isReturnedByDefault(String attrName) { final DummyAttributeDefinition attributeDefinition = getAttributeDefinition(attrName); if (attributeDefinition != null) { return attributeDefinition.isReturnedByDefault(); } else { System.out.println("Warning: attribute " + attrName + " is not defined in " + this); return false; } } }
public DummyObjectAsserter<D,R> assertEnabled() { assertTrue(desc() + " is disabled", getDummyObjectAssertExists().isEnabled()); return this; }
protected <T> void assertDummyAttributeValues(DummyObject object, String attributeName, T... expectedValues) { Set<T> attributeValues = (Set<T>) object.getAttributeValues(attributeName, expectedValues[0].getClass()); assertNotNull("No attribute "+attributeName+" in "+object.getShortTypeName()+" "+object, attributeValues); TestUtil.assertSetEquals("Wrong values of attribute "+attributeName+" in "+object.getShortTypeName()+" "+object, attributeValues, expectedValues); }