/** * 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); } } }
@Test public void shouldStoreResourceTypeIdInThreadLocalDuringPropertyUpdateInResourceTypeContext() throws AMWException { // given Integer resourceTypeIdForAuditLog = 1; ForeignableOwner deletingOwner = ForeignableOwner.AMW; Set<PropertyEntity> properties = new HashSet<>(); properties.add(new PropertyEntity()); PropertyDescriptorEntity descriptor = new PropertyDescriptorEntityBuilder().withOwner(deletingOwner).withId(1).withProperties(properties).build(); AbstractContext abstractContextMock = mock(AbstractContext.class); doNothing().when(service).removePropertyDescriptorByOwner(eq(descriptor), eq(abstractContextMock), anyBoolean()); doReturn(descriptor).when(service).getPropertyDescriptorWithTags(anyInt()); // when service.deletePropertyDescriptorByOwnerInResourceTypeContext(descriptor, abstractContextMock, resourceTypeIdForAuditLog); // then assertThat("The resourceTypeId Param must be stored as ThreadLocal variable for auditing (envers)", ThreadLocalUtil.getThreadVariable(ThreadLocalUtil.KEY_RESOURCE_TYPE_ID), is(CoreMatchers.notNullValue())); int resourceTypeId = (int) ThreadLocalUtil.getThreadVariable(ThreadLocalUtil.KEY_RESOURCE_TYPE_ID); assertThat(resourceTypeId, is(resourceTypeIdForAuditLog)); }