PropertyResolver implements ConfigTextResolver { @Override public ItemChangeSets resolve(long namespaceId, String configText, List<ItemDTO> baseItems) { Map<Integer, ItemDTO> oldLineNumMapItem = BeanUtils.mapByKey("lineNum", baseItems); Map<String, ItemDTO> oldKeyMapItem = BeanUtils.mapByKey("key", baseItems); oldKeyMapItem.remove(""); String[] newItems = configText.split(ITEM_SEPARATOR); if (isHasRepeatKey(newItems)) { throw new BadRequestException("config text has repeat key please check."); } ItemChangeSets changeSets = new ItemChangeSets(); Map<Integer, String> newLineNumMapItem = new HashMap<>(); int lineCounter = 1; for (String newItem : newItems) { newItem = newItem.trim(); newLineNumMapItem.put(lineCounter, newItem); ItemDTO oldItemByLine = oldLineNumMapItem.get(lineCounter); if (isCommentItem(newItem)) { handleCommentLine(namespaceId, oldItemByLine, newItem, lineCounter, changeSets); } else if (isBlankItem(newItem)) { handleBlankLine(namespaceId, oldItemByLine, lineCounter, changeSets); } else { handleNormalLine(namespaceId, oldKeyMapItem, newItem, lineCounter, changeSets); } lineCounter++; } deleteCommentAndBlankItem(oldLineNumMapItem, newLineNumMapItem, changeSets); deleteNormalKVItem(oldKeyMapItem, changeSets); return changeSets; }  @Override ItemChangeSets resolve(long namespaceId, String configText, List<ItemDTO> baseItems);  }
@Test public void testEmptyText() { try { resolver.resolve(0, "", null); } catch (Exception e) { Assert.assertTrue(e instanceof BadRequestException); } }
@Test public void testRepeatKey() { try { resolver.resolve(1, "a=b\nb=c\nA=d\nB=e", Collections.emptyList()); } catch (Exception e) { Assert.assertTrue(e instanceof BadRequestException); } }
@Test public void testAddItemBeforeNoItem() { ItemChangeSets changeSets = resolver.resolve(1, "a=b\nb=c", Collections.emptyList()); Assert.assertEquals(2, changeSets.getCreateItems().size()); }
@Test public void testAddItemBeforeHasItem() { ItemChangeSets changeSets = resolver.resolve(1, "x=y\na=b\nb=c\nc=d", mockBaseItemHas3Key()); Assert.assertEquals("x", changeSets.getCreateItems().get(0).getKey()); Assert.assertEquals(1, changeSets.getCreateItems().size()); Assert.assertEquals(3, changeSets.getUpdateItems().size()); }
@Test public void testAddCommentAndBlankItem() { ItemChangeSets changeSets = resolver.resolve(1, "#ddd\na=b\n\nb=c\nc=d", mockBaseItemHas3Key()); Assert.assertEquals(2, changeSets.getCreateItems().size()); Assert.assertEquals(3, changeSets.getUpdateItems().size()); }
@Test public void testChangeItemNumLine() { ItemChangeSets changeSets = resolver.resolve(1, "b=c\nc=d\na=b", mockBaseItemHas3Key()); Assert.assertEquals(3, changeSets.getUpdateItems().size()); }
@Test public void testDeleteItem() { ItemChangeSets changeSets = resolver.resolve(1, "a=b", mockBaseItemHas3Key()); Assert.assertEquals(2, changeSets.getDeleteItems().size()); }
@Test public void testDeleteCommentItem() { ItemChangeSets changeSets = resolver.resolve(1, "a=b\n\nb=c", mockBaseItemWith2Key1Comment1Blank()); Assert.assertEquals(2, changeSets.getDeleteItems().size()); Assert.assertEquals(2, changeSets.getUpdateItems().size()); Assert.assertEquals(1, changeSets.getCreateItems().size()); }
@Test public void testDeleteBlankItem(){ ItemChangeSets changeSets = resolver.resolve(1, "#qqqq\na=b\nb=c", mockBaseItemWith2Key1Comment1Blank()); Assert.assertEquals(1, changeSets.getDeleteItems().size()); Assert.assertEquals(1, changeSets.getUpdateItems().size()); Assert.assertEquals(0, changeSets.getCreateItems().size()); }
@Test public void testUpdateItem() { ItemChangeSets changeSets = resolver.resolve(1, "a=d", mockBaseItemHas3Key()); List<ItemDTO> updateItems = changeSets.getUpdateItems(); Assert.assertEquals(1, updateItems.size()); Assert.assertEquals("d", updateItems.get(0).getValue()); }
@Test public void testUpdateCommentItem() { ItemChangeSets changeSets = resolver.resolve(1, "#ww\n" + "a=b\n" +"\n" + "b=c", mockBaseItemWith2Key1Comment1Blank()); Assert.assertEquals(1, changeSets.getDeleteItems().size()); Assert.assertEquals(0, changeSets.getUpdateItems().size()); Assert.assertEquals(1, changeSets.getCreateItems().size()); }
@Test public void testAllSituation(){ ItemChangeSets changeSets = resolver.resolve(1, "#ww\nd=e\nb=c\na=b\n\nq=w\n#eee", mockBaseItemWith2Key1Comment1Blank()); Assert.assertEquals(2, changeSets.getDeleteItems().size()); Assert.assertEquals(2, changeSets.getUpdateItems().size()); Assert.assertEquals(5, changeSets.getCreateItems().size()); }