SetStaticFieldStage extends AbstractProcessStage { @Override public void process(LocalDocument doc) throws FieldAlreadyExistsException { for (Map.Entry<String, Object> entry : fieldValueMap.entrySet()) { final String key = entry.getKey(); if (!hasContent(doc, key) || overwritePolicy == Policy.OVERWRITE) { doc.putContentField(key, entry.getValue()); } else if (overwritePolicy == Policy.ADD) { addValueToField(doc, key, entry.getValue()); } else if (overwritePolicy == Policy.THROW) { throw new FieldAlreadyExistsException("Field " + key + " already has a value!"); } } }  @Override void process(LocalDocument doc); void setFieldValueMap(Map<String, Object> fieldValueMap); void setOverwritePolicy(Policy overwritePolicy);  }
@Test public void testOverwrite() throws Exception { init(Policy.OVERWRITE); stage.process(doc); Assert.assertEquals("value", doc.getContentField("empty")); Assert.assertEquals(1, doc.getContentField("string")); Assert.assertEquals("value", doc.getContentField("list")); }
@Test public void testSkip() throws Exception { init(Policy.SKIP); stage.process(doc); Assert.assertEquals("value", doc.getContentField("empty")); Assert.assertEquals("string", doc.getContentField("string")); Assert.assertTrue(((List<?>) doc.getContentField("list")).contains("x")); Assert.assertTrue(((List<?>) doc.getContentField("list")).contains(1)); }
@Test public void testSkipEmptyString() throws Exception { init(Policy.SKIP); doc.putContentField("empty", ""); stage.process(doc); Assert.assertEquals("Empty string should not count as content", "value", doc.getContentField("empty")); }
@Test public void testSkipEmptyList() throws Exception { init(Policy.SKIP); doc.putContentField("empty", Arrays.asList()); stage.process(doc); Assert.assertEquals("Empty list should not count as content", "value", doc.getContentField("empty")); }
@Test public void testThrowPolicyNoCollision() throws Exception { init(Policy.THROW); LocalDocument doc = new LocalDocument(); stage.process(doc); Assert.assertEquals("value", doc.getContentField("empty")); Assert.assertEquals(1, doc.getContentField("string")); Assert.assertEquals("value", doc.getContentField("list")); }
@Test(expected = SetStaticFieldStage.FieldAlreadyExistsException.class) public void testThrowPolicyWithCollision() throws Exception { init(Policy.THROW); stage.process(doc); }
@Test public void testAdd() throws Exception { init(Policy.ADD); stage.process(doc); Assert.assertEquals("value", doc.getContentField("empty")); Assert.assertTrue(doc.getContentField("string") instanceof List); Assert.assertTrue(((List<?>) doc.getContentField("string")).contains(1)); Assert.assertTrue(((List<?>) doc.getContentField("string")).contains("string")); Assert.assertTrue(((List<?>) doc.getContentField("list")).contains("x")); Assert.assertTrue(((List<?>) doc.getContentField("list")).contains(1)); Assert.assertTrue(((List<?>) doc.getContentField("list")).contains("value")); }