@Test public void testIsValidTechnicalKey() throws Exception { assertThat(propertyValidationService.isValidTechnicalKey("test")).isTrue(); assertThat(propertyValidationService.isValidTechnicalKey("test.test")).isTrue(); assertThat(propertyValidationService.isValidTechnicalKey("test_test")).isTrue(); assertThat(propertyValidationService.isValidTechnicalKey("test-test")).isTrue(); assertThat(propertyValidationService.isValidTechnicalKey("test/test")).isTrue(); assertThat(propertyValidationService.isValidTechnicalKey("<test>")).isTrue(); assertThat(propertyValidationService.isValidTechnicalKey("${test}")).isTrue(); assertThat(propertyValidationService.isValidTechnicalKey("<test> </test>")).isTrue(); assertThat(propertyValidationService.isValidTechnicalKey("/")).isTrue(); assertThat(propertyValidationService.isValidTechnicalKey(".")).isTrue(); assertThat(propertyValidationService.isValidTechnicalKey("")).isFalse(); assertThat(propertyValidationService.isValidTechnicalKey(null)).isFalse(); }
private void checkForValidTechnicalKey(PropertyDescriptorEntity propertyDescriptor) throws AMWException { boolean isValid = propertyDescriptor != null && propertyDescriptor.getPropertyName() != null; if (!isValid || !propertyValidationService.isValidTechnicalKey(propertyDescriptor.getPropertyName().trim())) { throw new AMWException("The propertyname ('" + ((propertyDescriptor == null || propertyDescriptor.getPropertyName() == null) ? "" : propertyDescriptor.getPropertyName()) + "') is invalid!"); } }
@Test(expected = AMWException.class) public void savePropertyDescriptorForOwnerWhenTechnicalKeyIsInvalidShouldThrowException() throws AMWException { // given ForeignableOwner changingOwner = ForeignableOwner.AMW; AbstractContext abstractContextMock = mock(AbstractContext.class); PropertyDescriptorEntity descriptor = new PropertyDescriptorEntityBuilder().build(); List<PropertyTagEntity> tags = new ArrayList<>(); Assert.assertNull(descriptor.getId()); ResourceEntity resourceEntityMock = mock(ResourceEntity.class); when(propertyValidationServiceMock.isValidTechnicalKey(descriptor.getPropertyName())).thenReturn(false); // when service.savePropertyDescriptorForOwner(changingOwner, abstractContextMock, descriptor, tags, resourceEntityMock); }
@Test public void shouldStoreResourceIdInThreadLocalDuringPropertyDescriptorCreation() throws AMWException { // given ForeignableOwner changingOwner = ForeignableOwner.AMW; Integer expectedResourceId = 99; AbstractContext abstractContextMock = mock(AbstractContext.class); PropertyDescriptorEntity newDescriptor = new PropertyDescriptorEntityBuilder().build(); List<PropertyTagEntity> tags = new ArrayList<>(); ResourceEntity resourceEntityMock = mock(ResourceEntity.class); when(propertyValidationServiceMock.isValidTechnicalKey(newDescriptor.getPropertyName())).thenReturn(true); when(resourceEntityMock.getId()).thenReturn(expectedResourceId); // when service.savePropertyDescriptorForOwner(changingOwner, abstractContextMock, newDescriptor, tags, 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(expectedResourceId)); }
@Test public void shouldStoreResourceTypeIdInThreadLocalDuringPropertyDescriptorCreation() throws AMWException { // given ForeignableOwner changingOwner = ForeignableOwner.AMW; Integer resourceTypeId = 2; AbstractContext abstractContextMock = mock(AbstractContext.class); PropertyDescriptorEntity newDescriptor = new PropertyDescriptorEntityBuilder().build(); List<PropertyTagEntity> tags = new ArrayList<>(); ResourceTypeEntity resourceTypeEntityMock = mock(ResourceTypeEntity.class); doReturn(resourceTypeId).when(resourceTypeEntityMock).getId(); when(propertyValidationServiceMock.isValidTechnicalKey(newDescriptor.getPropertyName())).thenReturn(true); // when service.savePropertyDescriptorForOwner(changingOwner, abstractContextMock, newDescriptor, tags, resourceTypeEntityMock); // 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 actualResourceTypeId = (int) ThreadLocalUtil.getThreadVariable(ThreadLocalUtil.KEY_RESOURCE_TYPE_ID); assertThat(actualResourceTypeId, is(resourceTypeId)); }
@Test public void savePropertyDescriptorForOwnerWhenDescriptorIdIsNotNullAndSameOwnerShouldSavePropertyDescriptor() throws AMWException { // given ForeignableOwner changingOwner = ForeignableOwner.AMW; AbstractContext abstractContextMock = mock(AbstractContext.class); PropertyDescriptorEntity descriptor = new PropertyDescriptorEntityBuilder().withId(1).withOwner(changingOwner).build(); List<PropertyTagEntity> tags = new ArrayList<>(); when(propertyValidationServiceMock.isValidTechnicalKey(descriptor.getPropertyName())).thenReturn(true); Assert.assertNotNull(descriptor.getId()); // mock (implicit verify for merge) merging of descriptor PropertyDescriptorEntity mergedPropertyDescriptorMock = mock(PropertyDescriptorEntity.class); when(entityManagerMock.merge(descriptor)).thenReturn(mergedPropertyDescriptorMock); // mocking the manageChangeOfEncryptedPropertyDescriptor when(entityManagerMock.find(PropertyDescriptorEntity.class, descriptor.getId())).thenReturn(descriptor); when(entityManagerMock.createQuery("select p from PropertyEntity p where p.descriptor=:descriptor", PropertyEntity.class)).thenReturn(mock(TypedQuery.class)); ResourceEntity resourceEntityMock = mock(ResourceEntity.class); // when service.savePropertyDescriptorForOwner(changingOwner, abstractContextMock, descriptor, tags, resourceEntityMock); // then verify(propertyTagEditingServiceMock).updateTags(tags, mergedPropertyDescriptorMock); }
@Test public void shouldStoreResourceTypeIdInThreadLocalDuringPropertyDescriptorUpdate() throws AMWException { // given ForeignableOwner changingOwner = ForeignableOwner.AMW; Integer resourceTypeId = 2; AbstractContext abstractContextMock = mock(AbstractContext.class); PropertyDescriptorEntity newDescriptor = new PropertyDescriptorEntityBuilder().withId(2).build(); List<PropertyTagEntity> tags = new ArrayList<>(); ResourceTypeEntity resourceTypeEntityMock = mock(ResourceTypeEntity.class); doReturn(resourceTypeId).when(resourceTypeEntityMock).getId(); when(propertyValidationServiceMock.isValidTechnicalKey(newDescriptor.getPropertyName())).thenReturn(true); when(entityManagerMock.find(PropertyDescriptorEntity.class, newDescriptor.getId())).thenReturn(newDescriptor); when(entityManagerMock.merge(newDescriptor)).thenReturn(newDescriptor); // when service.savePropertyDescriptorForOwner(changingOwner, abstractContextMock, newDescriptor, tags, resourceTypeEntityMock); // 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 actualResourceTypeId = (int) ThreadLocalUtil.getThreadVariable(ThreadLocalUtil.KEY_RESOURCE_TYPE_ID); assertThat(actualResourceTypeId, is(resourceTypeId)); }
@Test(expected = AMWException.class) public void savePropertyDescriptorShouldNotCreateMultiplePropertyDescriptorsWithSameName() throws AMWException { // given ForeignableOwner changingOwner = ForeignableOwner.AMW; PropertyDescriptorEntity descriptor = new PropertyDescriptorEntityBuilder().withPropertyName("existing").build(); AbstractContext abstractContextMock = mock(AbstractContext.class); PropertyDescriptorEntity newDescriptor = new PropertyDescriptorEntityBuilder().withPropertyName("existing").build(); List<PropertyTagEntity> tags = new ArrayList<>(); ResourceEntity resourceEntityMock = mock(ResourceEntity.class); when(propertyValidationServiceMock.isValidTechnicalKey(descriptor.getPropertyName())).thenReturn(true); when(abstractContextMock.getPropertyDescriptors()).thenReturn(new HashSet<>(Collections.singletonList(descriptor))); // when service.savePropertyDescriptorForOwner(changingOwner, abstractContextMock, newDescriptor, tags, resourceEntityMock); }
@Test public void savePropertyDescriptorForOwnerWhenDescriptorIdIsNullShouldCreateNewPropertyDescriptorForOwner() throws AMWException { // given ForeignableOwner changingOwner = ForeignableOwner.AMW; AbstractContext abstractContextMock = mock(AbstractContext.class); PropertyDescriptorEntity descriptor = new PropertyDescriptorEntityBuilder().build(); List<PropertyTagEntity> tags = new ArrayList<>(); ResourceEntity resourceEntityMock = mock(ResourceEntity.class); when(propertyValidationServiceMock.isValidTechnicalKey(descriptor.getPropertyName())).thenReturn(true); Assert.assertNull(descriptor.getId()); // when service.savePropertyDescriptorForOwner(changingOwner, abstractContextMock, descriptor, tags, resourceEntityMock); // then verify(abstractContextMock).addPropertyDescriptor(descriptor); verify(entityManagerMock).persist(descriptor); verify(propertyTagEditingServiceMock).updateTags(tags, descriptor); verify(entityManagerMock).persist(abstractContextMock); }
@Test public void savePropertyDescriptorForOwnerWhenDescriptorIdIsNotNullNoForeignableFieldsChangedButDifferentOwnerShouldSavePropertyDescriptor() throws AMWException { // given ForeignableOwner changingOwner = ForeignableOwner.AMW; AbstractContext abstractContextMock = mock(AbstractContext.class); PropertyDescriptorEntity descriptor = new PropertyDescriptorEntityBuilder().withId(1).withOwner(ForeignableOwner.MAIA).build(); List<PropertyTagEntity> tags = new ArrayList<>(); when(propertyValidationServiceMock.isValidTechnicalKey(descriptor.getPropertyName())).thenReturn(true); Assert.assertNotNull(descriptor.getId()); // mock (implicit verify for merge) merging of descriptor PropertyDescriptorEntity mergedPropertyDescriptorMock = mock(PropertyDescriptorEntity.class); when(entityManagerMock.merge(descriptor)).thenReturn(mergedPropertyDescriptorMock); // mocking the manageChangeOfEncryptedPropertyDescriptor when(entityManagerMock.createQuery("select p from PropertyEntity p where p.descriptor=:descriptor", PropertyEntity.class)).thenReturn(mock(TypedQuery.class)); // return descriptor with same values (not changed fields) when(entityManagerMock.find(PropertyDescriptorEntity.class, descriptor.getId())).thenReturn(descriptor); ResourceEntity resourceEntityMock = mock(ResourceEntity.class); // when service.savePropertyDescriptorForOwner(changingOwner, abstractContextMock, descriptor, tags, resourceEntityMock); // then verify(propertyTagEditingServiceMock).updateTags(tags, mergedPropertyDescriptorMock); }
@Test public void shouldStoreResourceIdInThreadLocalDuringPropertyDescriptorUpdate() throws AMWException { //given ForeignableOwner changingOwner = ForeignableOwner.AMW; Integer expectedResourceId = 99; AbstractContext abstractContextMock = mock(AbstractContext.class); PropertyDescriptorEntity descriptorToUpdate = new PropertyDescriptorEntityBuilder().withId(1).withOwner(ForeignableOwner.MAIA).build(); List<PropertyTagEntity> tags = new ArrayList<>(); when(propertyValidationServiceMock.isValidTechnicalKey(descriptorToUpdate.getPropertyName())).thenReturn(true); Assert.assertNotNull(descriptorToUpdate.getId()); // mock (implicit verify for merge) merging of descriptorToUpdate PropertyDescriptorEntity mergedPropertyDescriptorMock = mock(PropertyDescriptorEntity.class); when(entityManagerMock.merge(descriptorToUpdate)).thenReturn(mergedPropertyDescriptorMock); // mocking the manageChangeOfEncryptedPropertyDescriptor when(entityManagerMock.createQuery("select p from PropertyEntity p where p.descriptor=:descriptor", PropertyEntity.class)).thenReturn(mock(TypedQuery.class)); // return descriptorToUpdate with same values (not changed fields) when(entityManagerMock.find(PropertyDescriptorEntity.class, descriptorToUpdate.getId())).thenReturn(descriptorToUpdate); ResourceEntity resourceEntityMock = mock(ResourceEntity.class); when(resourceEntityMock.getId()).thenReturn(expectedResourceId); // when service.savePropertyDescriptorForOwner(changingOwner, abstractContextMock, descriptorToUpdate, tags, 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(expectedResourceId)); }