text:
package org.apache.ambari.server.upgrade;

import javax.persistence.EntityManager;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertNull;
import static junit.framework.Assert.assertTrue;
import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.anyString;
import static org.easymock.EasyMock.capture;
import static org.easymock.EasyMock.createMockBuilder;
import static org.easymock.EasyMock.createNiceMock;
import static org.easymock.EasyMock.createStrictMock;
import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.ambari.server.AmbariException;
import org.apache.ambari.server.H2DatabaseCleaner;
import org.apache.ambari.server.api.services.AmbariMetaInfo;
import org.apache.ambari.server.configuration.Configuration;
import org.apache.ambari.server.controller.AmbariManagementController;
import org.apache.ambari.server.controller.ServiceConfigVersionResponse;
import org.apache.ambari.server.orm.DBAccessor;
import org.apache.ambari.server.orm.DBAccessor.DBColumnInfo;
import org.apache.ambari.server.orm.GuiceJpaInitializer;
import org.apache.ambari.server.orm.InMemoryDefaultTestModule;
import org.apache.ambari.server.orm.dao.ArtifactDAO;
import org.apache.ambari.server.orm.dao.ClusterDAO;
import org.apache.ambari.server.orm.dao.ClusterStateDAO;
import org.apache.ambari.server.orm.dao.HostComponentDesiredStateDAO;
import org.apache.ambari.server.orm.dao.ServiceComponentDesiredStateDAO;
import org.apache.ambari.server.orm.dao.StackDAO;
import org.apache.ambari.server.orm.entities.ArtifactEntity;
import org.apache.ambari.server.orm.entities.ClusterEntity;
import org.apache.ambari.server.orm.entities.ClusterServiceEntity;
import org.apache.ambari.server.orm.entities.ClusterStateEntity;
import org.apache.ambari.server.orm.entities.HostComponentDesiredStateEntity;
import org.apache.ambari.server.orm.entities.HostEntity;
import org.apache.ambari.server.orm.entities.ServiceComponentDesiredStateEntity;
import org.apache.ambari.server.orm.entities.StackEntity;
import org.apache.ambari.server.state.Cluster;
import org.apache.ambari.server.state.Clusters;
import org.apache.ambari.server.state.Config;
import org.apache.ambari.server.state.ConfigHelper;
import org.apache.ambari.server.state.Host;
import org.apache.ambari.server.state.HostComponentAdminState;
import org.apache.ambari.server.state.Service;
import org.apache.ambari.server.state.kerberos.KerberosDescriptor;
import org.apache.ambari.server.state.kerberos.KerberosDescriptorFactory;
import org.apache.ambari.server.state.kerberos.KerberosServiceDescriptor;
import org.apache.ambari.server.state.stack.OsFamily;
import org.easymock.Capture;
import org.easymock.CaptureType;
import org.easymock.EasyMockSupport;
import org.junit.Assert;
import org.junit.Test;

import com.google.inject.AbstractModule;
import com.google.inject.Binder;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Provider;

/**
 * {@link org.apache.ambari.server.upgrade.UpgradeCatalog210} unit tests.
 */
public class UpgradeCatalog210Test {
  private Injector injector;
  private Provider<EntityManager> entityManagerProvider = createStrictMock(Provider.class);
  private EntityManager entityManager = createNiceMock(EntityManager.class);
  private UpgradeCatalogHelper upgradeCatalogHelper;
  private StackEntity desiredStackEntity;

  public void initData() {
    //reset(entityManagerProvider);
    //expect(entityManagerProvider.get()).andReturn(entityManager).anyTimes();
    //replay(entityManagerProvider);
    injector = Guice.createInjector(new InMemoryDefaultTestModule());
    injector.getInstance(GuiceJpaInitializer.class);

    upgradeCatalogHelper = injector.getInstance(UpgradeCatalogHelper.class);
    // inject AmbariMetaInfo to ensure that stacks get populated in the DB
    injector.getInstance(AmbariMetaInfo.class);
    // load the stack entity
    StackDAO stackDAO = injector.getInstance(StackDAO.class);
    desiredStackEntity = stackDAO.find("HDP", "2.2.0");
  }

  public void tearDown() throws AmbariException, SQLException {
    H2DatabaseCleaner.clearDatabaseAndStopPersistenceService(injector);
  }

  @Test
  public void testExecuteDDLUpdates() throws Exception {
    final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
    Configuration configuration = createNiceMock(Configuration.class);
    Connection connection = createNiceMock(Connection.class);
    Statement statement = createNiceMock(Statement.class);
    ResultSet resultSet = createNiceMock(ResultSet.class);
    expect(configuration.getDatabaseUrl()).andReturn(Configuration.JDBC_IN_MEMORY_URL).anyTimes();
    dbAccessor.getConnection();
    expectLastCall().andReturn(connection).anyTimes();
    connection.createStatement();
    expectLastCall().andReturn(statement).anyTimes();
    statement.executeQuery(anyObject(String.class));
    expectLastCall().andReturn(resultSet).anyTimes();

    // Create DDL sections with their own capture groups
    AlertSectionDDL alertSectionDDL = new AlertSectionDDL();
    HostSectionDDL hostSectionDDL = new HostSectionDDL();
    WidgetSectionDDL widgetSectionDDL = new WidgetSectionDDL();
    ViewSectionDDL viewSectionDDL = new ViewSectionDDL();

    // Execute any DDL schema changes
    alertSectionDDL.execute(dbAccessor);
    hostSectionDDL.execute(dbAccessor);
    widgetSectionDDL.execute(dbAccessor);
    viewSectionDDL.execute(dbAccessor);

    // Replay sections
    replay(dbAccessor, configuration, resultSet, connection, statement);

    AbstractUpgradeCatalog upgradeCatalog = getUpgradeCatalog(dbAccessor);
    Class<?> c = AbstractUpgradeCatalog.class;
    Field f = c.getDeclaredField("configuration");
    f.setAccessible(true);
    f.set(upgradeCatalog, configuration);

    upgradeCatalog.executeDDLUpdates();
    verify(dbAccessor, configuration, resultSet, connection, statement);

    // Verify sections
    alertSectionDDL.verify(dbAccessor);
    hostSectionDDL.verify(dbAccessor);
    widgetSectionDDL.verify(dbAccessor);
    viewSectionDDL.verify(dbAccessor);
  }

