<commit_before>/*
 * Copyright 2012 Splunk, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"): you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.splunk;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
import java.util.Collection;

public class ApplicationTest extends SDKTestCase {
    String applicationName;
    Application application;

    @Before @Override public void setUp() throws Exception {
        super.setUp();

        for (Application app : service.getApplications().values()) {
            if (app.getName().startsWith("delete-me")) {
                app.remove();
            }
        }

        applicationName = createTemporaryName();
        EntityCollection<Application> applications = service.getApplications();
        application = applications.create(applicationName);
    }

    @After @Override public void tearDown() throws Exception {
        super.tearDown();
        final EntityCollection<Application> apps = service.getApplications();
        for (Application app : apps.values()) {
            final String appName = app.getName();
            if (appName.startsWith("delete-me")) {
                app.remove();
                assertEventuallyTrue(new EventuallyTrueBehavior() {
                    @Override public boolean predicate() {
                        apps.refresh();
                        return !apps.containsKey(appName);
                    }
                });
            }
        }
        // Clear the restart message that deleting apps causes in splunkd.
        // It's fine to keep going despite it.
        clearRestartMessage();
    }

    @Test public void testForEmptySetup() {
        // Newly created applications have no setup.
        ApplicationSetup applicationSetup = application.setup();
        assertNull(applicationSetup.getSetupXml());
    }

    @Test public void testForSetupPresent() {
        if (!hasApplicationCollection()) {
           return;
        }
        try {
            installApplicationFromCollection("has_setup_xml");
        } catch (Exception e) {
            fail(e.toString());
        }
        assertTrue(service.getApplications().containsKey("has_setup_xml"));
        Application applicationWithSetupXml = service.getApplications().get("has_setup_xml");
        ApplicationSetup applicationSetup = applicationWithSetupXml.setup();
        assertEquals("has_setup_xml", applicationSetup.getName());
        String xml = applicationSetup.getSetupXml();
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        Document parsedXml = null;
        try {
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            InputStream xmlStream = new ByteArrayInputStream(xml.getBytes());
            parsedXml = docBuilder.parse(xmlStream);
        } catch (Exception e) {
            fail(e.toString());
        }
        parsedXml.getDocumentElement().normalize();
        assertEquals(parsedXml.getDocumentElement().getNodeName(), "SetupInfo");
        NodeList blocks = parsedXml.getDocumentElement().getElementsByTagName("block");
        assertEquals(1, blocks.getLength());
        Node block = blocks.item(0);
        assertEquals("block", block.getNodeName());
    }

    @Test
    public void testArchive() {
        ApplicationArchive archive = application.archive();
        assertEquals(applicationName, archive.getAppName());
        String path = archive.getFilePath();
        File archiveFile = new File(path);
        assertTrue(archiveFile.exists());
    }

    @Test
    public void testFields() {
        // Initially, should be empty.
        assertEquals(null, application.getAuthor());
        assertTrue(application.getCheckForUpdates());
        assertFalse(application.isConfigured());
        assertTrue(application.isVisible());
        assertFalse(application.stateChangeRequiresRestart());

        String authorString = "<NAME> <PASSWORD>oon";
        application.setAuthor(authorString);
        application.setCheckForUpdates(false);
        String descriptionString = "Meep the nudebranch!";
        application.setDescription(descriptionString);
        String labelString = "Hugga wugga";
        application.setLabel(labelString);
        String versionString = "VII";
        application.setVersion(versionString);
        application.setConfigured(true);
        application.setVisible(false);
<commit_msg>Refactor ApplicationTest.<commit_after>/*
 * Copyright 2012 Splunk, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"): you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.splunk;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.*;
import java.util.Collection;

public class ApplicationTest extends SDKTestCase {
    String applicationName;
    Application application;

    @Before @Override public void setUp() throws Exception {
        super.setUp();

        for (Application app : service.getApplications().values()) {
            if (app.getName().startsWith("delete-me")) {
                app.remove();
            }
        }

        applicationName = createTemporaryName();
        application = service.getApplications().create(applicationName);
    }

    @After @Override public void tearDown() throws Exception {
        super.tearDown();
        
        final EntityCollection<Application> apps = service.getApplications();
        for (Application app : apps.values()) {
            final String appName = app.getName();
            if (appName.startsWith("delete-me")) {
                app.remove();
                assertEventuallyTrue(new EventuallyTrueBehavior() {
                    @Override public boolean predicate() {
                        apps.refresh();
                        return !apps.containsKey(appName);
                    }
                });
            }
        }
        
        // Clear the restart message that deleting apps causes in splunkd.
        // It's fine to keep going despite it.
        clearRestartMessage();
    }

    @Test public void testForEmptySetup() {
        // Newly created applications have no setup.
        assertNull(application.setup().getSetupXml());
    }

    @Test public void testForSetupPresent() throws Exception {
        if (!hasApplicationCollection()) {
            return;
        }
        
        installApplicationFromCollection("has_setup_xml");
        assertTrue(service.getApplications().containsKey("has_setup_xml"));
        Application applicationWithSetupXml = service.getApplications().get("has_setup_xml");
        
        ApplicationSetup applicationSetup = applicationWithSetupXml.setup();
        assertEquals("has_setup_xml", applicationSetup.getName());
        
        String setupXml = applicationSetup.getSetupXml();
        Document parsedSetupXml = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(
            new ByteArrayInputStream(setupXml.getBytes()));
        parsedSetupXml.getDocumentElement().normalize();
        
        assertEquals(parsedSetupXml.getDocumentElement().getNodeName(), "SetupInfo");
        
        NodeList blocks = parsedSetupXml.getDocumentElement().getElementsByTagName("block");
        assertEquals(1, blocks.getLength());
        Node block = blocks.item(0);
        assertEquals("block", block.getNodeName());
    }

    @Test
    public void testArchive() {
        ApplicationArchive archive = application.archive();
        assertEquals(applicationName, archive.getAppName());
        assertTrue(new File(archive.getFilePath()).exists());
    }

    @Test
    public void testFields() {
        // Initially, should be empty.
        assertEquals(null, application.getAuthor());
        assertTrue(application.getCheckForUpdates());
        assertFalse(application.isConfigured());
        assertTrue(application.isVisible());
        assertFalse(application.stateChangeRequiresRestart());

        String authorString = "<NAME>";
        application.setAuthor(authorString);
        application.setCheckForUpdates(false);
        String descriptionString = "Meep the nudebranch!";
        application.setDescription(descriptionString);
        String labelString = "Hugga wugga";
        application.setLabel(labelString);
        String versionString = "VII";
        application.setVersion(versionString);
        application.setConfigured(true);
        application.setVisible(false);

{'fraction_non_alphanumeric': 0.100276554337806, 'fraction_numerical': 0.002663115845539281, 'mean_word_length': 2.764070932922128, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 16}, 'pii_count': 0, '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': '1355231', 'n_tokens_mistral': 2369, 'n_tokens_neox': 2312, 'n_words': 594}