@Test(expected = ValidationException.class) public void setPropertyValueShouldThrowValidationExceptionWhenPropertyIsNotAllowedToSetOnContext() throws ValidationException { // given String unobfuscatedValue = "some value"; PropertyEntity propertyMock = mock(PropertyEntity.class); Mockito.when(resourceContextMock.getPropertyForDescriptor(propertyDescriptorId)).thenReturn(propertyMock); Mockito.when(propertyValidationServiceMock.canPropertyValueBeSetOnContext(Mockito.any(PropertyDescriptorEntity.class), Mockito.any(ContextDependency.class))).thenReturn(false); //when service.setPropertyValue(resourceContextMock, propertyDescriptorId, unobfuscatedValue); }
private PropertyDescriptorEntity savePropertyDescriptorResource(ForeignableOwner editingOwner, Integer resourceId, PropertyDescriptorEntity descriptor, String propertyTagsString) throws AMWException { ResourceEntity attachedResource = entityManager.find(ResourceEntity.class, Objects.requireNonNull(resourceId)); ResourceContextEntity resourceContext = attachedResource.getOrCreateContext(contextService.getGlobalResourceContextEntity()); ResourceTypeContextEntity resourceTypeContextEntity = attachedResource.getResourceType().getOrCreateContext(contextService.getGlobalResourceContextEntity()); List<String> duplicatePropertyDescriptorNames = propertyValidationService.getDuplicatePropertyDescriptors(resourceContext, resourceTypeContextEntity, descriptor); if (!duplicatePropertyDescriptorNames.isEmpty()) { throw new AMWException("Failure - duplicate propertydescriptors: " + duplicatePropertyDescriptorNames); } return propertyDescriptorService.savePropertyDescriptorForOwner(editingOwner, resourceContext, descriptor, propertyTagEditingService.convertToTags(propertyTagsString), attachedResource); }
@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(); }
public boolean canPropertyValueBeSetOnContext(PropertyDescriptorEntity property, ContextDependency<?> contextDependency){ if(isHostNameProperty(property) && isDefinedOnNode(contextDependency)){ return contextDependency.getContext().isEnvironment(); } return true; }
public List<String> getDuplicatePropertyDescriptors(ResourceContextEntity resourceContext, ResourceTypeContextEntity resourceTypeContextEntity, PropertyDescriptorEntity...propertyDescriptors) { List<PropertyDescriptorEntity> descriptors = Arrays.asList(propertyDescriptors); return duplicatePropertyNames(descriptors, resourceContext, resourceTypeContextEntity); }
@Before public void setUp() { propertyValidationService = new PropertyValidationService(); hostNameProperty = new PropertyDescriptorEntityBuilder().withPropertyName(AppServerRelationsTemplateProcessor.HOST_NAME).build(); otherThanHostNameProperty = new PropertyDescriptorEntityBuilder().withPropertyName("anyPropertyName").build(); nodeResourceType = new ResourceTypeEntityBuilder().buildResourceTypeEntity(DefaultResourceTypeDefinition.NODE.name(), null, false); nodeResource = new ResourceEntityBuilder().withType(nodeResourceType).build(); otherThanNodeResourceType = new ResourceTypeEntityBuilder().buildResourceTypeEntity("other", null, false); otherThanNodeResource = new ResourceEntityBuilder().withType(otherThanNodeResourceType).build(); assertThat(hostNameProperty.getPropertyName()).isEqualTo(AppServerRelationsTemplateProcessor.HOST_NAME); assertThat(otherThanHostNameProperty.getPropertyName()).isNotEqualTo(AppServerRelationsTemplateProcessor.HOST_NAME); }
@Test public void setPropertyValueShouldLoadPropertyFromDBAndAddToContextWhenPropertyIsNotYetInContext() throws ValidationException { // given String unobfuscatedValue = "some value"; Mockito.when(resourceContextMock.getPropertyForDescriptor(propertyDescriptorId)).thenReturn(null); Mockito.when(propertyValidationServiceMock.canPropertyValueBeSetOnContext(Mockito.any(PropertyDescriptorEntity.class), Mockito.any(ContextDependency.class))).thenReturn(true); //when service.setPropertyValue(resourceContextMock, propertyDescriptorId, unobfuscatedValue); // then Mockito.verify(entityManagerMock).find(PropertyDescriptorEntity.class, propertyDescriptorId); Mockito.verify(resourceContextMock).addProperty(Mockito.any(PropertyEntity.class)); }
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 savePropertyDescriptorForResourceWhenDublicateDescriptorNamesShouldThrowException() throws ForeignableOwnerViolationException, AMWException { // given ForeignableOwner changingOwner = ForeignableOwner.AMW; Integer resourceId = 1; PropertyDescriptorEntity descriptor = new PropertyDescriptorEntityBuilder().withId(2).build(); String propertyTagsString = "propertyTagsString"; ResourceEntity resourceEntityMock = mock(ResourceEntity.class); ResourceTypeEntity resourceTypeEntityMock = mock(ResourceTypeEntity.class); ResourceContextEntity resourceContextEntityMock = mock(ResourceContextEntity.class); ResourceTypeContextEntity resourceTypeContextEntityMock = mock(ResourceTypeContextEntity.class); when(entityManagerMock.find(ResourceEntity.class, resourceId)).thenReturn(resourceEntityMock); when(resourceEntityMock.getOrCreateContext(any(ContextEntity.class))).thenReturn(resourceContextEntityMock); when(resourceEntityMock.getResourceType()).thenReturn(resourceTypeEntityMock); when(resourceTypeEntityMock.getOrCreateContext(any(ContextEntity.class))).thenReturn(resourceTypeContextEntityMock); List<String> notEmptyList = new ArrayList<>(); notEmptyList.add("dublicat property"); when(propertyValidationServiceMock.getDuplicatePropertyDescriptors(resourceContextEntityMock, resourceTypeContextEntityMock, descriptor)).thenReturn(notEmptyList); // when editor.savePropertyDescriptorForResource(changingOwner, resourceId, descriptor, descriptor.foreignableFieldHashCode(), propertyTagsString); // then }
@Test public void setPropertyValueShouldSetValueAndCommentToPropertyInContext() throws ValidationException { // given String unobfuscatedValue = "some value"; PropertyEntity propertyMock = mock(PropertyEntity.class); Mockito.when(resourceContextMock.getPropertyForDescriptor(propertyDescriptorId)).thenReturn(propertyMock); Mockito.when(propertyValidationServiceMock.canPropertyValueBeSetOnContext(Mockito.any(PropertyDescriptorEntity.class), Mockito.any(ContextDependency.class))).thenReturn(true); //when service.setPropertyValue(resourceContextMock, propertyDescriptorId, unobfuscatedValue); // then Mockito.verify(propertyMock).setValue(unobfuscatedValue); }
@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 savePropertyDescriptorForResourceShouldDelegatePropertyTagEditingService() throws ForeignableOwnerViolationException, AMWException { // given ForeignableOwner changingOwner = ForeignableOwner.AMW; Integer resourceId = 1; PropertyDescriptorEntity descriptor = new PropertyDescriptorEntityBuilder().withId(2).build(); String propertyTagsString = "propertyTagsString"; ResourceEntity resourceEntityMock = mock(ResourceEntity.class); ResourceTypeEntity resourceTypeEntityMock = mock(ResourceTypeEntity.class); ResourceContextEntity resourceContextEntityMock = mock(ResourceContextEntity.class); ResourceTypeContextEntity resourceTypeContextEntityMock = mock(ResourceTypeContextEntity.class); when(entityManagerMock.find(ResourceEntity.class, resourceId)).thenReturn(resourceEntityMock); when(resourceEntityMock.getOrCreateContext(any(ContextEntity.class))).thenReturn(resourceContextEntityMock); when(resourceEntityMock.getResourceType()).thenReturn(resourceTypeEntityMock); when(resourceTypeEntityMock.getOrCreateContext(any(ContextEntity.class))).thenReturn(resourceTypeContextEntityMock); List<String> emptyList = new ArrayList<>(); when(propertyValidationServiceMock.getDuplicatePropertyDescriptors(resourceContextEntityMock, resourceTypeContextEntityMock, descriptor)).thenReturn(emptyList); // when editor.savePropertyDescriptorForResource(changingOwner, resourceId, descriptor, descriptor.foreignableFieldHashCode(), propertyTagsString); // then verify(propertyTagEditingServiceMock).convertToTags(propertyTagsString); }
@Test public void canPropertyValueBeSetOnContextForOtherThanHostNamePropertyAndNodeResourceOnDomainContextShouldReturnFalse() { // given ContextDependency<ResourceEntity> nodeResourceDependency = createFor(nodeResource, ContextNames.DOMAIN.name()); // when boolean b = propertyValidationService.canPropertyValueBeSetOnContext(hostNameProperty, nodeResourceDependency); // then assertThat(b).isFalse(); }
@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 savePropertyDescriptorForResourceShouldVerifyIfEditableByOwner() throws ForeignableOwnerViolationException, AMWException { // given ForeignableOwner changingOwner = ForeignableOwner.AMW; Integer resourceId = 1; PropertyDescriptorEntity descriptor = new PropertyDescriptorEntityBuilder().withId(2).build(); String propertyTagsString = "propertyTagsString"; ResourceEntity resourceEntityMock = mock(ResourceEntity.class); ResourceTypeEntity resourceTypeEntityMock = mock(ResourceTypeEntity.class); ResourceContextEntity resourceContextEntityMock = mock(ResourceContextEntity.class); ResourceTypeContextEntity resourceTypeContextEntityMock = mock(ResourceTypeContextEntity.class); when(entityManagerMock.find(ResourceEntity.class, resourceId)).thenReturn(resourceEntityMock); when(resourceEntityMock.getOrCreateContext(any(ContextEntity.class))).thenReturn(resourceContextEntityMock); when(resourceEntityMock.getResourceType()).thenReturn(resourceTypeEntityMock); when(resourceTypeEntityMock.getOrCreateContext(any(ContextEntity.class))).thenReturn(resourceTypeContextEntityMock); List<String> emptyList = new ArrayList<>(); when(propertyValidationServiceMock.getDuplicatePropertyDescriptors(resourceContextEntityMock, resourceTypeContextEntityMock, descriptor)).thenReturn(emptyList); when(permissionBoundaryMock.hasPermission(Permission.IGNORE_FOREIGNABLE_OWNER)).thenReturn(false); List<PropertyTagEntity> propertyTags = new ArrayList<>(); propertyTags.add(new PropertyTagEntity()); when(propertyTagEditingServiceMock.convertToTags(propertyTagsString)).thenReturn(propertyTags); // when editor.savePropertyDescriptorForResource(changingOwner, resourceId, descriptor, descriptor.foreignableFieldHashCode(), propertyTagsString); // then verify(foreignableServiceMock).verifyEditableByOwner(changingOwner, descriptor.foreignableFieldHashCode(),descriptor); }
@Test public void canPropertyValueBeSetOnContextForHostNamePropertyAndOtherThanNodeResourceTypeOnDomainContextShouldReturnTrue() { // given ContextDependency<ResourceEntity> nodeResourceDependency = createFor(otherThanNodeResourceType, ContextNames.DOMAIN.name()); // when boolean b = propertyValidationService.canPropertyValueBeSetOnContext(hostNameProperty, nodeResourceDependency); // then assertThat(b).isTrue(); }
@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 public void savePropertyDescriptorForResourceShouldSaveWithOwner() throws ForeignableOwnerViolationException, AMWException { // given ForeignableOwner changingOwner = ForeignableOwner.AMW; Integer resourceId = 1; PropertyDescriptorEntity descriptor = new PropertyDescriptorEntityBuilder().withId(2).build(); String propertyTagsString = "propertyTagsString"; ResourceEntity resourceEntityMock = mock(ResourceEntity.class); ResourceTypeEntity resourceTypeEntityMock = mock(ResourceTypeEntity.class); ResourceContextEntity resourceContextEntityMock = mock(ResourceContextEntity.class); ResourceTypeContextEntity resourceTypeContextEntityMock = mock(ResourceTypeContextEntity.class); when(entityManagerMock.find(ResourceEntity.class, resourceId)).thenReturn(resourceEntityMock); when(resourceEntityMock.getOrCreateContext(any(ContextEntity.class))).thenReturn(resourceContextEntityMock); when(resourceEntityMock.getResourceType()).thenReturn(resourceTypeEntityMock); when(resourceTypeEntityMock.getOrCreateContext(any(ContextEntity.class))).thenReturn(resourceTypeContextEntityMock); List<String> emptyList = new ArrayList<>(); when(propertyValidationServiceMock.getDuplicatePropertyDescriptors(resourceContextEntityMock, resourceTypeContextEntityMock, descriptor)).thenReturn(emptyList); when(permissionBoundaryMock.hasPermission(Permission.IGNORE_FOREIGNABLE_OWNER)).thenReturn(false); List<PropertyTagEntity> propertyTags = new ArrayList<>(); propertyTags.add(new PropertyTagEntity()); when(propertyTagEditingServiceMock.convertToTags(propertyTagsString)).thenReturn(propertyTags); // when editor.savePropertyDescriptorForResource(changingOwner, resourceId, descriptor, descriptor.foreignableFieldHashCode(), propertyTagsString); // then verify(propertyDescriptorServiceMock).savePropertyDescriptorForOwner(changingOwner, resourceContextEntityMock, descriptor, propertyTags, resourceEntityMock); }
@Test public void canPropertyValueBeSetOnContextForOtherThanHostNamePropertyAndOtherThanNodeResourceOnEnvContextShouldReturnTrue() { // given ContextDependency<ResourceEntity> nodeResourceDependency = createFor(otherThanNodeResource, ContextNames.ENV.name()); // when boolean b = propertyValidationService.canPropertyValueBeSetOnContext(hostNameProperty, nodeResourceDependency); // then assertThat(b).isTrue(); }
@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); }