/** * Checks Permissions and deletes the given property descriptor from database for resource type */ public void deletePropertyDescriptorForResourceType(ForeignableOwner deletingOwner, Integer resourceTypeId, PropertyDescriptorEntity descriptor, boolean forceDelete) throws AMWException, ForeignableOwnerViolationException { if (descriptor != null && descriptor.getId() != null) { ResourceTypeEntity attachedResourceType = entityManager.find(ResourceTypeEntity.class, resourceTypeId); ResourceTypeContextEntity resourceTypeContextEntity = attachedResourceType.getOrCreateContext(contextService.getGlobalResourceContextEntity()); permissionBoundary.checkPermissionAndFireException(Permission.RESOURCETYPE, null, Action.UPDATE, null, attachedResourceType, null); foreignableService.verifyDeletableByOwner(deletingOwner, descriptor); if (forceDelete) { propertyDescriptorService.deletePropertyDescriptorByOwnerIncludingPropertyValues(descriptor, resourceTypeContextEntity, attachedResourceType); } else { propertyDescriptorService.deletePropertyDescriptorByOwnerInResourceTypeContext(descriptor, resourceTypeContextEntity, resourceTypeId); } } }
/** * Checks Permissions and deletes the given property descriptor from database for resource */ public void deletePropertyDescriptorForResource(ForeignableOwner deletingOwner, Integer resourceId, PropertyDescriptorEntity descriptor, boolean forceDelete) throws AMWException, ForeignableOwnerViolationException { if (descriptor != null && descriptor.getId() != null) { ResourceEntity attachedResource = entityManager.find(ResourceEntity.class, resourceId); ResourceContextEntity resourceContext = attachedResource.getOrCreateContext(contextService.getGlobalResourceContextEntity()); permissionBoundary.checkPermissionAndFireException(Permission.RESOURCE, null, Action.UPDATE, attachedResource.getResourceGroup(), null, null); foreignableService.verifyDeletableByOwner(deletingOwner, descriptor); if (forceDelete) { propertyDescriptorService.deletePropertyDescriptorByOwnerIncludingPropertyValues(descriptor, resourceContext, attachedResource); } else { propertyDescriptorService.deletePropertyDescriptorByOwnerInResourceContext(descriptor, resourceContext, resourceId); } } }
@Test public void deletePropertyDescriptorByOwnerIncludingPropertyValuesWhenDeletingOwnerIsOwnerOfDescriptorDefinedOnResourceTypeWithPropertiesOnResourceShouldSucceed() throws AMWException { // given ForeignableOwner deletingOwner = ForeignableOwner.AMW; AbstractContext abstractContextMock = mock(AbstractContext.class); ResourceTypeEntity resourceTypeEntityMock = mock(ResourceTypeEntity.class); ResourceEntity resourceEntityMock = mock(ResourceEntity.class); ResourceContextEntity resourceContextEntityMock = mock(ResourceContextEntity.class); PropertyDescriptorEntity descriptor = new PropertyDescriptorEntityBuilder().withOwner(deletingOwner).withId(1).build(); PropertyEntity property = new PropertyEntityBuilder().buildPropertyEntity("propVal", descriptor); descriptor.addProperty(property); Set<PropertyEntity> properties = new HashSet<>(); properties.add(property); TypedQuery<PropertyDescriptorEntity> queryMock = mock(TypedQuery.class); when(entityManagerMock.createQuery("from PropertyDescriptorEntity d left join fetch d.propertyTags where d.id = :propertyDescriptorId ", PropertyDescriptorEntity.class)).thenReturn(queryMock); when(queryMock.getSingleResult()).thenReturn(descriptor); when(entityManagerMock.find(PropertyDescriptorEntity.class, descriptor.getId())).thenReturn(descriptor); when(resourceTypeEntityMock.getResources()).thenReturn(Collections.singleton(resourceEntityMock)); when(resourceEntityMock.getContexts()).thenReturn(Collections.singleton(resourceContextEntityMock)); when(resourceContextEntityMock.getProperties()).thenReturn(properties); // when service.deletePropertyDescriptorByOwnerIncludingPropertyValues(descriptor, abstractContextMock, resourceTypeEntityMock); // then verify(resourceContextEntityMock).removeProperty(property); verify(entityManagerMock).remove(descriptor); }
@Test public void deletePropertyDescriptorByOwnerIncludingPropertyValuesWhenDeletingOwnerIsOwnerOfDescriptorWithPropertiesOnResourceShouldSucceed() throws AMWException { // given ForeignableOwner deletingOwner = ForeignableOwner.AMW; AbstractContext abstractContextMock = mock(AbstractContext.class); ResourceEntity resourceEntityMock = mock(ResourceEntity.class); PropertyEntity property = new PropertyEntity(); Set<PropertyEntity> properties = new HashSet<>(); properties.add(property); PropertyDescriptorEntity descriptor = new PropertyDescriptorEntityBuilder().withOwner(deletingOwner).withId(1).withProperties(properties).build(); Assert.assertEquals(deletingOwner, descriptor.getOwner()); ResourceContextEntity resourceContextEntityMock = mock(ResourceContextEntity.class); TypedQuery<PropertyDescriptorEntity> queryMock = mock(TypedQuery.class); when(entityManagerMock.createQuery("from PropertyDescriptorEntity d left join fetch d.propertyTags where d.id = :propertyDescriptorId ", PropertyDescriptorEntity.class)).thenReturn(queryMock); when(queryMock.getSingleResult()).thenReturn(descriptor); when(resourceEntityMock.getContexts()).thenReturn(Collections.singleton(resourceContextEntityMock)); when(resourceContextEntityMock.getProperties()).thenReturn(properties); // when service.deletePropertyDescriptorByOwnerIncludingPropertyValues(descriptor, abstractContextMock, resourceEntityMock); // then verify(resourceContextEntityMock).removeProperty(property); verify(entityManagerMock).remove(descriptor); }
@Test public void deletePropertyDescriptorByOwnerIncludingPropertyValuesWhenDeletingOwnerIsOwnerOfDescriptorWithPropertiesOnResourceRelationShouldSucceed() throws AMWException { // given ForeignableOwner deletingOwner = ForeignableOwner.AMW; AbstractContext abstractContextMock = mock(AbstractContext.class); ResourceEntity resourceEntityMock = mock(ResourceEntity.class); PropertyEntity property = new PropertyEntity(); Set<PropertyEntity> properties = new HashSet<>(); properties.add(property); PropertyDescriptorEntity descriptor = new PropertyDescriptorEntityBuilder().withOwner(deletingOwner).withId(1).withProperties(properties).build(); Assert.assertEquals(deletingOwner, descriptor.getOwner()); ResourceContextEntity resourceContextEntityMock = mock(ResourceContextEntity.class); ConsumedResourceRelationEntity consumedResourceRelationEntityMock = mock(ConsumedResourceRelationEntity.class); ResourceRelationContextEntity resourceRelationContextEntityMock = mock(ResourceRelationContextEntity.class); TypedQuery<PropertyDescriptorEntity> queryMock = mock(TypedQuery.class); when(entityManagerMock.createQuery("from PropertyDescriptorEntity d left join fetch d.propertyTags where d.id = :propertyDescriptorId ", PropertyDescriptorEntity.class)).thenReturn(queryMock); when(queryMock.getSingleResult()).thenReturn(descriptor); when(resourceEntityMock.getContexts()).thenReturn(Collections.singleton(resourceContextEntityMock)); when(resourceEntityMock.getConsumedSlaveRelations()).thenReturn(Collections.singleton(consumedResourceRelationEntityMock)); when(consumedResourceRelationEntityMock.getContexts()).thenReturn(Collections.singleton(resourceRelationContextEntityMock)); when(resourceRelationContextEntityMock.getProperties()).thenReturn(properties); // when service.deletePropertyDescriptorByOwnerIncludingPropertyValues(descriptor, abstractContextMock, resourceEntityMock); // then verify(resourceRelationContextEntityMock).removeProperty(property); verify(entityManagerMock).remove(descriptor); }
@Test public void deletePropertyDescriptorByOwnerIncludingPropertyValuesWhenDeletingOwnerIsOwnerOfDescriptorDefinedOnResourceTypeWithPropertiesOnResourceRelationShouldSucceed() throws AMWException { // given ForeignableOwner deletingOwner = ForeignableOwner.AMW; AbstractContext abstractContextMock = mock(AbstractContext.class); ResourceTypeEntity resourceTypeEntityMock = mock(ResourceTypeEntity.class); PropertyEntity property = new PropertyEntity(); Set<PropertyEntity> properties = new HashSet<>(); properties.add(property); PropertyDescriptorEntity descriptor = new PropertyDescriptorEntityBuilder().withOwner(deletingOwner).withId(1).withProperties(properties).build(); Assert.assertEquals(deletingOwner, descriptor.getOwner()); ResourceRelationTypeEntity resourceRelationTypeEntityMock = mock(ResourceRelationTypeEntity.class); ConsumedResourceRelationEntity consumedResourceRelationEntityMock = mock(ConsumedResourceRelationEntity.class); ResourceRelationContextEntity resourceRelationContextEntityMock = mock(ResourceRelationContextEntity.class); TypedQuery<PropertyDescriptorEntity> queryMock = mock(TypedQuery.class); when(entityManagerMock.createQuery("from PropertyDescriptorEntity d left join fetch d.propertyTags where d.id = :propertyDescriptorId ", PropertyDescriptorEntity.class)).thenReturn(queryMock); when(queryMock.getSingleResult()).thenReturn(descriptor); when(resourceTypeEntityMock.getResourceRelationTypesB()).thenReturn(Collections.singleton(resourceRelationTypeEntityMock)); when(resourceRelationTypeEntityMock.getConsumedResourceRelations()).thenReturn(Collections.singleton(consumedResourceRelationEntityMock)); when(consumedResourceRelationEntityMock.getContexts()).thenReturn(Collections.singleton(resourceRelationContextEntityMock)); when(resourceRelationContextEntityMock.getProperties()).thenReturn(properties); // when service.deletePropertyDescriptorByOwnerIncludingPropertyValues(descriptor, abstractContextMock, resourceTypeEntityMock); // then verify(resourceRelationContextEntityMock).removeProperty(property); verify(entityManagerMock).remove(descriptor); }
@Test public void shouldStoreResourceIdInThreadLocalWhenPropertyDescriptorsInlcudingPropertyValuesAreDeleted() throws AMWException { // given ForeignableOwner deletingOwner = ForeignableOwner.AMW; Integer resourceId = 22; AbstractContext abstractContextMock = mock(AbstractContext.class); ResourceEntity resourceEntityMock = mock(ResourceEntity.class); when(resourceEntityMock.getId()).thenReturn(resourceId); Set<PropertyEntity> properties = new HashSet<>(); properties.add(new PropertyEntity()); PropertyDescriptorEntity descriptor = new PropertyDescriptorEntityBuilder().withOwner(deletingOwner).withId(1).withProperties(properties).build(); Assert.assertEquals(deletingOwner, descriptor.getOwner()); TypedQuery<PropertyDescriptorEntity> queryMock = mock(TypedQuery.class); when(entityManagerMock.createQuery("from PropertyDescriptorEntity d left join fetch d.propertyTags where d.id = :propertyDescriptorId ", PropertyDescriptorEntity.class)).thenReturn(queryMock); when(queryMock.getSingleResult()).thenReturn(descriptor); // when service.deletePropertyDescriptorByOwnerIncludingPropertyValues(descriptor, abstractContextMock, resourceEntityMock); // then assertThat("The resourceId Param must be stored as ThreadLocal variable for auditing (envers)", ThreadLocalUtil.getThreadVariable(ThreadLocalUtil.KEY_RESOURCE_ID), is(CoreMatchers.notNullValue())); int actualResourceId = (int) ThreadLocalUtil.getThreadVariable(ThreadLocalUtil.KEY_RESOURCE_ID); assertThat(actualResourceId, is(resourceId)); }