<commit_before>/*******************************************************************************
 * This file is part of OpenNMS(R).
 *
 * Copyright (C) 2011-2015 The OpenNMS Group, Inc.
 * OpenNMS(R) is Copyright (C) 1999-2015 The OpenNMS Group, Inc.
 *
 * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
 *
 * OpenNMS(R) is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * OpenNMS(R) is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with OpenNMS(R).  If not, see:
 *      http://www.gnu.org/licenses/
 *
 * For more information contact:
 *     OpenNMS(R) Licensing <<EMAIL>>
 *     http://www.opennms.org/
 *     http://www.opennms.com/
 *******************************************************************************/

package org.opennms.smoketest;

import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;

/**
 * The Test Class for the New Provisioning UI using AngularJS.
 * <p>This test will left the current OpenNMS installation as it was before running,
 * to avoid issues related with the execution order of the smoke-tests.</p>
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public class ProvisioningNewUIIT extends OpenNMSSeleniumTestCase {

    /** The Constant NODE_LABEL. */
    private static final String NODE_LABEL = "localNode";
    private static final String NODE_FOREIGNID = "localNode";
    private static final String NODE_IPADDR = "127.0.0.1";

    private RequisitionUtils m_requisitionUtils = new RequisitionUtils(this);

    /**
     * Sets up the test.
     *
     * @throws Exception the exception
     */
    @Before
    public void setUp() throws Exception {
        m_requisitionUtils.deleteTestRequisition();
        provisioningPage();
    }

    /**
     * Tears down the test.
     * <p>Be 100% sure that there are no left-overs on the testing OpenNMS installation.</p>
     *
     * @throws Exception the exception
     */
    @After
    public void tearDown() throws Exception {
        m_requisitionUtils.deleteTestRequisition();
    }

    /**
     * Test requisition UI.
     *
     * @throws Exception the exception
     */
    @Test
    public void testRequisitionUI() throws Exception {
        // Add a new requisition
        clickId("add-requisition");
        WebElement modal = findModal();
        enterText(By.xpath("//form/input[contains(@class,'bootbox-input')]"), REQUISITION_NAME);
        modal.findElement(By.xpath("//div/button[text()='OK']")).click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//td[text()='" + REQUISITION_NAME + "']")));

        // Edit the foreign source

        findElementByXpath("//td[text()='" + REQUISITION_NAME + "']/../td/button[contains(@class,'edit-foreign-source')]").click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h4[text()='Foreign Source Definition for Requisition " + REQUISITION_NAME + "']")));

        // Add a detector
        clickId("add-detector");
        modal = findModal();
        enterText(By.xpath("//form[@name='detectorForm']//input[@ng-model='detector.<EMAIL>']"), "HTTP-8980");
        enterText(By.xpath("//form[@name='detectorForm']//input[@ng-model='detector.class']"), "HTTP");
        findElementByXpath("//form[@name='detectorForm']//ul[contains(@class, 'dropdown-menu')]/li/a/strong[text()='HTTP']").click();
        findElementByXpath("//div[contains(@class,'modal-footer')]//button[text()='Save']").click();

        // Save foreign source definition
        clickId("save-foreign-source");
        wait.until(ExpectedConditions.not(ExpectedConditions.visibilityOfElementLocated(By.id("save-foreign-source"))));

        // Go to the Requisition page
        clickId("go-back");
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h4[text()='Requisition " + REQUISITION_NAME + " (0 nodes)']")));

        // Add node to a requisition
        clickId("add-node");
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("nodeLabel")));
        enterText(By.id("nodeLabel"), NODE_LABEL);
        WebElement foreignId = m_driver.findElement(By.id("foreignId"));
        foreignId.clear();
        foreignId.sendKeys(NODE_FOREIGNID);

        // Add an IP Interface
        findElementByXpath("//*[@id='tab-interfaces']/a").click();
        final WebElement intfElement = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div/a[contains(@ng-click,'addInterface')]")));
        intfElement.click();
        final By ipaddrBy = By.xpath("//div/input[@id='ipAddress']");
        wait.until(ExpectedConditions.visibilityOfElementLocated(ipaddrBy));
        enterText(ipaddrBy, NODE_IPADDR);

        // Add a service to the IP Interface
        findElementByXpath("//div/button[contains(@ng-click,'addService')]").click();
        final String serviceXpath = "//div/input[@ng-model='service.name']";
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(serviceXpath)));
        enterText(By.xpath(serviceXpath), "HTTP-8980");

        // Save the IP interface
        findElementByXpath("//div[@class='modal-footer']/button[contains(@ng-click,'save')]").click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//td[contains(@class,'ng-binding') and text()='" + NODE_IPADDR + "']")));

        // Save the node
        clickId("save-node");
        wait.until(ExpectedConditions.not(ExpectedConditions.visibilityOfElementLocated(By.id("save-node"))));

        // Go to the requisition page
        clickId("go-back");
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//td[contains(@class,'ng-binding') and text()='" + NODE_LABEL + "']")));

        // Synchronize the requisition
        clickId("synchronize");
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".modal-dialog")));
        modal = findModal();
        modal.findElement(By.xpath("//div/button[text()='Yes']")).click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h4[text()='Requisition " + REQUISITION_NAME + " (1 nodes)']")));

        // Go to the requisitions page
        clickId("go-back");

        // Wait until the node has been added to the database, using the ReST API
        m_driver.get(BASE_URL + "opennms/rest/nodes/" + REQUISITION_NAME + ":" + NODE_FOREIGNID + "/ipinterfaces/" + NODE_IPADDR + "/services/ICMP");
        m_driver.manage().timeouts().implicitlyWait(2000, TimeUnit.MILLISECONDS);
        try {
            for (int i=0; i<30; i++) {
                try {
                    final WebElement e = m_driver.findElement(By.xpath("//service/serviceType/name[text()='ICMP']"));
                    if (e != null) {
                        break;
                    }
                } catch (Exception e) {}
                m_driver.navigate().refresh();
            }
        } finally {
            m_driver.manage().timeouts().implicitlyWait(LOAD_TIMEOUT, TimeUnit.MILLISECONDS);
        }

        // Open the nodes list page
        m_driver.get(BASE_URL + "opennms/");
        clickMenuItem("Info", "Nodes", "element/nodeList.htm");

        try {
            // Disable implicitlyWait
            m_driver.manage().timeouts().implicitlyWait(0, TimeUnit.MILLISECONDS);
            // If this is the only node on the system, we'll be sent directly to its node details page.
            findElementByXpath("//h3[text()='Availability']");
        } catch (NoSuchElementException e) {
            // If there are multiple nodes, we will be on the node list page, click through to the node
            findElementByLink(NODE_LABEL).click();
        } finally {
            // Restore the implicitlyWait timeout
            m_driver.manage().timeouts().implicitlyWait(LOAD_TIMEOUT, TimeUnit.MILLISECONDS);
        }

        wait.until(ExpectedConditions.elementToBeClickable(By.linkText("ICMP")));
        findElementByXpath("//a[contains(@href, 'element/interface.jsp') and text()='" + NODE_IPADDR + "']");
        findElementByLink("HTTP-8980");
    }

    private void clickId(final String id) throws InterruptedException {
        WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(id)));
        final long waitUntil = System.currentTimeMillis() + 60000;
        while (element.getAttribute("disabled") != null || !element.isDisplayed() || !element.isEnabled()) {
            Thread.sleep(1000);
            if (System.currentTimeMillis() >= waitUntil) {
                break;
            }
            element = findElementById(id);<commit_msg>try tweaking this a bit more to make smoke tests happy<commit_after>/*******************************************************************************
 * This file is part of OpenNMS(R).
 *
 * Copyright (C) 2011-2015 The OpenNMS Group, Inc.
 * OpenNMS(R) is Copyright (C) 1999-2015 The OpenNMS Group, Inc.
 *
 * OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.
 *
 * OpenNMS(R) is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License,
 * or (at your option) any later version.
 *
 * OpenNMS(R) is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with OpenNMS(R).  If not, see:
 *      http://www.gnu.org/licenses/
 *
 * For more information contact:
 *     OpenNMS(R) Licensing <<EMAIL>>
 *     http://www.opennms.org/
 *     http://www.opennms.com/
 *******************************************************************************/

package org.opennms.smoketest;

import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;

/**
 * The Test Class for the New Provisioning UI using AngularJS.
 * <p>This test will left the current OpenNMS installation as it was before running,
 * to avoid issues related with the execution order of the smoke-tests.</p>
 * 
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
public class ProvisioningNewUIIT extends OpenNMSSeleniumTestCase {

    /** The Constant NODE_LABEL. */
    private static final String NODE_LABEL = "localNode";
    private static final String NODE_FOREIGNID = "localNode";
    private static final String NODE_IPADDR = "127.0.0.1";

    private RequisitionUtils m_requisitionUtils = new RequisitionUtils(this);

    /**
     * Sets up the test.
     *
     * @throws Exception the exception
     */
    @Before
    public void setUp() throws Exception {
        m_requisitionUtils.deleteTestRequisition();
        provisioningPage();
    }

    /**
     * Tears down the test.
     * <p>Be 100% sure that there are no left-overs on the testing OpenNMS installation.</p>
     *
     * @throws Exception the exception
     */
    @After
    public void tearDown() throws Exception {
        m_requisitionUtils.deleteTestRequisition();
    }

    /**
     * Test requisition UI.
     *
     * @throws Exception the exception
     */
    @Test
    public void testRequisitionUI() throws Exception {
        // Add a new requisition
        clickId("add-requisition");
        WebElement modal = findModal();
        enterText(By.xpath("//form/input[contains(@class,'bootbox-input')]"), REQUISITION_NAME);
        modal.findElement(By.xpath("//div/button[text()='OK']")).click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//td[text()='" + REQUISITION_NAME + "']")));

        // Edit the foreign source

        findElementByXpath("//td[text()='" + REQUISITION_NAME + "']/../td/button[contains(@class,'edit-foreign-source')]").click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h4[text()='Foreign Source Definition for Requisition " + REQUISITION_NAME + "']")));

        // Add a detector
        clickId("add-detector");
        modal = findModal();
        enterText(By.xpath("//form[@name='detectorForm']//input[@ng-model='detector.name']"), "HTTP-8980");
        enterText(By.xpath("//form[@name='detectorForm']//input[@ng-model='detector.class']"), "HTTP");
        findElementByXpath("//form[@name='detectorForm']//ul[contains(@class, 'dropdown-menu')]/li/a/strong[text()='HTTP']").click();
        findElementByXpath("//div[contains(@class,'modal-footer')]//button[text()='Save']").click();

        // Save foreign source definition
        clickId("save-foreign-source");
        wait.until(ExpectedConditions.not(ExpectedConditions.visibilityOfElementLocated(By.id("save-foreign-source"))));

        // Go to the Requisition page
        clickId("go-back");
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h4[text()='Requisition " + REQUISITION_NAME + " (0 nodes)']")));

        // Add node to a requisition
        clickId("add-node");
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("nodeLabel")));
        enterText(By.id("nodeLabel"), NODE_LABEL);
        WebElement foreignId = m_driver.findElement(By.id("foreignId"));
        foreignId.clear();
        foreignId.sendKeys(NODE_FOREIGNID);

        // Add an IP Interface
        findElementByXpath("//*[@id='tab-interfaces']/a").click();
        final WebElement intfElement = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div/a[contains(@ng-click,'addInterface')]")));
        intfElement.click();
        final By ipaddrBy = By.xpath("//div/input[@id='ipAddress']");
        wait.until(ExpectedConditions.visibilityOfElementLocated(ipaddrBy));
        enterText(ipaddrBy, NODE_IPADDR);

        // Add a service to the IP Interface
        findElementByXpath("//div/button[contains(@ng-click,'addService')]").click();
        final String serviceXpath = "//div/input[@ng-model='service.name']";
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(serviceXpath)));
        enterText(By.xpath(serviceXpath), "HTTP-8980");

        // Save the IP interface
        findElementByXpath("//div[@class='modal-footer']/button[contains(@ng-click,'save')]").click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//td[contains(@class,'ng-binding') and text()='" + NODE_IPADDR + "']")));

        // Save the node
        clickId("save-node");
        wait.until(ExpectedConditions.not(ExpectedConditions.visibilityOfElementLocated(By.id("save-node"))));

        // Go to the requisition page
        clickId("go-back");
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//td[contains(@class,'ng-binding') and text()='" + NODE_LABEL + "']")));

        // Synchronize the requisition
        clickId("synchronize");
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".modal-dialog")));
        modal = findModal();
        modal.findElement(By.xpath("//div/button[text()='Yes']")).click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//h4[text()='Requisition " + REQUISITION_NAME + " (1 nodes)']")));

        // Go to the requisitions page
        clickId("go-back");

        // Wait until the node has been added to the database, using the ReST API
        m_driver.get(BASE_URL + "opennms/rest/nodes/" + REQUISITION_NAME + ":" + NODE_FOREIGNID + "/ipinterfaces/" + NODE_IPADDR + "/services/ICMP");
        m_driver.manage().timeouts().implicitlyWait(2000, TimeUnit.MILLISECONDS);
        try {
            for (int i=0; i<30; i++) {
                try {
                    final WebElement e = m_driver.findElement(By.xpath("//service/serviceType/name[text()='ICMP']"));
                    if (e != null) {
                        break;
                    }
                } catch (Exception e) {}
                m_driver.navigate().refresh();
            }
        } finally {
            m_driver.manage().timeouts().implicitlyWait(LOAD_TIMEOUT, TimeUnit.MILLISECONDS);
        }

        // Open the nodes list page
        m_driver.get(BASE_URL + "opennms/");
        clickMenuItem("Info", "Nodes", "element/nodeList.htm");

        try {
            // Disable implicitlyWait
            m_driver.manage().timeouts().implicitlyWait(0, TimeUnit.MILLISECONDS);
            // If this is the only node on the system, we'll be sent directly to its node details page.
            findElementByXpath("//h3[text()='Availability']");
        } catch (NoSuchElementException e) {
            // If there are multiple nodes, we will be on the node list page, click through to the node
            findElementByLink(NODE_LABEL).click();
        } finally {
            // Restore the implicitlyWait timeout
            m_driver.manage().timeouts().implicitlyWait(LOAD_TIMEOUT, TimeUnit.MILLISECONDS);
        }

        wait.until(ExpectedConditions.elementToBeClickable(By.linkText("ICMP")));
        findElementByXpath("//a[contains(@href, 'element/interface.jsp') and text()='" + NODE_IPADDR + "']");
        findElementByLink("HTTP-8980");
    }

    private void clickId(final String id) throws InterruptedException {
        WebElement element = findElementById(id);
        final long waitUntil = System.currentTimeMillis() + 60000;
        while (element.getAttribute("disabled") != null || !element.isDisplayed() || !element.isEnabled()) {
            if (System.currentTimeMillis() >= waitUntil) {
                break;
            }
            Thread.sleep(1000);
            element = findElementById(id);
{'fraction_non_alphanumeric': 0.15494475882511452, 'fraction_numerical': 0.006575047157100512, 'mean_word_length': 3.127224199288256, 'pattern_counts': {'":': 2, '<': 26, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '140570', 'n_tokens_mistral': 5544, 'n_tokens_neox': 5327, 'n_words': 1147}