private void addAmbariServerIdentity(Map<String, String> kerberosEnvProperties) throws Exception {

    KerberosHelperImpl kerberosHelper = injector.getInstance(KerberosHelperImpl.class);

    boolean createAmbariIdentities = kerberosHelper.createAmbariIdentities(kerberosEnvProperties);

    KerberosIdentityDescriptor ambariKerberosIdentity = null;

    KerberosDescriptor kerberosDescriptor = createMock(KerberosDescriptor.class);
    if (createAmbariIdentities) {
      String ambariServerPrincipalName = "ambari-server${principal_suffix}@${realm}";
      KerberosPrincipalType ambariServerPrincipalType = KerberosPrincipalType.USER;
      String ambariServerKeytabFilePath = new File("ambari.server.keytab").getAbsolutePath();

      ambariKerberosIdentity = createMockIdentityDescriptor(
          KerberosHelper.AMBARI_SERVER_KERBEROS_IDENTITY_NAME,
          createMockPrincipalDescriptor(ambariServerPrincipalName, ambariServerPrincipalType, "ambari", null),
          createMockKeytabDescriptor(ambariServerKeytabFilePath, null));

      ArrayList<KerberosIdentityDescriptor> ambarServerComponent1Identities = new ArrayList<>();
      ambarServerComponent1Identities.add(ambariKerberosIdentity);

      ambarServerComponent1Identities.add(createMockIdentityDescriptor(
          "ambari-server_spnego",
          createMockPrincipalDescriptor("HTTP/_HOST@${realm}", KerberosPrincipalType.SERVICE, null, null),
          createMockKeytabDescriptor("spnego.service.keytab", null)));

      HashMap<String, KerberosComponentDescriptor> ambariServerComponentDescriptorMap = new HashMap<>();
      KerberosComponentDescriptor componentDescrptor = createMockComponentDescriptor(RootComponent.AMBARI_SERVER.name(), ambarServerComponent1Identities, null);
      ambariServerComponentDescriptorMap.put(RootComponent.AMBARI_SERVER.name(), componentDescrptor);

      KerberosServiceDescriptor ambariServiceKerberosDescriptor = createMockServiceDescriptor(RootService.AMBARI.name(), ambariServerComponentDescriptorMap, null, false);
      expect(ambariServiceKerberosDescriptor.getComponent(RootComponent.AMBARI_SERVER.name())).andReturn(componentDescrptor).once();

      expect(kerberosDescriptor.getService(RootService.AMBARI.name())).andReturn(ambariServiceKerberosDescriptor).once();
    }

    replayAll();

    // Needed by infrastructure
    injector.getInstance(AmbariMetaInfo.class).init();

    List<KerberosIdentityDescriptor> identities = (createAmbariIdentities)
        ? kerberosHelper.getAmbariServerIdentities(kerberosDescriptor)
        : new ArrayList<>();

    verifyAll();

    if (createAmbariIdentities) {
      Assert.assertEquals(2, identities.size());
      Assert.assertSame(ambariKerberosIdentity, identities.get(0));
    } else {
      Assert.assertTrue(identities.isEmpty());
    }
  }
Test that a Kerberos Descriptor that contains a Service with 0 Components does not raise any exceptions.
ambari-server/src/test/java/org/apache/ambari/server/controller/KerberosHelperTest.java