/** * Returns the property values (including information about their descriptors and overwritten values) for * the given resource in the specified context. * * @param resourceId * @param contextId * @return */ public List<ResourceEditProperty> getPropertiesForResource(Integer resourceId, Integer contextId) { ResourceEntity resource = entityManager.find(ResourceEntity.class, resourceId); ContextEntity context = entityManager.find(ContextEntity.class, contextId); List<ResourceEditProperty> properties = propertyEditingService.loadPropertiesForEditResource( resource.getId(), resource.getResourceType(), context); if (permissionBoundary.hasPermission(Permission.RESOURCE_PROPERTY_DECRYPT, context, Action.ALL, resource, null)) { return propertyValueService.decryptProperties(properties); } return properties; }
/** * * @param relation * @param property * @param relevantContexts * @return a List containing all properties which would be overridden by setting the property on the relation. */ private List<DifferingProperty> getPropertyOverviewForProvidedRelation(ResourceEditRelation relation, ResourceEditProperty property, List<ContextEntity> relevantContexts) { if (relevantContexts.isEmpty()) { return Collections.EMPTY_LIST; } List<Integer> relevantContextIds = buildRelevantContextIdsList(relevantContexts); Query query = queries.getPropertyOverviewForProvidedRelatedResourceQuery(property.getTechnicalKey(), relation.getResRelId(), relevantContextIds); List<DifferingProperty> differingProperties = getDifferingProperties(property, query, Origin.RELATION); // obtain property values defined on the (slave) resource, which would be overwritten by defining one on the relation - global context is relevant here relevantContextIds.add(contextHierarchy.getContextWithParentIds(relevantContexts.get(0)).get(0)); differingProperties.addAll(getPropertyDefinedOnResource(relation, property, relevantContextIds, Origin.INSTANCE)); return differingProperties; }
private List<DifferingProperty> getDifferingProperties(ResourceEditProperty property, Query query, Origin origin) { List<DifferingProperty> differingProps = new ArrayList<>(); List resultList = query.getResultList(); for (Object o : resultList) { Map.Entry<String, String> entry = createEntryForOverridenProperty(o, property.getPropertyId()); differingProps.add(new DifferingProperty(origin, entry.getKey(), entry.getValue())); } return differingProps; }
/** * * @param resourceType * @param property * @param relevantContexts * @return a List containing all properties which override the value of its parent context. */ public List<DifferingProperty> getPropertyOverviewForResourceType(ResourceTypeEntity resourceType, ResourceEditProperty property, List<ContextEntity> relevantContexts) { if (relevantContexts.isEmpty()) { return Collections.EMPTY_LIST; } List<Integer> contextIds = buildRelevantContextIdsList(relevantContexts); Query query = queries.getPropertyOverviewForResourceTypeQuery(property.getTechnicalKey(), resourceType.getId(), contextIds); return getDifferingProperties(property, query, Origin.INSTANCE); }
public List<DifferingProperty> getPropertyOverviewForRelation(ResourceEditRelation relation, ResourceEditProperty property, List<ContextEntity> relevantContexts) { switch (relation.getMode()) { case CONSUMED: return getPropertyOverviewForConsumedRelation(relation, property, relevantContexts); case PROVIDED: return getPropertyOverviewForProvidedRelation(relation, property, relevantContexts); default: String msg = String.format("Relation mode '%s' is not supported for property overview (property id: %d)", relation.getMode().name(), property.getPropertyId()); log.warning(msg); return Collections.EMPTY_LIST; } } /**
/** * Returns the property values (including information about their descriptors and overwritten values) * Encrypted property values are returned decrypted if the caller has the permission to do so */ public List<ResourceEditProperty> getPropertiesForRelatedResource( ConsumedResourceRelationEntity relationEntity, Integer contextId) { ResourceEntity resource = entityManager.find(ResourceEntity.class, relationEntity.getMasterResourceId()); ContextEntity context = entityManager.find(ContextEntity.class, contextId); ResourceEntity slaveResource = entityManager.find(ResourceEntity.class, relationEntity .getSlaveResource().getId()); // a decrypt permission on the master resource allows to decrypt on the related slave properties if (permissionBoundary.hasPermission(Permission.RESOURCE_PROPERTY_DECRYPT, context, Action.ALL, resource, null)) { return propertyValueService.decryptProperties(propertyEditingService.loadPropertiesForEditRelation( ResourceEditRelation.Mode.CONSUMED, relationEntity.getId(), slaveResource.getId(), resource.getResourceType(), slaveResource.getResourceType(), context)); } return propertyEditingService.loadPropertiesForEditRelation(ResourceEditRelation.Mode.CONSUMED, relationEntity.getId(), slaveResource.getId(), resource.getResourceType(), slaveResource.getResourceType(), context); }
/** * loads the properties from the database in a single query and returns them as a transfer object * * @param resourceId * - the resource id of the resource for which the properties should be loaded * @param type * - the resource type of the resource for which the properties should be loaded * @param currentContext * - the context for which the properties should be loaded * @return a list of containers which contain the required property information */ public List<ResourceEditProperty> loadPropertiesForEditResource(Integer resourceId, ResourceTypeEntity type, ContextEntity currentContext) { Map<Integer, ResourceEditProperty> propMap = new HashMap<>(); List<Integer> contextList = contextHierarchy.getContextWithParentIds(currentContext); List<Integer> typeList = getTypeWithParentIds(null, type); Query query = queries.getPropertyValueForResource(resourceId, typeList, contextList); List<ResourceEditProperty> result = JpaSqlResultMapper.list(query, ResourceEditProperty.class); for (ResourceEditProperty prop : result) { // Since we have to load also the properties of the parent contexts (to display the replaced // values) we must find the property of the current context and set parents hierarchical propMap.put(prop.getDescriptorId(), findChildPropAndSetParent(prop, propMap.get(prop.getDescriptorId()), contextList)); } return new ArrayList<>(new TreeSet<>(propMap.values())); }
public List<PropertyEditingService.DifferingProperty> getPropertyOverviewForRelation(ResourceEditRelation relation, ResourceEditProperty property, List<ContextEntity> relevantContexts) { return propertyEditingService.getPropertyOverviewForRelation(relation, property, relevantContexts); } }
private List<DifferingProperty> getPropertyDefinedOnResource(ResourceEditRelation relation, ResourceEditProperty property, List<Integer> relevantContextIds, Origin origin) { Query query = queries.getPropertyOverviewForResourceQuery(property.getTechnicalKey(), relation.getSlaveId(), relevantContextIds); return getDifferingProperties(property, query, origin); }
ResourceEditProperty bestParentMatch = findChildPropAndSetParent(candidate, existing.getParent(), contextList); existing.setParent(bestParentMatch);
/** * Returns the property values (including information about their descriptors and overwritten values) * * @param masterResourceId * @param contextId * @return */ public List<ResourceEditProperty> getPropertiesForRelatedResource(Integer masterResourceId, ResourceEditRelation resourceRelation, Integer contextId) { if (masterResourceId != null && resourceRelation != null && contextId != null) { ResourceEntity resource = entityManager.find(ResourceEntity.class, masterResourceId); ContextEntity context = entityManager.find(ContextEntity.class, contextId); ResourceEntity slaveResource = entityManager.find(ResourceEntity.class, resourceRelation.getSlaveId()); // a decrypt permission on the master resource allows to decrypt on the related slave properties if (permissionBoundary.hasPermission(Permission.RESOURCE_PROPERTY_DECRYPT, context, Action.ALL, resource, null)) { return propertyValueService.decryptProperties(propertyEditingService.loadPropertiesForEditRelation( resourceRelation.getMode(), resourceRelation.getResRelId(), slaveResource.getId(), resource.getResourceType(), slaveResource.getResourceType(), context)); } return propertyEditingService.loadPropertiesForEditRelation(resourceRelation.getMode(), resourceRelation.getResRelId(), slaveResource.getId(), resource.getResourceType(), slaveResource.getResourceType(), context); } return new ArrayList<>(); }
public List<ResourceEditProperty> loadPropertiesForEditResourceTypeRelation(ResourceTypeEntity masterResourceType, ResourceTypeEntity slaveResourceType, ContextEntity currentContext) { Map<Integer, ResourceEditProperty> propMap = new HashMap<>(); List<Integer> contextList = contextHierarchy.getContextWithParentIds(currentContext); List<Integer> masterResourceTypeList = getTypeWithParentIds(null, masterResourceType); List<Integer> slaveResourceTypeList = getTypeWithParentIds(null, slaveResourceType); Query query = queries.getPropertyValueForResourceTypeRelationQuery(masterResourceTypeList, slaveResourceTypeList, contextList); List<ResourceEditProperty> result = JpaSqlResultMapper.list(query, ResourceEditProperty.class); for (ResourceEditProperty prop : result) { if(!slaveResourceType.getId().equals(prop.getPropertyValueTypeId()) || !masterResourceType.getId().equals(prop.getMasterTypeId())){ prop.setDefinedOnSuperResourceType(true); } // Since we have to load also the properties of the parent contexts (to display the replaced // values) we must find the property of the current context and set parents hierarchical propMap.put(prop.getDescriptorId(), findChildPropAndSetParent(prop, propMap.get(prop.getDescriptorId()), contextList)); } return new ArrayList<>(new TreeSet<>(propMap.values())); }
/** * * @param resource * @param property * @param relevantContexts * @return a List containing all properties which override the value of its parent context. */ public List<DifferingProperty> getPropertyOverviewForResource(ResourceEntity resource, ResourceEditProperty property, List<ContextEntity> relevantContexts) { if (relevantContexts.isEmpty()) { return Collections.EMPTY_LIST; } List<Integer> contextIds = buildRelevantContextIdsList(relevantContexts); Query query = queries.getPropertyOverviewForResourceQuery(property.getTechnicalKey(), resource.getId(), contextIds); return getDifferingProperties(property, query, Origin.INSTANCE); }
@Test(expected = NotAuthorizedException.class) public void setPropertyValueOnResourceRelationForContextShouldThrowExceptionWhenWhenResourceUpdatePermissionIsMissing() throws Exception { // given String resourceGroupName = "resourceGroupName"; String releaseName = "releaseName"; String relatedResourceGroupName = "relatedResourceGroupName"; String relatedResourceReleaseName ="relatedResourceReleaseName"; String contextName = "contextName"; String propertyName = "propertyName"; String propertyValue = "propertyValue"; List<ResourceEditProperty> properties = new ArrayList<>(); properties.add(new ResourceEditPropertyBuilder().withDisplayAndTechKeyName(propertyName).withValue(propertyValue).build()); ContextEntity contextMock = mock(ContextEntity.class); ConsumedResourceRelationEntity relation = createWithMasterAndSlave(resourceGroupName, relatedResourceGroupName); when(resourceRelationLocatorMock.getResourceRelation(resourceGroupName, releaseName, relatedResourceGroupName, relatedResourceReleaseName)).thenReturn(relation); when(contextLocatorMock.getContextByName(contextName)).thenReturn(contextMock); when(entityManagerMock.find(ResourceEntity.class, 1)).thenReturn(mock(ResourceEntity.class)); when(entityManagerMock.find(ResourceEntity.class, 2)).thenReturn(mock(ResourceEntity.class)); when(propertyEditingServiceMock.loadPropertiesForEditRelation(any(ResourceEditRelation.Mode.class), anyInt(), anyInt(), any(ResourceTypeEntity.class), any(ResourceTypeEntity.class), any(ContextEntity.class))).thenReturn(properties); when(permissionBoundaryMock.hasPermission(Permission.RESOURCE, contextMock, Action.UPDATE, null, null)).thenReturn(false); when(permissionBoundaryMock.hasPermission(Permission.RESOURCE_PROPERTY_DECRYPT, contextMock, Action.ALL, relation.getMasterResource(), null)).thenReturn(true); // when Assert.assertFalse(properties.isEmpty()); verify(propertyValueServiceMock, never()).decryptProperties(anyList()); editor.setPropertyValueOnResourceRelationForContext(resourceGroupName, releaseName, relatedResourceGroupName, relatedResourceReleaseName, contextName, propertyName, propertyValue); }
@Test public void resetPropertyValueOnResourceForContextShouldBeSuccessfullWhenResourceUpdatePermissionIsPresentButResourcePropertyDecryptIsMissing() throws Exception { // given String resourceGroupName = "resourceGroupName"; String releaseName = "releaseName"; String contextName = "contextName"; String propertyName = "propertyName"; String propertyValue = "propertyValue"; String typeName = "resourceGroupTypeName"; List<ResourceEditProperty> properties = new ArrayList<>(); properties.add(new ResourceEditPropertyBuilder().withDisplayAndTechKeyName(propertyName).withValue(propertyValue).withDescriptorId(12).build()); ContextEntity contextMock = mock(ContextEntity.class); ResourceEntity resource = createWithIdNameAndTypeName(1, resourceGroupName, typeName); when(resourceLocatorMock.getResourceByGroupNameAndRelease(resourceGroupName, releaseName)).thenReturn(resource); when(contextLocatorMock.getContextByName(contextName)).thenReturn(contextMock); when(propertyEditingServiceMock.loadPropertiesForEditResource(anyInt(), any(ResourceTypeEntity.class), any(ContextEntity.class))).thenReturn(properties); when(permissionBoundaryMock.hasPermission(Permission.RESOURCE, contextMock, Action.UPDATE, resource, null)).thenReturn(true); when(permissionBoundaryMock.hasPermission(Permission.RESOURCE_PROPERTY_DECRYPT, contextMock, Action.ALL, resource, null)).thenReturn(false); when(entityManagerMock.merge(resource)).thenReturn(resource); when(entityManagerMock.merge(contextMock)).thenReturn(contextMock); // when Assert.assertFalse(properties.isEmpty()); editor.resetPropertyValueOnResourceForContext(resourceGroupName, releaseName, contextName, propertyName); }
/** * * @param relation * @param property * @param relevantContexts * @return a List containing all properties which would be overridden by setting the property on the relation. */ private List<DifferingProperty> getPropertyOverviewForConsumedRelation(ResourceEditRelation relation, ResourceEditProperty property, List<ContextEntity> relevantContexts) { if (relevantContexts.isEmpty()) { return Collections.EMPTY_LIST; } List<Integer> relevantContextIds = buildRelevantContextIdsList(relevantContexts); Query query = queries.getPropertyOverviewForConsumedRelatedResourceQuery(property.getTechnicalKey(), relation.getResRelId(), relevantContextIds); List<DifferingProperty> differingProperties = getDifferingProperties(property, query, Origin.RELATION); // obtain property values defined on the (slave) resource, which would be overwritten by defining one on the relation - global context is relevant here relevantContextIds.add(contextHierarchy.getContextWithParentIds(relevantContexts.get(0)).get(0)); differingProperties.addAll(getPropertyDefinedOnResource(relation, property, relevantContextIds, Origin.INSTANCE)); return differingProperties; }
public List<ResourceEditProperty> loadPropertiesForEditRelation(Mode relationTyp, Integer resourceRelationId, Integer relatedResourceId, ResourceTypeEntity masterResourceType, ResourceTypeEntity slaveResourceType, ContextEntity currentContext) { Map<Integer, ResourceEditProperty> propMap = new HashMap<>(); List<Integer> contextList = contextHierarchy.getContextWithParentIds(currentContext); List<Integer> masterResourceTypeList = getTypeWithParentIds(null, masterResourceType); List<Integer> slaveResourceTypeList = getTypeWithParentIds(null, slaveResourceType); Query query; switch (relationTyp) { case CONSUMED: query = queries.getPropertyValueForConsumedRelationQuery(resourceRelationId, relatedResourceId, masterResourceTypeList, slaveResourceTypeList, contextList); break; case PROVIDED: query = queries.getPropertyValueForProvidedRelationQuery(resourceRelationId, relatedResourceId, masterResourceTypeList, slaveResourceTypeList, contextList); break; default: query = null; } List<ResourceEditProperty> result = JpaSqlResultMapper.list(query, ResourceEditProperty.class); for (ResourceEditProperty prop : result) { // Since we have to load also the properties of the parent contexts (to display the replaced // values) we must find the property of the current context and set parents hierarchical propMap.put(prop.getDescriptorId(), findChildPropAndSetParent(prop, propMap.get(prop.getDescriptorId()), contextList)); } return new ArrayList<>(new TreeSet<>(propMap.values())); }
@Test public void setPropertyValueOnResourceRelationForContextShouldSetPropertyValue() throws Exception { // given String resourceGroupName = "resourceGroupName"; String releaseName = "releaseName"; String relatedResourceGroupName = "relatedResourceGroupName"; String relatedResourceReleaseName ="relatedResourceReleaseName"; String contextName = "contextName"; String propertyName = "propertyName"; String propertyValue = "propertyValue"; List<ResourceEditProperty> properties = new ArrayList<>(); properties.add(new ResourceEditPropertyBuilder().withDisplayAndTechKeyName(propertyName).withValue(propertyValue).build()); ContextEntity contextMock = mock(ContextEntity.class); ConsumedResourceRelationEntity relation = createWithMasterAndSlave(resourceGroupName, relatedResourceGroupName); when(resourceRelationLocatorMock.getResourceRelation(resourceGroupName, releaseName, relatedResourceGroupName, relatedResourceReleaseName)).thenReturn(relation); when(contextLocatorMock.getContextByName(contextName)).thenReturn(contextMock); when(entityManagerMock.find(ResourceEntity.class, 1)).thenReturn(mock(ResourceEntity.class)); when(entityManagerMock.find(ResourceEntity.class, 2)).thenReturn(mock(ResourceEntity.class)); when(propertyEditingServiceMock.loadPropertiesForEditRelation(any(ResourceEditRelation.Mode.class), anyInt(), anyInt(), any(ResourceTypeEntity.class), any(ResourceTypeEntity.class), any(ContextEntity.class))).thenReturn(properties); when(permissionBoundaryMock.hasPermission(Permission.RESOURCE, contextMock, Action.UPDATE, relation.getMasterResource(), null)).thenReturn(true); when(permissionBoundaryMock.hasPermission(Permission.RESOURCE_PROPERTY_DECRYPT, contextMock, Action.ALL, relation.getMasterResource(), null)).thenReturn(true); setupMocksForSinglePropertiesModificationsFor(relation); // when Assert.assertFalse(properties.isEmpty()); editor.setPropertyValueOnResourceRelationForContext(resourceGroupName, releaseName, relatedResourceGroupName, relatedResourceReleaseName, contextName, propertyName, propertyValue); // then verify(propertyValueServiceMock).setPropertyValue(Matchers.any(ContextDependency.class), Matchers.anyInt(), Matchers.eq(propertyValue)); }
@Test public void setPropertyValueOnResourceForContextShouldSetPropertyValue() throws Exception { // given String resourceGroupName = "resourceGroupName"; String releaseName = "releaseName"; String contextName = "contextName"; String propertyName = "propertyName"; String propertyValue = "propertyValue"; String typeName = "resourceGroupTypeName"; List<ResourceEditProperty> properties = new ArrayList<>(); properties.add(new ResourceEditPropertyBuilder().withDisplayAndTechKeyName(propertyName).withValue(propertyValue).build()); ContextEntity contextMock = mock(ContextEntity.class); ResourceEntity resource = createWithIdNameAndTypeName(1, resourceGroupName, typeName); when(resourceLocatorMock.getResourceByGroupNameAndRelease(resourceGroupName, releaseName)).thenReturn(resource); when(contextLocatorMock.getContextByName(contextName)).thenReturn(contextMock); when(propertyEditingServiceMock.loadPropertiesForEditResource(any(Integer.class), any(ResourceTypeEntity.class), any(ContextEntity.class))).thenReturn(properties); when(permissionBoundaryMock.hasPermission(Permission.RESOURCE, contextMock, Action.UPDATE, resource, null)).thenReturn(true); when(permissionBoundaryMock.hasPermission(Permission.RESOURCE_PROPERTY_DECRYPT, contextMock, Action.ALL, resource, null)).thenReturn(true); setupMocksForSinglePropertiesModificationsFor(resource); // when Assert.assertFalse(properties.isEmpty()); editor.setPropertyValueOnResourceForContext(resourceGroupName, releaseName, contextName, propertyName, propertyValue); //then verify(propertyValueServiceMock).setPropertyValue(Matchers.any(ContextDependency.class), Matchers.anyInt(), Matchers.eq(propertyValue)); }
/** * loads the properties from the database in a single query and returns them as a transfer object * * @param resourceType * - the resourcetype for which the properties should be loaded * @param currentContext * - the context for which the properties should be loaded * @return a list of containers which contain the required property information */ public List<ResourceEditProperty> loadPropertiesForEditResourceType(ResourceTypeEntity resourceType, ContextEntity currentContext) { Map<Integer, ResourceEditProperty> propMap = new HashMap<>(); List<Integer> contextList = contextHierarchy.getContextWithParentIds(currentContext); List<Integer> typeList = getTypeWithParentIds(null, resourceType); Query query = queries.getPropertyValueForResourceType(typeList, contextList); List<ResourceEditProperty> result = JpaSqlResultMapper.list(query, ResourceEditProperty.class); for (ResourceEditProperty prop : result) { // Since we have to load also the properties of the parent contexts (to display the replaced // values) we must find the property of the current context and set parents hierarchical propMap.put(prop.getDescriptorId(), findChildPropAndSetParent(prop, propMap.get(prop.getDescriptorId()), contextList)); if(!resourceType.getId().equals(prop.getTypeId())){ prop.setDescriptorDefinedOnSuperResourceType(true); } if(!resourceType.getId().equals(prop.getPropertyValueTypeId())){ prop.setDefinedOnSuperResourceType(true); } } return new ArrayList<>(new TreeSet<>(propMap.values())); }