

package org.apache.ambari.logfeeder.mapper;

import java.util.HashMap;
import java.util.Map;

import org.apache.ambari.logsearch.config.zookeeper.model.inputconfig.impl.MapFieldNameDescriptorImpl;
import org.apache.log4j.Logger;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class MapperFieldNameTest {
  private static final Logger LOG = Logger.getLogger(MapperFieldNameTest.class);

  @Test
  public void testMapperFieldName_replaceField() {
    LOG.info("testMapperFieldName_replaceField()");

    MapFieldNameDescriptorImpl mapFieldNameDescriptor = new MapFieldNameDescriptorImpl();
    mapFieldNameDescriptor.setNewFieldName("someOtherField");

    MapperFieldName mapperFieldName = new MapperFieldName();
    assertTrue("Could not initialize!", mapperFieldName.init(null, "someField", null, mapFieldNameDescriptor));

    Map<String, Object> jsonObj = new HashMap<>();
    jsonObj.put("someField", "someValue");

    mapperFieldName.apply(jsonObj, "someOtherValue");

    assertFalse("Old field name wasn't removed", jsonObj.containsKey("someField"));
    assertEquals("New field wasn't put", "someOtherValue", jsonObj.remove("someOtherField"));
    assertTrue("jsonObj is not empty", jsonObj.isEmpty());
  }

  @Test
  public void testMapperFieldName_noNewFieldName() {
    LOG.info("testMapperFieldName_noNewFieldName()");

    MapFieldNameDescriptorImpl mapFieldNameDescriptor = new MapFieldNameDescriptorImpl();

    MapperFieldName mapperFieldName = new MapperFieldName();
    assertFalse("Was able to initialize!", mapperFieldName.init(null, "someField", null, mapFieldNameDescriptor));
  }
}
