@Test
public void testUpdateKerberosConfigurationWithChangedKrb5ConfContent() throws Exception {

  final AmbariManagementController controller = createMock(AmbariManagementController.class);
  final AmbariMetaInfo metaInfo = createMock(AmbariMetaInfo.class);
  final StackInfo stackInfo = createMock(StackInfo.class);
  final ServiceInfo serviceInfo = createMock(ServiceInfo.class);
  final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
  final OsFamily osFamily = createNiceMock(OsFamily.class);
  final Map<String, String> propertiesKerberosEnv = new HashMap<String, String>() {
    {
      put("realm", "EXAMPLE.COM");
      put("encryption_types", "aes des3-cbc-sha1 rc4 des-cbc-md5");
      put("kdc_host", "c6407.ambari.apache.org");
      put("admin_server_host", "c6407.ambari.apache.org");
      put("kdc_type", "mit-kdc");
    }
  };
  final Map<String, String> propertiesKrb5Conf = new HashMap<String, String>() {
    {
      put("content", "CHANGED CONTENT");
    }
  };
  final Config configKerberosEnv = createNiceMock(Config.class);
  expect(configKerberosEnv.getProperties()).andReturn(propertiesKerberosEnv).anyTimes();
  expect(configKerberosEnv.getTag()).andReturn("tag1").anyTimes();
  final Config configKrb5Conf = createNiceMock(Config.class);
  expect(configKrb5Conf.getProperties()).andReturn(propertiesKrb5Conf).anyTimes();
  expect(configKrb5Conf.getTag()).andReturn("tag1").anyTimes();
  final Cluster cluster = createNiceMock(Cluster.class);
  expect(cluster.getDesiredConfigByType("kerberos-env")).andReturn(configKerberosEnv).once();
  expect(cluster.getDesiredConfigByType("krb5-conf")).andReturn(configKrb5Conf).once();
  final Clusters clusters = createNiceMock(Clusters.class);
  expect(clusters.getClusters()).andReturn(Collections.singletonMap("c1", cluster));
  expect(controller.getClusters()).andReturn(clusters).once();
  expect(controller.getAmbariMetaInfo()).andReturn(metaInfo).once();
  final StackId currentStackVersion = new StackId("HDP", "2.4.2");
  expect(metaInfo.getStack(currentStackVersion.getStackName(), currentStackVersion.getStackVersion()))
      .andReturn(stackInfo)
      .once();
  expect(stackInfo.getService("KERBEROS")).andReturn(serviceInfo).once();
  final PropertyInfo propertyInfo = new PropertyInfo();
  propertyInfo.setFilename("krb5-conf.xml");
  propertyInfo.setName("content");
  propertyInfo.setValue("new content template");
  expect(serviceInfo.getProperties()).andReturn(Collections.singletonList(propertyInfo)).once();
  expect(cluster.getConfigsByType("kerberos-env"))
      .andReturn(Collections.singletonMap("tag1", configKerberosEnv))
      .once();
  expect(cluster.getDesiredConfigByType("kerberos-env"))
      .andReturn(configKerberosEnv)
      .once();
  expect(cluster.getCurrentStackVersion()).andReturn(currentStackVersion).once();
  Capture<Cluster> clusterCapture = newCapture(CaptureType.ALL);
  Capture<String> typeCapture = newCapture(CaptureType.ALL);
  Capture<Map<String, String>> propertiesCapture = newCapture(CaptureType.ALL);
  Capture<String> tagCapture = newCapture(CaptureType.ALL);
  Capture<Map<String, Map<String, String>>> attributesCapture = newCapture(CaptureType.ALL);
  expect(controller.createConfig(capture(clusterCapture), capture(typeCapture),
      capture(propertiesCapture), capture(tagCapture), capture(attributesCapture)))
      .andReturn(createNiceMock(Config.class))
      .anyTimes();
  replay(controller, metaInfo, stackInfo, serviceInfo, dbAccessor, osFamily, cluster, configKerberosEnv, configKrb5Conf, clusters);
  final Injector injector = Guice.createInjector(new AbstractModule() {
    @Override
    protected void configure() {
      bind(AmbariManagementController.class).toInstance(controller);
      bind(DBAccessor.class).toInstance(dbAccessor);
      bind(OsFamily.class).toInstance(osFamily);
      bind(EntityManager.class).toInstance(entityManager);
      bind(PasswordEncoder.class).toInstance(createNiceMock(PasswordEncoder.class));
      bind(HookContextFactory.class).toInstance(createMock(HookContextFactory.class));
      bind(HookService.class).toInstance(createMock(HookService.class));
    }
  });
  injector.getInstance(UpgradeCatalog240.class).updateKerberosConfigs();
  verify(controller, metaInfo, stackInfo, serviceInfo, dbAccessor, osFamily, cluster, configKerberosEnv, configKrb5Conf, clusters);
  List<String> typeCaptureValues = typeCapture.getValues();
  Assert.assertEquals(1, typeCaptureValues.size());
  Assert.assertEquals("kerberos-env", typeCaptureValues.get(0));
  List<Map<String, String>> propertiesCaptureValues = propertiesCapture.getValues();
  Assert.assertEquals(1, propertiesCaptureValues.size());
  Map<String, String> capturedCRProperties;
  capturedCRProperties = propertiesCaptureValues.get(0);
  Assert.assertNotNull(capturedCRProperties);
  Assert.assertFalse(capturedCRProperties.containsKey("kdc_host"));
  Assert.assertTrue(capturedCRProperties.containsKey("kdc_hosts"));
  Assert.assertTrue(capturedCRProperties.containsKey("create_ambari_principal"));
  for (String property : propertiesKerberosEnv.keySet()) {
    if ("kdc_host".equals(property)) {
      Assert.assertEquals(property, propertiesKerberosEnv.get(property), capturedCRProperties.get("kdc_hosts"));
    } else if ("create_ambari_principal".equals(property)) {
      Assert.assertEquals(property, propertiesKerberosEnv.get(property), "false");
    } else {
      Assert.assertEquals(property, propertiesKerberosEnv.get(property), capturedCRProperties.get(property));
    }
  }
}