@Before
  public void setUp() throws Exception {
    // Use the mock.logLevel system property to control the log level
    MockLogAppender.setupLogging(true);

    // Set the operation delay to 1 second so that queued operations execute immediately
    m_adapter.setDelay(1);
    m_adapter.setTimeUnit(TimeUnit.SECONDS);

    Assert.notNull(m_nodeDao, "Autowiring failed, NodeDao is null");
    Assert.notNull(m_mockEventIpcManager, "Autowiring failed, IPC manager is null");
    Assert.notNull(m_populator, "Autowiring failed, DB populator is null");
    Assert.notNull(m_adapter, "Autowiring failed, adapter is null");

    // Make sure that the localhost SNMP connection config factory has overridden
    // the normal config factory
    assertTrue(m_adapter.getSnmpPeerFactory() instanceof ProxySnmpAgentConfigFactory);

    m_populator.populateDatabase();

    OnmsNode node = m_nodeDao.get(NODE_ID);
    assertNotNull(node);
    node.setSysObjectId(".1.3");
    m_nodeDao.saveOrUpdate(node);
  }
 @Test
 public void list_should_return_non_empty_list() {
   try {
     List<User> list = userDao.list();
     Assert.notNull(list);
     Assert.notEmpty(list);
   } catch (UnexpectedException e) {
     assertFalse(true);
   } catch (SessionException e) {
     assertFalse(true);
   }
 }
 /**
  * @param history a message history
  * @param componentName the name of a component to scan for
  * @param startingIndex the index to start scanning
  * @return the properties provided by the named component or null if none available
  */
 public static Properties locateComponentInHistory(
     MessageHistory history, String componentName, int startingIndex) {
   Assert.notNull(history, "'history' must not be null");
   Assert.isTrue(StringUtils.hasText(componentName), "'componentName' must be provided");
   Assert.isTrue(
       startingIndex < history.size(), "'startingIndex' can not be greater then size of history");
   Properties component = null;
   for (int i = startingIndex; i < history.size(); i++) {
     Properties properties = history.get(i);
     if (componentName.equals(properties.get("name"))) {
       component = properties;
       break;
     }
   }
   return component;
 }
 private Schema loadSchema(Resource[] resources, String schemaLanguage)
     throws IOException, SAXException {
   Assert.notEmpty(resources, "No resources given");
   Assert.hasLength(schemaLanguage, "No schema language provided");
   Source[] schemaSources = new Source[resources.length];
   XMLReader xmlReader = XMLReaderFactory.createXMLReader();
   xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
   for (int i = 0; i < resources.length; i++) {
     Assert.notNull(resources[i], "Resource is null");
     Assert.isTrue(resources[i].exists(), "Resource " + resources[i] + " does not exist");
     InputSource inputSource = SaxResourceUtils.createInputSource(resources[i]);
     schemaSources[i] = new SAXSource(xmlReader, inputSource);
   }
   SchemaFactory schemaFactory = SchemaFactory.newInstance(schemaLanguage);
   return schemaFactory.newSchema(schemaSources);
 }
  /**
   * This test is to get unit test coverage for the rest method. Real unit tests are covered in
   * Service layer EmrServiceTest
   */
  @Test
  public void testCreateEmrCluster() throws Exception {
    // Create the namespace entity.
    NamespaceEntity namespaceEntity = createNamespaceEntity(NAMESPACE_CD);

    createEmrClusterDefinitionEntity(
        namespaceEntity,
        EMR_CLUSTER_DEFINITION_NAME,
        IOUtils.toString(
            resourceLoader
                .getResource(EMR_CLUSTER_DEFINITION_XML_FILE_WITH_CLASSPATH)
                .getInputStream()));

    // Create a cluster definition create request
    EmrClusterCreateRequest request = getNewEmrClusterCreateRequest();

    EmrCluster emrCluster = emrRestController.createEmrCluster(request);

    Assert.notNull(emrCluster);
  }
 private static void registerBean(String beanName, Object bean, BeanFactory beanFactory) {
   Assert.notNull(beanName, "bean name must not be null");
   ConfigurableListableBeanFactory configurableListableBeanFactory = null;
   if (beanFactory instanceof ConfigurableListableBeanFactory) {
     configurableListableBeanFactory = (ConfigurableListableBeanFactory) beanFactory;
   } else if (beanFactory instanceof GenericApplicationContext) {
     configurableListableBeanFactory = ((GenericApplicationContext) beanFactory).getBeanFactory();
   }
   if (bean instanceof BeanNameAware) {
     ((BeanNameAware) bean).setBeanName(beanName);
   }
   if (bean instanceof BeanFactoryAware) {
     ((BeanFactoryAware) bean).setBeanFactory(beanFactory);
   }
   if (bean instanceof InitializingBean) {
     try {
       ((InitializingBean) bean).afterPropertiesSet();
     } catch (Exception e) {
       throw new FatalBeanException("failed to register bean with test context", e);
     }
   }
   configurableListableBeanFactory.registerSingleton(beanName, bean);
 }
 private static MethodParameter getMethodParameter(Method method, int index) {
   Assert.notNull(method, "Method must be set");
   return new MethodParameter(method, index);
 }
  /**
   * This test is to get unit test coverage for the rest method. Real unit tests are covered in
   * Service layer EmrServiceTest
   */
  @Test
  public void testAddEmrSteps() throws Exception {
    // Create the namespace entity.
    NamespaceEntity namespaceEntity = createNamespaceEntity(NAMESPACE_CD);

    createEmrClusterDefinitionEntity(
        namespaceEntity,
        EMR_CLUSTER_DEFINITION_NAME,
        IOUtils.toString(
            resourceLoader
                .getResource(EMR_CLUSTER_DEFINITION_XML_FILE_WITH_CLASSPATH)
                .getInputStream()));

    // Create a cluster definition create request
    EmrClusterCreateRequest clusterCreateRequest = getNewEmrClusterCreateRequest();
    EmrCluster emrCluster = emrRestController.createEmrCluster(clusterCreateRequest);

    Assert.notNull(emrCluster);
    Assert.notNull(emrCluster.getId());

    // Create a EmrShellStepAddRequest entry to pass it to RestController
    EmrShellStepAddRequest shellRequest = getNewEmrShellStepAddRequest();

    EmrShellStep emrShellStep = emrRestController.addShellStepToEmrCluster(shellRequest);

    Assert.notNull(emrShellStep);
    Assert.notNull(emrShellStep.getId());

    EmrCluster emrClusterStatus =
        emrRestController.getEmrCluster(
            emrCluster.getNamespace(),
            emrCluster.getEmrClusterDefinitionName(),
            emrCluster.getEmrClusterName(),
            emrCluster.getId(),
            emrShellStep.getId(),
            false,
            false);
    assertEquals(emrShellStep.getId(), emrClusterStatus.getStep().getId());

    // Create a EmrHiveStepAddRequest entry to pass it to RestController
    EmrHiveStepAddRequest hiveRequest = new EmrHiveStepAddRequest();

    hiveRequest.setNamespace(NAMESPACE_CD);
    hiveRequest.setEmrClusterDefinitionName(EMR_CLUSTER_DEFINITION_NAME);
    hiveRequest.setEmrClusterName(EMR_CLUSTER_NAME);

    hiveRequest.setStepName("A_HIVE_STEP");
    hiveRequest.setScriptLocation("SCRIPT_LOCATION");

    EmrHiveStep emrHiveStep = emrRestController.addHiveStepToEmrCluster(hiveRequest);

    Assert.notNull(emrHiveStep);
    Assert.notNull(emrHiveStep.getId());
    emrClusterStatus =
        emrRestController.getEmrCluster(
            emrCluster.getNamespace(),
            emrCluster.getEmrClusterDefinitionName(),
            emrCluster.getEmrClusterName(),
            emrCluster.getId(),
            emrHiveStep.getId(),
            false,
            false);
    assertEquals(emrHiveStep.getId(), emrClusterStatus.getStep().getId());

    // Create a EmrPigStepAddRequest entry to pass it to RestController
    EmrPigStepAddRequest pigRequest = new EmrPigStepAddRequest();

    pigRequest.setNamespace(NAMESPACE_CD);
    pigRequest.setEmrClusterDefinitionName(EMR_CLUSTER_DEFINITION_NAME);
    pigRequest.setEmrClusterName(EMR_CLUSTER_NAME);

    pigRequest.setStepName("A_HIVE_STEP");
    pigRequest.setScriptLocation("SCRIPT_LOCATION");

    EmrPigStep emrPigStep = emrRestController.addPigStepToEmrCluster(pigRequest);

    Assert.notNull(emrPigStep);
    Assert.notNull(emrPigStep.getId());

    emrClusterStatus =
        emrRestController.getEmrCluster(
            emrCluster.getNamespace(),
            emrCluster.getEmrClusterDefinitionName(),
            emrCluster.getEmrClusterName(),
            emrCluster.getId(),
            emrPigStep.getId(),
            false,
            false);
    assertEquals(emrPigStep.getId(), emrClusterStatus.getStep().getId());

    // Create a EmrOozieStepAddRequest entry to pass it to RestController
    EmrOozieStepAddRequest oozieRequest = new EmrOozieStepAddRequest();

    oozieRequest.setNamespace(NAMESPACE_CD);
    oozieRequest.setEmrClusterDefinitionName(EMR_CLUSTER_DEFINITION_NAME);
    oozieRequest.setEmrClusterName(EMR_CLUSTER_NAME);

    oozieRequest.setStepName("A_HIVE_STEP");
    oozieRequest.setWorkflowXmlLocation("WORKFLOW_LOCATION");
    oozieRequest.setOoziePropertiesFileLocation("SCRIPT_LOCATION");

    EmrOozieStep emrOozieStep = emrRestController.addOozieStepToEmrCluster(oozieRequest);

    Assert.notNull(emrOozieStep);
    Assert.notNull(emrOozieStep.getId());

    emrClusterStatus =
        emrRestController.getEmrCluster(
            emrCluster.getNamespace(),
            emrCluster.getEmrClusterDefinitionName(),
            emrCluster.getEmrClusterName(),
            emrCluster.getId(),
            emrOozieStep.getId(),
            false,
            false);
    assertEquals(emrOozieStep.getId(), emrClusterStatus.getStep().getId());

    // Create a EmrHadoopJarStepAddRequest entry to pass it to RestController
    EmrHadoopJarStepAddRequest hadoopJarRequest = new EmrHadoopJarStepAddRequest();

    hadoopJarRequest.setNamespace(NAMESPACE_CD);
    hadoopJarRequest.setEmrClusterDefinitionName(EMR_CLUSTER_DEFINITION_NAME);
    hadoopJarRequest.setEmrClusterName(EMR_CLUSTER_NAME);

    hadoopJarRequest.setStepName("A_HIVE_STEP");
    hadoopJarRequest.setJarLocation("JAR_LOCATION");

    EmrHadoopJarStep emrHadoopJarStep =
        emrRestController.addHadoopJarStepToEmrCluster(hadoopJarRequest);

    Assert.notNull(emrHadoopJarStep);
    Assert.notNull(emrHadoopJarStep.getId());

    emrClusterStatus =
        emrRestController.getEmrCluster(
            emrCluster.getNamespace(),
            emrCluster.getEmrClusterDefinitionName(),
            emrCluster.getEmrClusterName(),
            emrCluster.getId(),
            emrHadoopJarStep.getId(),
            false,
            false);
    assertEquals(emrHadoopJarStep.getId(), emrClusterStatus.getStep().getId());
  }