  @Test
  public void testExecutePreDMLUpdates() throws Exception {
    Method executeStackPreDMLUpdates = UpgradeCatalog210.class.getDeclaredMethod("executeStackPreDMLUpdates");
    Method cleanupStackUpdates = UpgradeCatalog210.class.getDeclaredMethod("cleanupStackUpdates");

    final UpgradeCatalog210 upgradeCatalog210 = createMockBuilder(UpgradeCatalog210.class)
        .addMockedMethod(executeStackPreDMLUpdates)
        .addMockedMethod(cleanupStackUpdates).createMock();

    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
      @Override
      protected void configure() {
        bind(UpgradeCatalog210.class).toInstance(upgradeCatalog210);
        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
      }
    });

    upgradeCatalog210.executeStackPreDMLUpdates();
    expectLastCall().once();

    replay(upgradeCatalog210);
    mockInjector.getInstance(UpgradeCatalog210.class).executePreDMLUpdates();

    verify(upgradeCatalog210);
  }

  @Test
  public void testExecuteDMLUpdates() throws Exception {
    Method addNewConfigurationsFromXml =
      AbstractUpgradeCatalog.class.getDeclaredMethod("addNewConfigurationsFromXml");

    Method initializeClusterAndServiceWidgets =
      UpgradeCatalog210.class.getDeclaredMethod("initializeClusterAndServiceWidgets");

    Method addMissingConfigs = UpgradeCatalog210.class.getDeclaredMethod("addMissingConfigs");

    Method updateAlertDefinitions = UpgradeCatalog210.class.getDeclaredMethod("updateAlertDefinitions");

    Method removeStormRestApiServiceComponent =
      UpgradeCatalog210.class.getDeclaredMethod("removeStormRestApiServiceComponent");

    Method updateKerberosDescriptorArtifacts =
      AbstractUpgradeCatalog.class.getDeclaredMethod("updateKerberosDescriptorArtifacts");

    UpgradeCatalog210 upgradeCatalog210 = createMockBuilder(UpgradeCatalog210.class)
        .addMockedMethod(addNewConfigurationsFromXml)
        .addMockedMethod(initializeClusterAndServiceWidgets)
        .addMockedMethod(addMissingConfigs)
        .addMockedMethod(updateAlertDefinitions)
        .addMockedMethod(removeStormRestApiServiceComponent)
        .addMockedMethod(updateKerberosDescriptorArtifacts)
        .createMock();

    upgradeCatalog210.addNewConfigurationsFromXml();
    expectLastCall().once();

    upgradeCatalog210.initializeClusterAndServiceWidgets();
    expectLastCall().once();

    upgradeCatalog210.addMissingConfigs();
    expectLastCall().once();

    upgradeCatalog210.updateAlertDefinitions();
    expectLastCall().once();

    upgradeCatalog210.removeStormRestApiServiceComponent();
    expectLastCall().once();

    upgradeCatalog210.updateKerberosDescriptorArtifacts();
    expectLastCall().once();

    replay(upgradeCatalog210);

    upgradeCatalog210.executeDMLUpdates();

    verify(upgradeCatalog210);
  }

  @Test
  public void testUpdateRangerHiveConfigs() throws Exception{
    EasyMockSupport easyMockSupport = new EasyMockSupport();
    final AmbariManagementController  mockAmbariManagementController = easyMockSupport.createNiceMock(
        AmbariManagementController.class);
    final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class);

    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);

    final Config mockRangerPlugin = easyMockSupport.createNiceMock(Config.class);
    final Config mockHiveEnv = easyMockSupport.createNiceMock(Config.class);
    final Config mockHiveServer = easyMockSupport.createNiceMock(Config.class);

    final Map<String, String> propertiesExpectedRangerPlugin = new HashMap<String, String>();
    propertiesExpectedRangerPlugin.put("ranger-hive-plugin-enabled", "yes");
    final Map<String, String> propertiesExpectedHiveEnv = new HashMap<String, String>();
    final Map<String, String> propertiesExpectedHiveServer2 = new HashMap<String, String>();
    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
      @Override
      protected void configure() {
        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
        bind(ConfigHelper.class).toInstance(mockConfigHelper);
        bind(Clusters.class).toInstance(mockClusters);

        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
      }
    });

    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
      put("normal", mockClusterExpected);
    }}).once();

    expect(mockClusterExpected.getDesiredConfigByType("ranger-hive-plugin-properties")).andReturn(mockRangerPlugin).atLeastOnce();
    expect(mockClusterExpected.getDesiredConfigByType("hive-env")).andReturn(mockHiveEnv).atLeastOnce();
    expect(mockClusterExpected.getDesiredConfigByType("hiveserver2-site")).andReturn(mockHiveServer).atLeastOnce();
    expect(mockRangerPlugin.getProperties()).andReturn(propertiesExpectedRangerPlugin).anyTimes();
    expect(mockHiveEnv.getProperties()).andReturn(propertiesExpectedHiveEnv).anyTimes();
    expect(mockHiveServer.getProperties()).andReturn(propertiesExpectedHiveServer2).anyTimes();

    ServiceConfigVersionResponse r = null;
    expect(mockClusterExpected.getConfig(anyObject(String.class), anyObject(String.class))).
        andReturn(mockHiveServer).anyTimes();
    expect(mockClusterExpected.addDesiredConfig("ambari-upgrade", Collections.singleton(mockHiveServer))).
        andReturn(r).times(3);

    easyMockSupport.replayAll();
    mockInjector.getInstance(UpgradeCatalog210.class).updateRangerHiveConfigs();
    easyMockSupport.verifyAll();
  }

  @Test
  public void testUpdateHiveConfigs() throws Exception {
    EasyMockSupport easyMockSupport = new EasyMockSupport();
    final AmbariManagementController  mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
    final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class);

    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
    final ServiceConfigVersionResponse mockServiceConfigVersionResponse = easyMockSupport.createNiceMock(ServiceConfigVersionResponse.class);
    final Config mockHiveEnv = easyMockSupport.createNiceMock(Config.class);
    final Config mockHiveServerSite = easyMockSupport.createNiceMock(Config.class);
    final Config mockHiveSite = easyMockSupport.createNiceMock(Config.class);

    final Map<String, String> propertiesExpectedHiveEnv = new HashMap<String, String>() {{
      put("hive_security_authorization", "none");
    }};
    final Map<String, String> propertiesExpectedHiveSite = new HashMap<String, String>() {{
      put("hive.server2.authentication", "pam");
      put("hive.server2.custom.authentication.class", "");
    }};
    final Map<String, String> propertiesExpectedHiveServerSite = new HashMap<String, String>() {{
      put("hive.security.authorization.manager", "");
      put("hive.security.authenticator.manager", "");
    }};
    final Map<String, Service> servicesExpected = new HashMap<String, Service>();

    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
      @Override
      protected void configure() {
        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
        bind(ConfigHelper.class).toInstance(mockConfigHelper);
        bind(Clusters.class).toInstance(mockClusters);

        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
      }
    });

    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
      put("normal", mockClusterExpected);
    }}).once();

    Capture<String> configTypeEnv = new Capture<String>();
    Capture<String> configTypeSite = new Capture<String>();
    Capture<String> configTypeServerSite = new Capture<String>();

    expect(mockClusterExpected.getDesiredConfigByType("hive-env")).andReturn(mockHiveEnv).atLeastOnce();
    expect(mockClusterExpected.getDesiredConfigByType("hiveserver2-site")).andReturn(mockHiveServerSite).atLeastOnce();
    expect(mockHiveEnv.getProperties()).andReturn(propertiesExpectedHiveEnv).anyTimes();
    expect(mockHiveServerSite.getProperties()).andReturn(propertiesExpectedHiveServerSite).anyTimes();
    expect(mockClusterExpected.getConfig(capture(configTypeEnv), anyObject(String.class))).andReturn(mockHiveEnv).once();
    expect(mockClusterExpected.getConfig(capture(configTypeServerSite), anyObject(String.class))).andReturn(mockHiveServerSite).once();
    expect(mockClusterExpected.addDesiredConfig("ambari-upgrade", Collections.singleton(mockHiveEnv))).andReturn(mockServiceConfigVersionResponse).once();
    expect(mockClusterExpected.addDesiredConfig("ambari-upgrade", Collections.singleton(mockHiveServerSite))).andReturn(mockServiceConfigVersionResponse).once();

    expect(mockClusterExpected.getDesiredConfigByType("hive-site")).andReturn(mockHiveSite).atLeastOnce();
    expect(mockHiveSite.getProperties()).andReturn(propertiesExpectedHiveSite).anyTimes();
    expect(mockClusterExpected.getServices()).andReturn(servicesExpected).once();
    expect(mockClusterExpected.getConfig(capture(configTypeSite), anyObject(String.class))).andReturn(mockHiveSite).once();
    expect(mockClusterExpected.addDesiredConfig("ambari-upgrade", Collections.singleton(mockHiveSite))).andReturn(mockServiceConfigVersionResponse).once();

    easyMockSupport.replayAll();
    mockInjector.getInstance(UpgradeCatalog210.class).updateHiveConfigs();
    easyMockSupport.verifyAll();

    assertEquals("hive-env", configTypeEnv.getValue());
    assertEquals("hive-site", configTypeSite.getValue());
    assertEquals("hiveserver2-site", configTypeServerSite.getValue());
  }

  @Test
  public void TestRangerSitePropertyConversion() throws Exception{
    EasyMockSupport easyMockSupport = new EasyMockSupport();
    final String clusterName = "c1";
    final AmbariManagementController  mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
    final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class);
    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
    final Cluster cluster = easyMockSupport.createStrictMock(Cluster.class);
    final Config config = easyMockSupport.createNiceMock(Config.class);
    final Map<String,Cluster> clusters = new HashMap<String,Cluster>(){{
      put(clusterName, cluster);
    }};
    final Map<String,String> properties = new HashMap<String, String>() {{
      put("HTTPS_CLIENT_AUTH", "test123");
      put("HTTPS_KEYSTORE_FILE", "test123");
      put("HTTPS_KEYSTORE_PASS", "test123");
      put("HTTPS_KEY_ALIAS", "test123");
      put("HTTPS_SERVICE_PORT", "test123");
      put("HTTP_ENABLED", "test123");
      put("HTTP_SERVICE_PORT", "test123");
    }};

    final Map<String, String> expectedPropertyMap = new HashMap<String, String>() {{
      put("HTTPS_CLIENT_AUTH", "https.attrib.clientAuth");
      put("HTTPS_KEYSTORE_FILE", "https.attrib.keystoreFile");
      put("HTTPS_KEYSTORE_PASS", "https.attrib.keystorePass");
      put("HTTPS_KEY_ALIAS", "https.attrib.keyAlias");
      put("HTTP_SERVICE_PORT", "http.service.port");
      put("HTTPS_SERVICE_PORT", "https.service.port");
      put("HTTP_ENABLED", "http.enabled");
    }};

    final Map<String,String> convertedProperties = new HashMap<>();
    final Set<String> removedProperties = new HashSet<>();

    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
      @Override
      protected void configure() {
        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
        bind(Clusters.class).toInstance(mockClusters);
        bind(ConfigHelper.class).toInstance(mockConfigHelper);

        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
      }
    });

    UpgradeCatalog210 upgradeCatalog210 = new UpgradeCatalog210(mockInjector) {

      @Override
      protected void updateConfigurationPropertiesForCluster(Cluster cluster, String configType,
        Map<String, String> properties, boolean updateIfExists, boolean createNewConfigType) throws AmbariException {
        convertedProperties.putAll(properties);
      }

      @Override
      protected void removeConfigurationPropertiesFromCluster(Cluster cluster, String configType, Set<String> removePropertiesList)
        throws AmbariException {
        removedProperties.addAll(removePropertiesList);
      }
    };

    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).atLeastOnce();
    expect(mockClusters.getClusters()).andReturn(clusters).atLeastOnce();
    expect(config.getProperties()).andReturn(properties).atLeastOnce();
    expect(cluster.getDesiredConfigByType("ranger-site")).andReturn(config).atLeastOnce();

    replay(mockAmbariManagementController, mockClusters, cluster, config);

    upgradeCatalog210.updateRangerSiteConfigs();

    for (Map.Entry<String,String> propertyEntry: expectedPropertyMap.entrySet()){
      String oldKey = propertyEntry.getKey();
      String newKey = propertyEntry.getValue();
      assertTrue(String.format("Old property %s doesn't migrated to new name %s", oldKey, newKey), convertedProperties.containsKey(newKey));
      assertTrue(String.format("Property value %s doesn't preserved after renaming: %s",properties.get(oldKey), convertedProperties.get(newKey)),
        convertedProperties.get(newKey).equals(properties.get(oldKey)));
      assertTrue(String.format("Old property %s doesn't removed after renaming", oldKey), removedProperties.contains(oldKey));
    }
  }

  @Test
  public void testUpdateHiveConfigsWithKerberos() throws Exception {
    EasyMockSupport easyMockSupport = new EasyMockSupport();
    final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class);
    final AmbariManagementController  mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);

    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
    final Config mockHiveEnv = easyMockSupport.createNiceMock(Config.class);
    final Config mockHiveSite = easyMockSupport.createNiceMock(Config.class);
    final Config mockHiveServerSite = easyMockSupport.createNiceMock(Config.class);

    final Map<String, String> propertiesExpectedHiveEnv = new HashMap<String, String>();
    final Map<String, String> propertiesExpectedHiveSite = new HashMap<String, String>() {{
      put("hive.server2.authentication", "kerberos");
    }};
    final Map<String, String> propertiesExpectedHiveServerSite = new HashMap<String, String>() {{
    }};
    final Map<String, Service> servicesExpected = new HashMap<String, Service>(){{
      put("KERBEROS", null);
    }};

    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
      @Override
      protected void configure() {
        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
        bind(ConfigHelper.class).toInstance(mockConfigHelper);
        bind(Clusters.class).toInstance(mockClusters);
        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
      }
    });

    final UpgradeCatalog210 upgradeCatalog210 =  mockInjector.getInstance(UpgradeCatalog210.class);

    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
      put("normal", mockClusterExpected);
    }}).once();

    Capture<Map<String,String>> configCreation = Capture.newInstance(CaptureType.ALL);

    expect(mockClusterExpected.getDesiredConfigByType("hive-env")).andReturn(mockHiveEnv).atLeastOnce();
    expect(mockClusterExpected.getDesiredConfigByType("hiveserver2-site")).andReturn(mockHiveServerSite).atLeastOnce();
    expect(mockHiveEnv.getProperties()).andReturn(propertiesExpectedHiveEnv).anyTimes();
    expect(mockHiveServerSite.getProperties()).andReturn(propertiesExpectedHiveServerSite).anyTimes();

    expect(mockClusterExpected.getDesiredConfigByType("hive-site")).andReturn(mockHiveSite).atLeastOnce();
    expect(mockHiveSite.getProperties()).andReturn(propertiesExpectedHiveSite).anyTimes();
    expect(mockClusterExpected.getServices()).andReturn(servicesExpected).atLeastOnce();
    expect(mockAmbariManagementController.createConfig((Cluster)anyObject(),
      anyString(),
      capture(configCreation),
      anyString(),
      (Map<String, Map<String, String>>)anyObject())).andReturn(null).atLeastOnce();

    easyMockSupport.replayAll();
    upgradeCatalog210.updateHiveConfigs();
    easyMockSupport.verifyAll();

    Assert.assertEquals(2, configCreation.getValues().size());

    boolean hiveSecFound = false;

    for (Map<String, String> cfg: configCreation.getValues()){
      if (cfg.containsKey("hive_security_authorization")) {
        hiveSecFound = true;
        Assert.assertTrue("sqlstdauth".equalsIgnoreCase(cfg.get("hive_security_authorization")));
        break;
      }
    }

    Assert.assertTrue(hiveSecFound);
  }

  @Test
  public void testUpdateHiveConfigsWithRangerPlugin() throws Exception {
    EasyMockSupport easyMockSupport = new EasyMockSupport();
    final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class);
    final AmbariManagementController mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);

    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
    final Config mockHiveEnv = easyMockSupport.createNiceMock(Config.class);
    final Config mockHiveSite = easyMockSupport.createNiceMock(Config.class);
    final Config mockHiveServerSite = easyMockSupport.createNiceMock(Config.class);
    final Config mockHivePluginProperies = easyMockSupport.createNiceMock(Config.class);

    final Map<String, String> propertiesExpectedHiveEnv = new HashMap<String, String>() {{
      put("hive_security_authorization", "none");
    }};
    final Map<String, String> propertiesExpectedHiveSite = new HashMap<String, String>() {{
    }};

    final Map<String, String> propertiesExpectedPluginProperies = new HashMap<String, String>() {{
      put("ranger-hive-plugin-enabled", "yes");
    }};
    final Map<String, String> propertiesExpectedHiveServerSite = new HashMap<String, String>() {{
      put("hive.security.authorization.manager", "test");
      put("hive.security.authenticator.manager", "test");
    }};
    final Map<String, Service> servicesExpected = new HashMap<String, Service>() {{
      put("RANGER", null);
    }};

    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
      @Override
      protected void configure() {
        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
        bind(ConfigHelper.class).toInstance(mockConfigHelper);
        bind(Clusters.class).toInstance(mockClusters);
        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
      }
    });

    final UpgradeCatalog210 upgradeCatalog210 = mockInjector.getInstance(UpgradeCatalog210.class);

    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
      put("normal", mockClusterExpected);
    }}).once();

    Capture<Map<String, String>> configCreation = Capture.newInstance(CaptureType.ALL);

    expect(mockClusterExpected.getDesiredConfigByType("hive-env")).andReturn(mockHiveEnv).atLeastOnce();
    expect(mockClusterExpected.getDesiredConfigByType("hiveserver2-site")).andReturn(mockHiveServerSite).atLeastOnce();
    expect(mockHiveEnv.getProperties()).andReturn(propertiesExpectedHiveEnv).anyTimes();
    expect(mockHiveServerSite.getProperties()).andReturn(propertiesExpectedHiveServerSite).anyTimes();

    expect(mockClusterExpected.getDesiredConfigByType("ranger-hive-plugin-properties")).andReturn(mockHivePluginProperies).once();
    expect(mockClusterExpected.getDesiredConfigByType("hive-site")).andReturn(mockHiveSite).atLeastOnce();
    expect(mockHiveSite.getProperties()).andReturn(propertiesExpectedHiveSite).anyTimes();
    expect(mockHivePluginProperies.getProperties()).andReturn(propertiesExpectedPluginProperies).anyTimes();
    expect(mockClusterExpected.getServices()).andReturn(servicesExpected).atLeastOnce();
    expect(mockAmbariManagementController.createConfig((Cluster) anyObject(),
        anyString(),
        capture(configCreation),
        anyString(),
        (Map<String, Map<String, String>>) anyObject())).andReturn(null).atLeastOnce();

    easyMockSupport.replayAll();
    upgradeCatalog210.updateHiveConfigs();
    easyMockSupport.verifyAll();
    Assert.assertEquals(1, configCreation.getValues().size());

    boolean result = false;
    for (Map<String, String> cfg : configCreation.getValues()) {
      if (cfg.containsKey("hive.security.authorization.manager")) {
        result = true;
        break;
      }
    }
    Assert.assertFalse(result);
    result = false;
    for (Map<String, String> cfg : configCreation.getValues()) {
      if (cfg.containsKey("hive.security.authenticator.manager")) {
        result = true;
        break;
      }
    }
    Assert.assertFalse(result);
  }

  @Test
  public void TestUpdateHiveEnvContent() {
    EasyMockSupport easyMockSupport = new EasyMockSupport();
    final AmbariManagementController  mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
    final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class);
    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);

    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
      @Override
      protected void configure() {
        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
        bind(ConfigHelper.class).toInstance(mockConfigHelper);
        bind(Clusters.class).toInstance(mockClusters);

        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
      }
    });
    String content = "# Start HIVE_AUX_JARS_PATH \n" +
        "if [ \"${HIVE_AUX_JARS_PATH}\" != \"\" ]; then\n" +
        "  export HIVE_AUX_JARS_PATH=${HIVE_AUX_JARS_PATH}\n" +
        "elif [ -d \"/usr/hdp/current/hive-webhcat/share/hcatalog\" ]; then \n" +
        "  export HIVE_AUX_JARS_PATH=/usr/hdp/current/hive-webhcat/share/hcatalog\n" +
        "fi\n" +
        "#End HIVE_AUX_JARS_PATH";
    String expectedContent = "# Start HIVE_AUX_JARS_PATH \n" +
        "if [ \"${HIVE_AUX_JARS_PATH}\" != \"\" ]; then\n" +
        "  if [ -f \"${HIVE_AUX_JARS_PATH}\" ]; then    \n" +
        "    export HIVE_AUX_JARS_PATH=${HIVE_AUX_JARS_PATH}\n" +
        "  elif [ -d \"/usr/hdp/current/hive-webhcat/share/hcatalog\" ]; then\n" +
        "    export HIVE_AUX_JARS_PATH=/usr/hdp/current/hive-webhcat/share/hcatalog/hive-hcatalog-core.jar\n" +
        "  fi\n" +
        "elif [ -d \"/usr/hdp/current/hive-webhcat/share/hcatalog\" ]; then\n" +
        "  export HIVE_AUX_JARS_PATH=/usr/hdp/current/hive-webhcat/share/hcatalog/hive-hcatalog-core.jar\n" +
        "fi\n" +
        "#End HIVE_AUX_JARS_PATH";

    String modifiedContent = mockInjector.getInstance(UpgradeCatalog210.class).updateHiveEnvContent(content);
    Assert.assertEquals(modifiedContent, expectedContent);
  }

  @Test
  public void testInitializeClusterAndServiceWidgets() throws Exception {
    final AmbariManagementController controller = createStrictMock(AmbariManagementController.class);
    final Clusters clusters = createStrictMock(Clusters.class);
    final Cluster cluster = createStrictMock(Cluster.class);
    final Service service = createStrictMock(Service.class);
    final Map<String, Cluster> clusterMap = Collections.singletonMap("c1", cluster);
    final Map<String, Service> services = Collections.singletonMap("HBASE", service);

    Module module = new Module() {
      @Override
      public void configure(Binder binder) {
        binder.bind(AmbariManagementController.class).toInstance(controller);
        binder.bind(Clusters.class).toInstance(clusters);
        binder.bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
        binder.bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
      }
    };

    expect(controller.getClusters()).andReturn(clusters).anyTimes();
    expect(clusters.getClusters()).andReturn(clusterMap).anyTimes();
    controller.initializeWidgetsAndLayouts(cluster, null);
    expectLastCall().once();

    expect(cluster.getServices()).andReturn(services).once();
    controller.initializeWidgetsAndLayouts(cluster, service);
    expectLastCall().once();

    replay(controller, clusters, cluster);

    Injector injector = Guice.createInjector(module);
    injector.getInstance(UpgradeCatalog210.class).initializeClusterAndServiceWidgets();

    verify(controller, clusters, cluster);
  }

  @Test
  public void testUpdateStormConfiguration() throws Exception {
    EasyMockSupport easyMockSupport = new EasyMockSupport();
    final AmbariManagementController  mockAmbariManagementController = easyMockSupport.createNiceMock(
        AmbariManagementController.class);
    final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class);

    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);

    final Config mockClusterEnv = easyMockSupport.createNiceMock(Config.class);
    final Config mockStormSite = easyMockSupport.createNiceMock(Config.class);

    final Map<String, String> propertiesExpectedClusterEnv = new HashMap<String, String>();
    propertiesExpectedClusterEnv.put("security_enabled", "true");
    final Map<String, String> propertiesExpectedStormSite = new HashMap<String, String>();
    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
      @Override
      protected void configure() {
        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
        bind(ConfigHelper.class).toInstance(mockConfigHelper);
        bind(Clusters.class).toInstance(mockClusters);

        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
      }
    });

    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
      put("normal", mockClusterExpected);
    }}).once();

    expect(mockClusterExpected.getDesiredConfigByType("cluster-env")).andReturn(mockClusterEnv).atLeastOnce();
    expect(mockClusterExpected.getDesiredConfigByType("storm-site")).andReturn(mockStormSite).atLeastOnce();
    expect(mockClusterEnv.getProperties()).andReturn(propertiesExpectedClusterEnv).anyTimes();
    expect(mockStormSite.getProperties()).andReturn(propertiesExpectedStormSite).anyTimes();

    easyMockSupport.replayAll();
    mockInjector.getInstance(UpgradeCatalog210.class).updateStormConfigs();
    easyMockSupport.verifyAll();
  }

  @Test
  public void testUpdateHBaseConfiguration() throws Exception {
    EasyMockSupport easyMockSupport = new EasyMockSupport();
    final AmbariManagementController  mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
    final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class);

    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);
    final Host mockHost = easyMockSupport.createNiceMock(Host.class);

    final Config mockHBaseSite = easyMockSupport.createNiceMock(Config.class);
    final Config mockHBaseEnv = easyMockSupport.createNiceMock(Config.class);

    final Map<String, String> propertiesExpectedHBaseSite = new HashMap<String, String>();
    propertiesExpectedHBaseSite.put("hbase.region.server.rpc.scheduler.factory.class",
        "org.apache.phoenix.hbase.index.ipc.PhoenixIndexRpcSchedulerFactory");
    propertiesExpectedHBaseSite.put("hbase.security.authorization", "true");

    final Map<String, String> propertiesExpectedHBaseEnv = new HashMap<String, String>();
    propertiesExpectedHBaseEnv.put("phoenix_sql_enabled", "false");

    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
      @Override
      protected void configure() {
        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
        bind(ConfigHelper.class).toInstance(mockConfigHelper);
        bind(Clusters.class).toInstance(mockClusters);

        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
      }
    });

    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
      put("normal", mockClusterExpected);
    }}).once();

    expect(mockClusterExpected.getDesiredConfigByType("hbase-site")).andReturn(mockHBaseSite).atLeastOnce();
    expect(mockClusterExpected.getDesiredConfigByType("hbase-env")).andReturn(mockHBaseEnv).atLeastOnce();
    expect(mockHBaseSite.getProperties()).andReturn(propertiesExpectedHBaseSite).anyTimes();
    expect(mockHBaseEnv.getProperties()).andReturn(propertiesExpectedHBaseEnv).anyTimes();

    Capture<String> configType = new Capture<String>();
    Capture<String> configTag = new Capture<String>();
    expect(mockClusterExpected.getConfig(capture(configType), capture(configTag))).
            andReturn(mockHBaseSite).atLeastOnce();

    easyMockSupport.replayAll();
    mockInjector.getInstance(UpgradeCatalog210.class).updateHBaseConfigs();
    easyMockSupport.verifyAll();
  }

  @Test
  public void testDeleteStormRestApiServiceComponent() throws Exception {
    initData();
    ClusterEntity clusterEntity = upgradeCatalogHelper.createCluster(injector,
      "c1", desiredStackEntity);
    ClusterServiceEntity clusterServiceEntity = upgradeCatalogHelper.createService(
        injector, clusterEntity, "STORM");
    HostEntity hostEntity = upgradeCatalogHelper.createHost(injector,
        clusterEntity, "h1");

    // Set current stack version
    ClusterDAO clusterDAO = injector.getInstance(ClusterDAO.class);
    ClusterStateDAO clusterStateDAO = injector.getInstance(ClusterStateDAO.class);
    ClusterStateEntity clusterStateEntity = new ClusterStateEntity();
    clusterStateEntity.setClusterId(clusterEntity.getClusterId());
    clusterStateEntity.setClusterEntity(clusterEntity);
    clusterStateEntity.setCurrentStack(desiredStackEntity);
    clusterStateDAO.create(clusterStateEntity);
    clusterEntity.setClusterStateEntity(clusterStateEntity);
    clusterDAO.merge(clusterEntity);

    ServiceComponentDesiredStateEntity componentDesiredStateEntity = new ServiceComponentDesiredStateEntity();
    componentDesiredStateEntity.setClusterId(clusterEntity.getClusterId());
    componentDesiredStateEntity.setServiceName(clusterServiceEntity.getServiceName());
    componentDesiredStateEntity.setClusterServiceEntity(clusterServiceEntity);
    componentDesiredStateEntity.setComponentName("STORM_REST_API");
    componentDesiredStateEntity.setDesiredStack(desiredStackEntity);

    ServiceComponentDesiredStateDAO componentDesiredStateDAO =
      injector.getInstance(ServiceComponentDesiredStateDAO.class);

    componentDesiredStateDAO.create(componentDesiredStateEntity);

    HostComponentDesiredStateDAO hostComponentDesiredStateDAO =
      injector.getInstance(HostComponentDesiredStateDAO.class);

    HostComponentDesiredStateEntity hostComponentDesiredStateEntity = new HostComponentDesiredStateEntity();

    hostComponentDesiredStateEntity.setClusterId(clusterEntity.getClusterId());
    hostComponentDesiredStateEntity.setComponentName("STORM_REST_API");
    hostComponentDesiredStateEntity.setAdminState(HostComponentAdminState.INSERVICE);
    hostComponentDesiredStateEntity.setServiceName(clusterServiceEntity.getServiceName());
    hostComponentDesiredStateEntity.setServiceComponentDesiredStateEntity(componentDesiredStateEntity);
    hostComponentDesiredStateEntity.setHostEntity(hostEntity);
    hostComponentDesiredStateEntity.setDesiredStack(desiredStackEntity);

    hostComponentDesiredStateDAO.create(hostComponentDesiredStateEntity);

    HostComponentDesiredStateEntity entity = hostComponentDesiredStateDAO.findAll().get(0);

    Assert.assertEquals(HostComponentAdminState.INSERVICE.name(), entity.getAdminState().name());

    // ensure the desired state exists
    Assert.assertNotNull(componentDesiredStateDAO.findByName(clusterEntity.getClusterId(), "STORM",
        "STORM_REST_API"));

    UpgradeCatalog210 upgradeCatalog210 = injector.getInstance(UpgradeCatalog210.class);
    upgradeCatalog210.removeStormRestApiServiceComponent();

    Assert.assertNull(componentDesiredStateDAO.findByName(clusterEntity.getClusterId(), "STORM",
        "STORM_REST_API"));
    tearDown();
  }

  @Test
  public void testUpdateHDFSConfiguration() throws Exception {
    EasyMockSupport easyMockSupport = new EasyMockSupport();
    final AmbariManagementController  mockAmbariManagementController = easyMockSupport.createNiceMock(AmbariManagementController.class);
    final ConfigHelper mockConfigHelper = easyMockSupport.createMock(ConfigHelper.class);

    final Clusters mockClusters = easyMockSupport.createStrictMock(Clusters.class);
    final Cluster mockClusterExpected = easyMockSupport.createNiceMock(Cluster.class);

    final Config mockHdfsSite = easyMockSupport.createNiceMock(Config.class);
    final Config mockCoreSite = easyMockSupport.createStrictMock(Config.class);

    final Map<String, String> propertiesExpectedHdfs = new HashMap<String, String>();
    final Map<String, String> propertiesExpectedCoreSite = new HashMap<String, String>();
    propertiesExpectedHdfs.put("dfs.nameservices", "nncl1,nncl2");
    propertiesExpectedHdfs.put("dfs.ha.namenodes.nncl2", "nn1,nn2");
    propertiesExpectedCoreSite.put("fs.defaultFS", "hdfs://EXAMPLE.COM:8020");
    final Injector mockInjector = Guice.createInjector(new AbstractModule() {
      @Override
      protected void configure() {
        bind(AmbariManagementController.class).toInstance(mockAmbariManagementController);
        bind(ConfigHelper.class).toInstance(mockConfigHelper);
        bind(Clusters.class).toInstance(mockClusters);

        bind(DBAccessor.class).toInstance(createNiceMock(DBAccessor.class));
        bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
      }
    });

    expect(mockAmbariManagementController.getClusters()).andReturn(mockClusters).once();
    expect(mockClusters.getClusters()).andReturn(new HashMap<String, Cluster>() {{
      put("normal", mockClusterExpected);
    }}).once();

    // Expected operation
    expect(mockClusterExpected.getDesiredConfigByType("hadoop-env")).andReturn(null).once();

    // Expected operation
    expect(mockClusterExpected.getDesiredConfigByType("hdfs-site")).andReturn(mockHdfsSite).atLeastOnce();
    expect(mockClusterExpected.getHosts("HDFS", "NAMENODE")).andReturn( new HashSet<String>() {{
      add("host1");
    }}).atLeastOnce();
    expect(mockHdfsSite.getProperties()).andReturn(propertiesExpectedHdfs).anyTimes();

    expect(mockClusterExpected.getDesiredConfigByType("core-site")).andReturn(mockCoreSite).anyTimes();
    expect(mockCoreSite.getProperties()).andReturn(propertiesExpectedCoreSite).anyTimes();

    easyMockSupport.replayAll();
    mockInjector.getInstance(UpgradeCatalog210.class).updateHdfsConfigs();
    easyMockSupport.verifyAll();
  }

  /**
   * @param dbAccessor
   * @return
   */
  private AbstractUpgradeCatalog getUpgradeCatalog(final DBAccessor dbAccessor) {
    Module module = new Module() {
      @Override
      public void configure(Binder binder) {
        binder.bind(DBAccessor.class).toInstance(dbAccessor);
        binder.bind(EntityManager.class).toInstance(entityManager);
        binder.bind(OsFamily.class).toInstance(createNiceMock(OsFamily.class));
      }
    };

    Injector injector = Guice.createInjector(module);
    return injector.getInstance(UpgradeCatalog210.class);
  }

  @Test
  public void testGetSourceVersion() {
    final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
    UpgradeCatalog upgradeCatalog = getUpgradeCatalog(dbAccessor);
    Assert.assertEquals("2.0.0", upgradeCatalog.getSourceVersion());
  }

  @Test
  public void testGetTargetVersion() throws Exception {
    final DBAccessor dbAccessor = createNiceMock(DBAccessor.class);
    UpgradeCatalog upgradeCatalog = getUpgradeCatalog(dbAccessor);

    Assert.assertEquals("2.1.0", upgradeCatalog.getTargetVersion());
  }

  @Test
  public void testUpdateKerberosDescriptorArtifact_Simple() throws Exception {
    final KerberosDescriptorFactory kerberosDescriptorFactory = new KerberosDescriptorFactory();

    KerberosServiceDescriptor serviceDescriptor;

    URL systemResourceURL = ClassLoader.getSystemResource("kerberos/test_kerberos_descriptor_simple.json");
    assertNotNull(systemResourceURL);

    final KerberosDescriptor kerberosDescriptorOrig = kerberosDescriptorFactory.createInstance(new File(systemResourceURL.getFile()));
    assertNotNull(kerberosDescriptorOrig);
    assertNotNull(kerberosDescriptorOrig.getIdentity("hdfs"));

    serviceDescriptor = kerberosDescriptorOrig.getService("HDFS");
    assertNotNull(serviceDescriptor);
    assertNotNull(serviceDescriptor.getIdentity("/hdfs"));
    assertNull(serviceDescriptor.getIdentity("hdfs"));

    serviceDescriptor = kerberosDescriptorOrig.getService("OOZIE");
    assertNotNull(serviceDescriptor);
    assertNotNull(serviceDescriptor.getIdentity("/hdfs"));
    assertNull(serviceDescriptor.getIdentity("/HDFS/hdfs"));

    UpgradeCatalog210 upgradeMock = createMockBuilder(UpgradeCatalog210.class).createMock();

    Capture<Map<String, Object>> updatedData = new Capture<Map<String, Object>>();

    ArtifactEntity artifactEntity = createNiceMock(ArtifactEntity.class);
    expect(artifactEntity.getArtifactData())
        .andReturn(kerberosDescriptorOrig.toMap())
        .once();

    artifactEntity.setArtifactData(capture(updatedData));
    expectLastCall().once();

    replay(artifactEntity, upgradeMock);
    upgradeMock.updateKerberosDescriptorArtifact(createNiceMock(ArtifactDAO.class), artifactEntity);
    verify(artifactEntity, upgradeMock);

    KerberosDescriptor kerberosDescriptorUpdated = new KerberosDescriptorFactory().createInstance(updatedData.getValue());
    assertNotNull(kerberosDescriptorUpdated);
    assertNull(kerberosDescriptorUpdated.getIdentity("/hdfs"));

    serviceDescriptor = kerberosDescriptorUpdated.getService("HDFS");
    assertNotNull(serviceDescriptor);
    assertNull(serviceDescriptor.getIdentity("/hdfs"));
    assertNotNull(serviceDescriptor.getIdentity("hdfs"));

    serviceDescriptor = kerberosDescriptorUpdated.getService("OOZIE");
    assertNotNull(serviceDescriptor);
    assertNull(serviceDescriptor.getIdentity("/hdfs"));
    assertNotNull(serviceDescriptor.getIdentity("/HDFS/hdfs"));
  }

  @Test
  public void testUpdateKerberosDescriptorArtifact_NoHDFSService() throws Exception {
    final KerberosDescriptorFactory kerberosDescriptorFactory = new KerberosDescriptorFactory();

    KerberosServiceDescriptor serviceDescriptor;

    URL systemResourceURL = ClassLoader.getSystemResource("kerberos/test_kerberos_descriptor_no_hdfs.json");
    assertNotNull(systemResourceURL);

    final KerberosDescriptor kerberosDescriptorOrig = kerberosDescriptorFactory.createInstance(new File(systemResourceURL.getFile()));
    assertNotNull(kerberosDescriptorOrig);
    assertNotNull(kerberosDescriptorOrig.getIdentity("hdfs"));

    serviceDescriptor = kerberosDescriptorOrig.getService("HDFS");
    assertNull(serviceDescriptor);

    serviceDescriptor = kerberosDescriptorOrig.getService("OOZIE");
    assertNotNull(serviceDescriptor);
    assertNotNull(serviceDescriptor.getIdentity("/hdfs"));
    assertNull(serviceDescriptor.getIdentity("/HDFS/hdfs"));

    UpgradeCatalog210 upgradeMock = createMockBuilder(UpgradeCatalog210.class).createMock();

    Capture<Map<String, Object>> updatedData = new Capture<Map<String, Object>>();

    ArtifactEntity artifactEntity = createNiceMock(ArtifactEntity.class);
    expect(artifactEntity.getArtifactData())
        .andReturn(kerberosDescriptorOrig.toMap())
        .once();

    artifactEntity.setArtifactData(capture(updatedData));
    expectLastCall().once();

    replay(artifactEntity, upgradeMock);
    upgradeMock.updateKerberosDescriptorArtifact(createNiceMock(ArtifactDAO.class), artifactEntity);
    verify(artifactEntity, upgradeMock);

    KerberosDescriptor kerberosDescriptorUpdated = new KerberosDescriptorFactory().createInstance(updatedData.getValue());
    assertNotNull(kerberosDescriptorUpdated);
    assertNull(kerberosDescriptorUpdated.getIdentity("/hdfs"));

    serviceDescriptor = kerberosDescriptorUpdated.getService("HDFS");
    assertNotNull(serviceDescriptor);
    assertNull(serviceDescriptor.getIdentity("/hdfs"));
    assertNotNull(serviceDescriptor.getIdentity("hdfs"));

    serviceDescriptor = kerberosDescriptorUpdated.getService("OOZIE");
    assertNotNull(serviceDescriptor);
    assertNull(serviceDescriptor.getIdentity("/hdfs"));
    assertNotNull(serviceDescriptor.getIdentity("/HDFS/hdfs"));
  }

  // *********** Inner Classes that represent sections of the DDL ***********
  // ************************************************************************

  /**
   * Verify that all of the host-related tables added a column for the host_id
   */
  class HostSectionDDL implements SectionDDL {

    HashMap<String, Capture<DBColumnInfo>> captures;

    public HostSectionDDL() {
      // Capture all tables that will have the host_id column added to it.
      captures = new HashMap<String, Capture<DBColumnInfo>>();

      // Column Capture section
      // Hosts
      Capture<DBAccessor.DBColumnInfo> clusterHostMappingColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
      Capture<DBAccessor.DBColumnInfo> configGroupHostMappingColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
      Capture<DBAccessor.DBColumnInfo> hostConfigMappingColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
      Capture<DBAccessor.DBColumnInfo> hostsColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
      Capture<DBAccessor.DBColumnInfo> hostComponentStateColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
      Capture<DBAccessor.DBColumnInfo> hostComponentDesiredStateColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
      Capture<DBAccessor.DBColumnInfo> hostRoleCommandColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
      Capture<DBAccessor.DBColumnInfo> hostStateColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
      Capture<DBAccessor.DBColumnInfo> hostVersionColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
      Capture<DBAccessor.DBColumnInfo> kerberosPrincipalHostColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
      Capture<DBAccessor.DBColumnInfo> requestOperationLevelColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
      Capture<DBAccessor.DBColumnInfo> serviceConfigHostsColumnCapture = new Capture<DBAccessor.DBColumnInfo>();

      captures.put("ClusterHostMapping", clusterHostMappingColumnCapture);
      captures.put("configgrouphostmapping", configGroupHostMappingColumnCapture);
      captures.put("hostconfigmapping", hostConfigMappingColumnCapture);
      captures.put("hosts", hostsColumnCapture);
      captures.put("hostcomponentstate", hostComponentStateColumnCapture);
      captures.put("hostcomponentdesiredstate", hostComponentDesiredStateColumnCapture);
      captures.put("host_role_command", hostRoleCommandColumnCapture);
      captures.put("hoststate", hostStateColumnCapture);
      captures.put("host_version", hostVersionColumnCapture);
      captures.put("kerberos_principal_host", kerberosPrincipalHostColumnCapture);
      captures.put("requestoperationlevel", requestOperationLevelColumnCapture);
      captures.put("serviceconfighosts", serviceConfigHostsColumnCapture);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void execute(DBAccessor dbAccessor) throws SQLException {
      // Add columns and alter table section
      dbAccessor.addColumn(eq("ClusterHostMapping"), capture(captures.get("ClusterHostMapping")));
      dbAccessor.addColumn(eq("configgrouphostmapping"), capture(captures.get("configgrouphostmapping")));
      dbAccessor.addColumn(eq("hostconfigmapping"), capture(captures.get("hostconfigmapping")));
      dbAccessor.addColumn(eq("hosts"), capture(captures.get("hosts")));
      dbAccessor.addColumn(eq("hostcomponentstate"), capture(captures.get("hostcomponentstate")));
      dbAccessor.addColumn(eq("hostcomponentdesiredstate"), capture(captures.get("hostcomponentdesiredstate")));
      dbAccessor.addColumn(eq("host_role_command"), capture(captures.get("host_role_command")));
      dbAccessor.addColumn(eq("hoststate"), capture(captures.get("hoststate")));
      dbAccessor.addColumn(eq("host_version"), capture(captures.get("host_version")));
      dbAccessor.addColumn(eq("kerberos_principal_host"), capture(captures.get("kerberos_principal_host")));
      dbAccessor.addColumn(eq("requestoperationlevel"), capture(captures.get("requestoperationlevel")));
      dbAccessor.addColumn(eq("serviceconfighosts"), capture(captures.get("serviceconfighosts")));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void verify(DBAccessor dbAccessor) throws SQLException {
      // Verification section
      for (Capture<DBColumnInfo> columnCapture : captures.values()) {
        verifyContainsHostIdColumn(columnCapture);
      }
    }

    /**
     * Verify that the column capture of the table contains a host_id column of type Long.
     * This is needed for all of the host-related tables that are switching from the
     * host_name to the host_id.
     * @param columnCapture
     */
    private void verifyContainsHostIdColumn(Capture<DBAccessor.DBColumnInfo> columnCapture) {
      DBColumnInfo idColumn = columnCapture.getValue();
      Assert.assertEquals(Long.class, idColumn.getType());
      Assert.assertEquals("host_id", idColumn.getName());
    }
  }

  /**
   * Verify that the widget, widget_layout, and widget_layout_user_widget tables are created correctly.
   */
  class WidgetSectionDDL implements SectionDDL {

    HashMap<String, Capture<List<DBColumnInfo>>> captures;
    Capture<DBColumnInfo> userActiveLayoutsColumnCapture;

    public WidgetSectionDDL() {
      captures = new HashMap<String, Capture<List<DBColumnInfo>>>();

      Capture<List<DBColumnInfo>> userWidgetColumnsCapture = new Capture<List<DBColumnInfo>>();
      Capture<List<DBColumnInfo>> widgetLayoutColumnsCapture = new Capture<List<DBColumnInfo>>();
      Capture<List<DBColumnInfo>> widgetLayoutUserWidgetColumnsCapture = new Capture<List<DBColumnInfo>>();

      captures.put("widget", userWidgetColumnsCapture);
      captures.put("widget_layout", widgetLayoutColumnsCapture);
      captures.put("widget_layout_user_widget", widgetLayoutUserWidgetColumnsCapture);
      userActiveLayoutsColumnCapture = new Capture<DBColumnInfo>();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void execute(DBAccessor dbAccessor) throws SQLException {
      Capture<List<DBColumnInfo>> userWidgetColumnsCapture = captures.get("widget");
      Capture<List<DBColumnInfo>> widgetLayoutColumnsCapture = captures.get("widget_layout");
      Capture<List<DBColumnInfo>> widgetLayoutUserWidgetColumnsCapture = captures.get("widget_layout_user_widget");

      // User Widget
      dbAccessor.createTable(eq("widget"),
          capture(userWidgetColumnsCapture), eq("id"));

      // Widget Layout
      dbAccessor.createTable(eq("widget_layout"),
          capture(widgetLayoutColumnsCapture), eq("id"));

      // Widget Layout User Widget
      dbAccessor.createTable(eq("widget_layout_user_widget"),
          capture(widgetLayoutUserWidgetColumnsCapture), eq("widget_layout_id"), eq("widget_id"));

      dbAccessor.addColumn(eq("users"), capture(userActiveLayoutsColumnCapture));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void verify(DBAccessor dbAccessor) throws SQLException {
      Capture<List<DBColumnInfo>> widgetColumnsCapture = captures.get("widget");
      Capture<List<DBColumnInfo>> widgetLayoutColumnsCapture = captures.get("widget_layout");
      Capture<List<DBColumnInfo>> widgetLayoutUserWidgetColumnsCapture = captures.get("widget_layout_user_widget");

      // Verify widget tables
      assertEquals(12, widgetColumnsCapture.getValue().size());
      assertEquals(7, widgetLayoutColumnsCapture.getValue().size());
      assertEquals(3, widgetLayoutUserWidgetColumnsCapture.getValue().size());

      DBColumnInfo idColumn = userActiveLayoutsColumnCapture.getValue();
      Assert.assertEquals(String.class, idColumn.getType());
      Assert.assertEquals("active_widget_layouts", idColumn.getName());
    }
  }

  /**
   * Verify view changes
   */
  class ViewSectionDDL implements SectionDDL {

    HashMap<String, Capture<DBColumnInfo>> captures;

    public ViewSectionDDL() {
      captures = new HashMap<String, Capture<DBColumnInfo>>();

      Capture<DBAccessor.DBColumnInfo> viewInstanceColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
      Capture<DBAccessor.DBColumnInfo> viewInstanceAlterNamesColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
      Capture<DBAccessor.DBColumnInfo> viewParamColumnCapture = new Capture<DBAccessor.DBColumnInfo>();
      Capture<DBAccessor.DBColumnInfo> viewBuildColumnCapture = new Capture<DBAccessor.DBColumnInfo>();

      captures.put("viewinstance", viewInstanceColumnCapture);
      captures.put("viewinstance_alter_names", viewInstanceAlterNamesColumnCapture);
      captures.put("viewparameter", viewParamColumnCapture);
      captures.put("viewmain", viewBuildColumnCapture);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void execute(DBAccessor dbAccessor) throws SQLException {
      Capture<DBColumnInfo> viewInstanceColumnCapture = captures.get("viewinstance");
      Capture<DBColumnInfo> viewInstanceAlterNamesColumnCapture = captures.get("viewinstance_alter_names");
      Capture<DBColumnInfo> viewParamColumnCapture = captures.get("viewparameter");
      Capture<DBColumnInfo> viewBuildColumnCapture = captures.get("viewmain");

      dbAccessor.addColumn(eq("viewinstance"), capture(viewInstanceColumnCapture));
      dbAccessor.addColumn(eq("viewinstance"), capture(viewInstanceAlterNamesColumnCapture));
      dbAccessor.addColumn(eq("viewparameter"), capture(viewParamColumnCapture));
      dbAccessor.addColumn(eq("viewmain"), capture(viewBuildColumnCapture));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void verify(DBAccessor dbAccessor) throws SQLException {
      verifyViewInstance(captures.get("viewinstance"));
      verifyViewInstanceAlterNames(captures.get("viewinstance_alter_names"));
      verifyViewParameter(captures.get("viewparameter"));
      verifyViewBuild(captures.get("viewmain"));
    }

    private void verifyViewInstance(Capture<DBAccessor.DBColumnInfo> viewInstanceColumnCapture) {
      DBColumnInfo clusterIdColumn = viewInstanceColumnCapture.getValue();
      Assert.assertEquals(String.class, clusterIdColumn.getType());
      Assert.assertEquals("cluster_handle", clusterIdColumn.getName());
    }

    private void verifyViewInstanceAlterNames(Capture<DBAccessor.DBColumnInfo> viewInstanceAlterNamesColumnCapture) {
      DBColumnInfo clusterIdColumn = viewInstanceAlterNamesColumnCapture.getValue();
      Assert.assertEquals(Integer.class, clusterIdColumn.getType());
      Assert.assertEquals("alter_names", clusterIdColumn.getName());
    }

    private void verifyViewParameter(Capture<DBAccessor.DBColumnInfo> viewParamColumnCapture) {
      DBColumnInfo clusterConfigColumn = viewParamColumnCapture.getValue();
      Assert.assertEquals(String.class, clusterConfigColumn.getType());
      Assert.assertEquals("cluster_config", clusterConfigColumn.getName());
    }

    private void verifyViewBuild(Capture<DBAccessor.DBColumnInfo> viewBuildColumnCapture) {
      DBColumnInfo clusterConfigColumn = viewBuildColumnCapture.getValue();
      Assert.assertEquals(String.class, clusterConfigColumn.getType());
      Assert.assertEquals("build", clusterConfigColumn.getName());
    }
  }

  /**
   * Verify alert changes
   */
  class AlertSectionDDL implements SectionDDL {
    HashMap<String, Capture<String>> stringCaptures;
    HashMap<String, Capture<Class>> classCaptures;

    public AlertSectionDDL() {
      stringCaptures = new HashMap<String, Capture<String>>();
      classCaptures = new HashMap<String, Capture<Class>>();

      Capture<String> textCaptureC = new Capture<String>();
      Capture<String> textCaptureH = new Capture<String>();
      Capture<Class>  classFromC = new Capture<Class>();
      Capture<Class>  classFromH = new Capture<Class>();
      Capture<Class>  classToC = new Capture<Class>();
      Capture<Class>  classToH = new Capture<Class>();

      stringCaptures.put("textCaptureC", textCaptureC);
      stringCaptures.put("textCaptureH", textCaptureH);
      classCaptures.put("classFromC", classFromC);
      classCaptures.put("classFromH", classFromH);
      classCaptures.put("classToC", classToC);
      classCaptures.put("classToH", classToH);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void execute(DBAccessor dbAccessor) throws SQLException {
      Capture<String> textCaptureC = stringCaptures.get("textCaptureC");
      Capture<String> textCaptureH = stringCaptures.get("textCaptureH");
      Capture<Class>  classFromC = classCaptures.get("classFromC");
      Capture<Class>  classFromH = classCaptures.get("classFromH");
      Capture<Class>  classToC = classCaptures.get("classToC");
      Capture<Class>  classToH = classCaptures.get("classToH");

      dbAccessor.changeColumnType(eq("alert_current"), capture(textCaptureC), capture(classFromC), capture(classToC));
      dbAccessor.changeColumnType(eq("alert_history"), capture(textCaptureH), capture(classFromH), capture(classToH));
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void verify(DBAccessor dbAccessor) throws SQLException {
      Capture<String> textCaptureC = stringCaptures.get("textCaptureC");
      Capture<String> textCaptureH = stringCaptures.get("textCaptureH");
      Capture<Class>  classFromC = classCaptures.get("classFromC");
      Capture<Class>  classFromH = classCaptures.get("classFromH");
      Capture<Class>  classToC = classCaptures.get("classToC");
      Capture<Class>  classToH = classCaptures.get("classToH");

      Assert.assertEquals("latest_text", textCaptureC.getValue());
      Assert.assertEquals(String.class, classFromC.getValue());
      Assert.assertEquals(char[].class, classToC.getValue());

      Assert.assertEquals("alert_text", textCaptureH.getValue());
      Assert.assertEquals(String.class, classFromH.getValue());
      Assert.assertEquals(char[].class, classToH.getValue());
    }
  }
}


text:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jme3.gde.gui.view;

import com.jme3.gde.gui.nodes.GElementNode;
import com.jme3.gde.gui.nodes.GUINode;
import jada.ngeditor.controller.CommandProcessor;
import jada.ngeditor.controller.GUIEditor;
import jada.ngeditor.controller.commands.SelectCommand;
import jada.ngeditor.listeners.events.SelectionChanged;
import jada.ngeditor.model.GUI;
import jada.ngeditor.model.GuiEditorModel;
import jada.ngeditor.model.elements.GElement;
import java.awt.BorderLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyVetoException;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JComponent;
import org.netbeans.spi.navigator.NavigatorPanel;
import org.openide.explorer.ExplorerManager;
import org.openide.explorer.ExplorerUtils;
import org.openide.explorer.view.BeanTreeView;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Node;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
import org.openide.util.lookup.ProxyLookup;

/**
 *
 * @author cris
 */
@NavigatorPanel.Registration(mimeType = "text/x-niftygui+xml", displayName="Gui View")
public class Navigator extends javax.swing.JPanel implements NavigatorPanel,ExplorerManager.Provider , Observer, PropertyChangeListener{
    private Lookup lookup;
    private  ExplorerManager mgr = new ExplorerManager();
    private final BeanTreeView beanTreeView;
    /**
     * Creates new form Navigator
     */
    public Navigator() {
        initComponents();
        setLayout(new BorderLayout());
        beanTreeView = new BeanTreeView();
        add(beanTreeView, BorderLayout.CENTER);
        final GuiEditorModel model = (GuiEditorModel) CommandProcessor.getInstance().getObservable();
        model.addObserver(this);
        if(model.getCurrent() != null){
            try {
                this.intNavigator(model.getCurrent());
            } catch (PropertyVetoException ex) {
                Exceptions.printStackTrace(ex);
            }
        }

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 244, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 274, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

    @Override
    public String getDisplayName() {
       return "GuiView";
    }

    @Override
    public String getDisplayHint() {
       return "Easy view for your gui";
    }

    @Override
    public JComponent getComponent() {
       return this;
    }

    @Override
    public void panelActivated(Lookup context) {

    }

    @Override
    public void panelDeactivated() {
        ExplorerUtils.activateActions(mgr, false);
    }

    @Override
    public Lookup getLookup() {
       return lookup;
    }

    @Override
    public ExplorerManager getExplorerManager() {
        return mgr;
    }

    @Override
    public void update(Observable o, Object arg) {
       if(o instanceof GuiEditorModel){
           try {
               GuiEditorModel model = (GuiEditorModel) o;
               model.getCurrent().addObserver(this);
               model.getCurrent().getSelection().addObserver(this);
               this.intNavigator(model.getCurrent());
           } catch (PropertyVetoException ex) {
               Exceptions.printStackTrace(ex);
           }
       }

       if(arg instanceof SelectionChanged){
           SelectionChanged event = (SelectionChanged) arg;
           if(event.getNewSelection().isEmpty()){
               return;
           }
           ArrayList<String> path = new ArrayList<String>();
           GElement parent = ((SelectionChanged)arg).getElement();
           while(parent!=null){
               path.add(parent.getID());
               parent = parent.getParent();
           }
           Node result = mgr.getRootContext();
           for(int i=path.size()-1;i>=0;i--){
               if(result!=null){
                    result = result.getChildren().findChild(path.get(i));
               }
           }
            try {
                if(result!=null){
                    mgr.setSelectedNodes(new Node[]{result});
                }

            } catch (PropertyVetoException ex) {
                Exceptions.printStackTrace(ex);
            }
       }
        beanTreeView.updateUI();
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals(ExplorerManager.PROP_SELECTED_NODES)) {
            Node[] newValue = (Node[]) evt.getNewValue();
            if (newValue.length > 0) {
                AbstractNode firstSelected = (AbstractNode) newValue[0];
                if (firstSelected instanceof GElementNode) {
                    GElement element = ((GElementNode) firstSelected).getGelement();
                    GUI gui = ((GUINode)mgr.getRootContext()).getGui();
                    gui.getSelection().deleteObserver(this); // I don't wont to get notified about this selection change
                    SelectCommand command = CommandProcessor.getInstance().getCommand(SelectCommand.class);
                    command.setElement(element);
                    try {
                        CommandProcessor.getInstance().excuteCommand(command);
                    } catch (Exception ex) {
                        Exceptions.printStackTrace(ex);
                    }
                     gui.getSelection().addObserver(this);

                }
            }
        }
    }

    private void intNavigator(GUI gui) throws PropertyVetoException {

        ExplorerUtils.activateActions(mgr, true);

        AbstractNode guiRoot = new GUINode(gui);
        guiRoot.setName("Gui");
        this.mgr.setRootContext(guiRoot);
        this.beanTreeView.updateUI();
        mgr.addPropertyChangeListener(this);
        this.mgr.setSelectedNodes(new Node[]{guiRoot});
        Lookup lookup1 = ExplorerUtils.createLookup(mgr, getActionMap());
        lookup = new ProxyLookup(lookup1);

    }
}

redpajama_set_name: RedPajamaGithub


text:
package ru.job4j;

/**
 * Класс для вычисление арифметических операций.
 * @author daitzhanov
 * @since 09.01.2017
 * @version 1.0
 */
public class Calculator {
	/**
	 *Result.
	 */
	private double result;
	/**
	 * Add.
	 * @param first - first
	 * @param second - second
	 */
	public void add(double first, double second) {
		result = first + second;
	}
	/**
	 * Subtruct.
	 * @param first - first
	 * @param second - second
	 */
	public void subtruct(double first, double second) {
		result = first - second;
	}
	/**
	 * Multiple.
	 * @param first - first
	 * @param second - second
	 */
	public void multiple(double first, double second) {
		result = first * second;
	}
	/**
	 * Div.
	 * @param first - first
	 * @param second - second
	 */
	public void div(double first, double second) {
		result = first / second;
	}
	/**
	 * GetResult.
	 * @return result - result
	 */
	public double getResult() {
		return result;
	}
}


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MvcTest
{
  public class RouteConfig
  {
    public static void RegisterRoutes(RouteCollection routes)
    {
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

      routes.MapRoute(
        name: "MyRoute",
        url: "Blog/{day}/{month}/{year}",
        defaults: new
        {
          controller = "Route",
          action = "Test",
          day = DateTime.Now.Day,
          month = DateTime.Now.Month,
          year = DateTime.Now.Year
        }
      );

      routes.MapRoute(
          name: "Default",
          url: "{controller}/{action}/{id}",
          defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
      );
    }
  }
}


text:
from django.db.models.base import ModelBase

class CheckConstraintMetaClass(ModelBase):
    def __new__(cls, name, bases, attrs):
        model = super(CheckConstraintMetaClass, cls).__new__(cls, name, bases, attrs)
        for (constraint_name, constraint_obj) in model._meta.constraints:
            constraint_obj.validate(model._meta)
        return model


text:
#include <gtest/gtest.h>
#include <arrayfire.h>
#include <af/dim4.hpp>
#include <af/traits.hpp>
#include <af/array.h>
#include <vector>
#include <iostream>
#include <string>
#include <testHelpers.hpp>
#include <af/device.h>

using std::vector;
using std::string;
using std::cout;
using std::endl;
using af::af_cfloat;
using af::af_cdouble;

typedef af_err (*scanFunc)(af_array *, const af_array, const int);

template<typename Ti, typename To, scanFunc af_scan>
void scanTest(string pTestFile, int off = 0, bool isSubRef=false, const vector<af_seq> seqv=vector<af_seq>())
{
    vector<af::dim4> numDims;

    vector<vector<int>> data;
    vector<vector<int>> tests;
    readTests<int,int,int> (pTestFile,numDims,data,tests);
    af::dim4 dims       = numDims[0];

    vector<Ti> in(data[0].begin(), data[0].end());

    af_array inArray   = 0;
    af_array outArray  = 0;
    af_array tempArray = 0;

    int nDevices = 0;
    ASSERT_EQ(AF_SUCCESS, af_get_device_count(&nDevices));

    for (int dev = 0; dev < nDevices; dev++) {

        ASSERT_EQ(AF_SUCCESS, af_set_device(dev));

        // Get input array
        if (isSubRef) {
            ASSERT_EQ(AF_SUCCESS, af_create_array(&tempArray, &in.front(), dims.ndims(), dims.get(), (af_dtype) af::dtype_traits<Ti>::af_type));
            ASSERT_EQ(AF_SUCCESS, af_index(&inArray, tempArray, seqv.size(), &seqv.front()));
        } else {

            ASSERT_EQ(AF_SUCCESS, af_create_array(&inArray, &in.front(), dims.ndims(), dims.get(), (af_dtype) af::dtype_traits<Ti>::af_type));
        }

        // Compare result
        for (int d = 0; d < (int)tests.size(); ++d) {
            vector<To> currGoldBar(tests[d].begin(), tests[d].end());

            // Run sum
            ASSERT_EQ(AF_SUCCESS, af_scan(&outArray, inArray, d + off));

            // Get result
            To *outData;
            outData = new To[dims.elements()];
            ASSERT_EQ(AF_SUCCESS, af_get_data_ptr((void*)outData, outArray));

            size_t nElems = currGoldBar.size();
            for (size_t elIter = 0; elIter < nElems; ++elIter) {
                ASSERT_EQ(currGoldBar[elIter], outData[elIter]) << "at: " << elIter
                                                                << " for dim " << d +off
                                                                << " for device " << dev
                                                                << std::endl;
            }

            // Delete
            delete[] outData;
        }

        if(inArray   != 0) af_destroy_array(inArray);
        if(outArray  != 0) af_destroy_array(outArray);
        if(tempArray != 0) af_destroy_array(tempArray);
    }
}

vector<af_seq> init_subs()
{
    vector<af_seq> subs;
    subs.push_back({2, 6, 1});
    subs.push_back({1, 5, 1});
    subs.push_back({1, 3, 1});
    subs.push_back({1, 2, 1});
    return subs;
}

#define SCAN_TESTS(FN, TAG, Ti, To)             \
    TEST(Scan,Test_##FN##_##TAG)                \
    {                                           \
        scanTest<Ti, To, af_##FN>(              \
            string(TEST_DIR"/scan/"#FN".test")  \
            );                                  \
    }                                           \

SCAN_TESTS(accum, float   , float     , float     );
SCAN_TESTS(accum, double  , double    , double    );
SCAN_TESTS(accum, int     , int       , int       );
SCAN_TESTS(accum, cfloat  , af_cfloat , af_cfloat );
SCAN_TESTS(accum, cdouble , af_cdouble, af_cdouble);
SCAN_TESTS(accum, unsigned, unsigned  , unsigned  );
SCAN_TESTS(accum, uchar   , unsigned char, unsigned);

TEST(Scan,Test_Scan_Big0)
{
    scanTest<int, int, af_accum>(
        string(TEST_DIR"/scan/big0.test"),
        0
        );
}

TEST(Scan,Test_Scan_Big1)
{
    scanTest<int, int, af_accum>(
        string(TEST_DIR"/scan/big1.test"),
        1
        );
}

///////////////////////////////// CPP ////////////////////////////////////
//
TEST(Scan, CPP)
{
    vector<af::dim4> numDims;

    vector<vector<int>> data;
    vector<vector<int>> tests;
    readTests<int,int,int> (string(TEST_DIR"/scan/accum.test"),numDims,data,tests);
    af::dim4 dims       = numDims[0];

    vector<float> in(data[0].begin(), data[0].end());

    int nDevices = af::getDeviceCount();;

    for (int dev = 0; dev < nDevices; dev++) {

        af::setDevice(dev);

        af::array input(dims, &(in.front()));

        // Compare result
        for (int d = 0; d < (int)tests.size(); ++d) {
            vector<float> currGoldBar(tests[d].begin(), tests[d].end());

            // Run sum
            af::array output = af::accum(input, d);

            // Get result
            float *outData;
            outData = new float[dims.elements()];
            output.host((void*)outData);

            size_t nElems = currGoldBar.size();
            for (size_t elIter = 0; elIter < nElems; ++elIter) {
                ASSERT_EQ(currGoldBar[elIter], outData[elIter]) << "at: " << elIter
                                                                << " for dim " << d
                                                                << " for device " << dev
                                                                << std::endl;
            }

            // Delete
            delete[] outData;
        }
    }
}


text:
import { expect } from 'chai';
import {
  loadCachedTestBed,
  takeScreenshot,
  TestBed,
} from '../../../puppeteer-tests/util';

describe('suggest-input-sk', () => {
  let testBed: TestBed;
  before(async () => {
    testBed = await loadCachedTestBed();
  });
  beforeEach(async () => {
    await testBed.page.goto(testBed.baseUrl);
    await testBed.page.setViewport({ width: 400, height: 500 });
  });

  it('should render the demo page', async () => {
    // Smoke test.
    expect(await testBed.page.$$('suggest-input-sk')).to.have.length(1);
  });

  describe('screenshots', () => {
    it('shows the default view', async () => {
      await takeScreenshot(testBed.page, 'ct', 'suggest-input-sk');
    });

    it('shows the suggestions', async () => {
      await testBed.page.click('input');
      await takeScreenshot(testBed.page, 'ct', 'suggest-input-sk_suggestions');
    });
  });
});


text:
if (!isObject(MineShotBehavior))
{
   %template = new BehaviorTemplate(MineShotBehavior);

   %template.friendlyName = "Tank Mine Shot";
   %template.behaviorType = "Combat";
   %template.description  = "Tank Mine laying ability";

   %template.addBehaviorField(duration, "Duration of Powerup/Ability (negative number for infinite) (in ms)", int, 15000);
   %template.addBehaviorField(damage, "Damage per mine", int, 60);

   %template.addBehaviorField(speed, "Mine Speed", float, 3);

   %template.addBehaviorField(reloadTime, "Reload time (in ms)", int, 1500);
   %template.addBehaviorField(ammoCount, "Amount of Ammo", int, 5);
   %template.addBehaviorField(mineLifespan, "LifeSpan of Mine (in ms)", int, 20000);
}

function MineShotBehavior::onBehaviorAdd(%this)
{
   %this.ammoSpent = 0;
   %this.isLoaded = true;
}

function MineShotBehavior::onBehaviorRemove(%this)
{
   %this.stopSounds();
}

function MineShotBehavior::loadMine(%this)
{
   %this.reloadSound = alxPlay("MyModule:spreadShotReloadSound");
   %this.isLoaded = true;
}
function MineShotBehavior::mineShot(%this)
{
   if(%this.ammoSpent < %this.ammoCount && %this.isLoaded)
   {
      %this.createMineShot();
      %this.shotSound = alxPlay("MyModule:spreadShotSound");
      %this.ammoSpent += 1;
      %this.isLoaded = false;
      if(%this.ammoSpent < %this.ammoCount) {
         %this.reloadSchedule = %this.schedule(%this.reloadTime, loadMine);
      } else {
         //Out of Ammo
         %this.outOfAmmoSound = alxPlay("MyModule:mineShotOutOfAmmoSound");
      }
   }
}

function MineShotBehavior::stopSounds(%this)
{
   alxStop(%this.shotSound);
   alxStop(%this.outOfAmmoSound);
   alxStop(%this.reloadSound);
}

function MineShotBehavior::createMineShot(%this)
{
   %mine = new Sprite()
   {
      class = MineShot;
      Image = "MyModule:landmineImage";
      Frame = 0;
      BodyType = dynamic;
      Size = 3;
      Position = %this.owner.getWorldPoint(0 SPC (-%this.owner.Size.height * 0.75) );
      LinearDamping = 2.0;
      AngularDamping = 1.0;
      SceneLayer = 1;
      SceneGroup = 5;
      CollisionCallback = true;

      isHidden = false;
      isAnimating = false;
   };
   %mine.setLinearVelocityPolar(%this.owner.Angle, %this.speed);
   //Sets the collision shape to a circle
   %mine.createCircleCollisionShape(%mine.Size.x / 2);
   %mine.setCollisionGroups("0 1 4 5");

   %proximitySensor = ProximitySensorBehavior.createInstance();
   %mine.addBehavior(%proximitySensor);

   %dealDmgBehavior = DealsDamageBehavior.createInstance();
   %dealDmgBehavior.strength = %this.damage;
   %mine.addBehavior(%dealDmgBehavior);

   %mine.dieSchedule = %mine.schedule(%this.mineLifespan,onDeath);

   // Add the sprite to the scene.
   %ownerScene = %this.owner.getScene();
   %ownerScene.add( %mine );
}

function MineShot::setIsNotAnimating(%this)
{
   %this.isAnimating = false;
}
function MineShot::onProximitySensorOn(%this)
{
   if(%this.isHidden && !%this.isAnimating) {
      %this.isAnimating = true;
      %this.isHidden = false;
      %this.Animation = "MyModule:landmineEmergeAnim";
      %this.animatingSchedule = %this.schedule(1500, setIsNotAnimating);
   }
}

function MineShot::onProximitySensorOff(%this)
{
   if(!%this.isHidden && !%this.isAnimating) {
      %this.isAnimating = true;
      %this.isHidden = true;
      %this.Animation = "MyModule:landmineSubmergeAnim";
      %this.animatingSchedule = %this.schedule(1500, setIsNotAnimating);
   }
}

function MineShot::onCollision(%this, %object, %details)
{
   if(%object.type $= "Vehicle") {
      %this.onDeath();
   }
}

function MineShot::onDeath(%this)
{
   %this.getScene().add(createExplosion(%this.Position.x SPC (%this.Position.y + 2), 3));
   alxPlay("MyModule:mineShotExplosionSound");
   %this.safeDelete();
}


text:
BinServerParserTests::BinServerParserTests() {

	hiapi::server::ParserPtr parser(new hiapi::server::Parser(HttpRequestPtr()));

	for (int i = 0; i<100; i++) {

		std::map<std::string, std::string> params;

		for (int j = 0; j<i+1; j++) {

			std::ostringstream ko;
			std::ostringstream vo;

			ko << "key" << j;
			vo << "value" << j;

			params[ko.str()] = vo.str();
		}

		std::string req_dump;

		hiapi::client::BinClientA::buildRequest("test_method", params, req_dump);

		std::ostringstream o;
		o << req_dump.size() << "\n" << req_dump;

		parser->execute(o.str());

		TS_ASSERT(parser->hasRequest());

		CustomRequestPtr cus_req = parser->getRequest();

		hiapi::server::Request *req = dynamic_cast<hiapi::server::Request*>( cus_req.get() );

		TS_ASSERT(req->method == "test_method");

		std::map<std::string, std::string>::iterator it = params.begin();
		std::map<std::string, std::string>::iterator end = params.end();

		while (it != end) {

			TS_ASSERT(it->second == req->params[it->first]);

			it++;
		}
	}
}


text:
 {{if request.controller == "default" and request.function == "index" or request.args == ["alert_hub_cop"]:}}

{{=T("Follow Us")}}

 {{pass}}

  {{poweredby=T("Powered by Sahana")}}
  {{=poweredby}}


text:
#ifndef QT3DCORE_CORELOGGING_P_H
#define QT3DCORE_CORELOGGING_P_H

//
//  W A R N I N G
//  -------------
//
// This file is not part of the Qt API.  It exists for the convenience
// of other Qt classes.  This header file may change from version to
// version without notice, or even be removed.
//
// We mean it.
//

#include <QLoggingCategory>

QT_BEGIN_NAMESPACE

namespace Qt3DCore {

Q_DECLARE_LOGGING_CATEGORY(Resources)
Q_DECLARE_LOGGING_CATEGORY(Nodes)
Q_DECLARE_LOGGING_CATEGORY(Aspects)
Q_DECLARE_LOGGING_CATEGORY(ChangeArbiter)

} // Qt3D

QT_END_NAMESPACE

#endif // QT3DCORE_CORELOGGING_P_H