TagDaoHelper { public void validateParentTagEntity(TagEntity tagEntity, TagEntity parentTagEntity) { Integer maxAllowedTagNesting = configurationHelper.getProperty(ConfigurationValue.MAX_ALLOWED_TAG_NESTING, Integer.class); TagEntity localParentTagEntity = parentTagEntity; int level = 0; while (localParentTagEntity != null) { Assert.isTrue(!tagEntity.equals(localParentTagEntity), "Parent tag key cannot be the requested tag key or any of its children’s tag keys."); localParentTagEntity = localParentTagEntity.getParentTagEntity(); if (level++ >= maxAllowedTagNesting) { throw new IllegalArgumentException("Exceeds maximum allowed tag nesting level of " + maxAllowedTagNesting); } } }  void assertDisplayNameDoesNotExistForTag(String tagCode, String displayName); List<TagEntity> getTagChildrenEntities(TagEntity parentTagEntity); TagEntity getTagEntity(TagKey tagKey); void validateParentTagEntity(TagEntity tagEntity, TagEntity parentTagEntity); void validateParentTagType(String tagTypeCode, String parentTagTypeCode);  }
@Test public void testValidateParentTagEntity() { TagTypeEntity tagTypeEntity = new TagTypeEntity(); tagTypeEntity.setCode(TAG_TYPE_CODE); TagEntity tagEntity = new TagEntity(); tagEntity.setTagType(tagTypeEntity); tagEntity.setTagCode(TAG_CODE); tagEntity.setDisplayName(TAG_DISPLAY_NAME); TagEntity parentTagEntity = new TagEntity(); parentTagEntity.setTagType(tagTypeEntity); parentTagEntity.setTagCode(TAG_CODE_2); parentTagEntity.setDisplayName(TAG_DISPLAY_NAME_2); parentTagEntity.setChildrenTagEntities(Lists.newArrayList(tagEntity)); tagEntity.setParentTagEntity(parentTagEntity); when(configurationHelper.getProperty(ConfigurationValue.MAX_ALLOWED_TAG_NESTING, Integer.class)).thenReturn(1); tagDaoHelper.validateParentTagEntity(tagEntity, parentTagEntity); verify(configurationHelper).getProperty(ConfigurationValue.MAX_ALLOWED_TAG_NESTING, Integer.class); verifyNoMoreInteractions(configurationHelper, tagDao); }
@Test public void testValidateParentTagEntityWithLevelGreaterThanMaxAllowedTagNesting() { TagTypeEntity tagTypeEntity = new TagTypeEntity(); tagTypeEntity.setCode(TAG_TYPE_CODE); TagEntity tagEntity = new TagEntity(); tagEntity.setTagType(tagTypeEntity); tagEntity.setTagCode(TAG_CODE); tagEntity.setDisplayName(TAG_DISPLAY_NAME); TagEntity parentTagEntity = new TagEntity(); parentTagEntity.setTagType(tagTypeEntity); parentTagEntity.setTagCode(TAG_CODE_2); parentTagEntity.setDisplayName(TAG_DISPLAY_NAME_2); parentTagEntity.setChildrenTagEntities(Lists.newArrayList(tagEntity)); tagEntity.setParentTagEntity(parentTagEntity); when(configurationHelper.getProperty(ConfigurationValue.MAX_ALLOWED_TAG_NESTING, Integer.class)).thenReturn(0); try { tagDaoHelper.validateParentTagEntity(tagEntity, parentTagEntity); fail("Expected to throw an exception."); } catch (IllegalArgumentException illegalArgumentException) { assertThat("Exception message is not correct.", illegalArgumentException.getMessage(), is(equalTo("Exceeds maximum allowed tag nesting level of " + 0))); } verify(configurationHelper).getProperty(ConfigurationValue.MAX_ALLOWED_TAG_NESTING, Integer.class); verifyNoMoreInteractions(configurationHelper, tagDao); }
@Test public void testValidateParentTagEntityWithTagEntityEqualToParentTagEntity() { TagTypeEntity tagTypeEntity = new TagTypeEntity(); tagTypeEntity.setCode(TAG_TYPE_CODE); TagEntity tagEntity = new TagEntity(); tagEntity.setTagType(tagTypeEntity); tagEntity.setTagCode(TAG_CODE); tagEntity.setDisplayName(TAG_DISPLAY_NAME); when(configurationHelper.getProperty(ConfigurationValue.MAX_ALLOWED_TAG_NESTING, Integer.class)).thenReturn(0); try { tagDaoHelper.validateParentTagEntity(tagEntity, tagEntity); fail("Expected to throw an exception."); } catch (IllegalArgumentException illegalArgumentException) { assertThat("Exception message is not correct.", illegalArgumentException.getMessage(), is(equalTo("Parent tag key cannot be the requested tag key or any of its children’s tag keys."))); } verify(configurationHelper).getProperty(ConfigurationValue.MAX_ALLOWED_TAG_NESTING, Integer.class); verifyNoMoreInteractions(configurationHelper, tagDao); }