<commit_before>package com.suse.saltstack.netapi.client;

import com.suse.saltstack.netapi.exception.SaltStackException;
import com.suse.saltstack.netapi.results.SaltStackToken;

import com.github.tomakehurst.wiremock.junit.WireMockRule;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.net.HttpURLConnection;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.junit.Assert.assertEquals;

/**
 * SaltStack API unit tests.
 */
public class SaltStackClientTest {
    private static final int MOCK_HTTP_PORT = 8888;

    @Rule
    public WireMockRule wireMockRule = new WireMockRule(MOCK_HTTP_PORT);

    private SaltStackClient client;

    @Before
    public void init() throws SaltStackException {
        client = new SaltStackClient(
                "http://localhost:" + Integer.toString(MOCK_HTTP_PORT));
    }

    @Test
    public void testLoginOk() throws SaltStackException {
        final String JSON_LOGIN_REQUEST = "{\"username\":\"user\"," +
                "\"password\":\"<PASSWORD>\",\"eauth\":\"auto\"}";
        final String JSON_LOGIN_RESPONSE = "{\"return\": [{\"perms\": [\".*\"], " +
                "\"start\": 1422803163.765152, " +
                "\"token\": \"2fea67bb673e012f11ca7cad0d1079ccf1decaa2\", " +
                "\"expire\": 1422846363.765152, " +
                "\"user\": \"user\", \"eauth\": \"auto\"}]}";

        stubFor(post(urlEqualTo("/login"))
                .withHeader("Accept", equalTo("application/json"))
                .withHeader("Content-Type", equalTo("application/json"))
                .withRequestBody(equalTo(JSON_LOGIN_REQUEST))
                .willReturn(aResponse()
                        .withStatus(HttpURLConnection.HTTP_OK)
                        .withHeader("Content-Type", "application/json")
                        .withBody(JSON_LOGIN_RESPONSE)));

        SaltStackToken token = client.login("user", "pass");
        assertEquals("Token mismatch", token.getToken(), "2fea67bb673e012f11ca7cad0d1079ccf1decaa2");
        assertEquals("EAuth mismatch", token.getEauth(), "auto");
        assertEquals("User mismatch", token.getUser(), "user");
    }

    @Test(expected = SaltStackException.class)
    public void testLoginFailure() throws SaltStackException {
        stubFor(post(urlEqualTo("/login"))
                .withHeader("Accept", equalTo("application/json"))
                .withHeader("Content-Type", equalTo("application/json"))
                .willReturn(aResponse()
                        .withStatus(HttpURLConnection.HTTP_UNAUTHORIZED)));
        client.login("user", "pass");
    }
}<commit_msg>Add test cases for SaltStackClient.run()<commit_after>package com.suse.saltstack.netapi.client;

import com.suse.saltstack.netapi.exception.SaltStackException;
import com.suse.saltstack.netapi.results.SaltStackToken;
import com.suse.saltstack.netapi.results.SaltStackRunResults;

import com.github.tomakehurst.wiremock.junit.WireMockRule;

import java.util.List;
import java.util.Map;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.net.HttpURLConnection;

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNotNull;

/**
 * SaltStack API unit tests.
 */
public class SaltStackClientTest {
    private static final int MOCK_HTTP_PORT = 8888;

    @Rule
    public WireMockRule wireMockRule = new WireMockRule(MOCK_HTTP_PORT);

    private SaltStackClient client;

    @Before
    public void init() throws SaltStackException {
        client = new SaltStackClient(
                "http://localhost:" + Integer.toString(MOCK_HTTP_PORT));
    }

    @Test
    public void testLoginOk() throws SaltStackException {
        final String JSON_LOGIN_REQUEST = "{\"username\":\"user\"," +
                "\"password\":\"<PASSWORD>\",\"eauth\":\"auto\"}";
        final String JSON_LOGIN_RESPONSE = "{\"return\": [{\"perms\": [\".*\"], " +
                "\"start\": 1422803163.765152, " +
                "\"token\": \"2fea67bb673e012f11ca7cad0d1079ccf1decaa2\", " +
                "\"expire\": 1422846363.765152, " +
                "\"user\": \"user\", \"eauth\": \"auto\"}]}";

        stubFor(post(urlEqualTo("/login"))
                .withHeader("Accept", equalTo("application/json"))
                .withHeader("Content-Type", equalTo("application/json"))
                .withRequestBody(equalTo(JSON_LOGIN_REQUEST))
                .willReturn(aResponse()
                        .withStatus(HttpURLConnection.HTTP_OK)
                        .withHeader("Content-Type", "application/json")
                        .withBody(JSON_LOGIN_RESPONSE)));

        SaltStackToken token = client.login("user", "pass");
        assertEquals("Token mismatch", token.getToken(), "2fea67bb673e012f11ca7cad0d1079ccf1decaa2");
        assertEquals("EAuth mismatch", token.getEauth(), "auto");
        assertEquals("User mismatch", token.getUser(), "user");
    }

    @Test(expected = SaltStackException.class)
    public void testLoginFailure() throws SaltStackException {
        stubFor(post(urlEqualTo("/login"))
                .withHeader("Accept", equalTo("application/json"))
                .withHeader("Content-Type", equalTo("application/json"))
                .willReturn(aResponse()
                        .withStatus(HttpURLConnection.HTTP_UNAUTHORIZED)));
        client.login("user", "pass");
    }

    @Test
    public void testRunRequest() throws SaltStackException {
        final String JSON_RUN_REQUEST = "[{\"username\":\"user\",\"password\":\"<PASSWORD>\"" +
                ",\"eauth\":\"pam\",\"client\":\"local\",\"tgt\":\"*\",\"fun\":" +
                "\"test.ping\"}]";
        final String JSON_RUN_RESPONSE = "{\"return\": [{\"minion-1\": true}]}";

        stubFor(post(urlEqualTo("/run"))
                .willReturn(aResponse()
                    .withStatus(HttpURLConnection.HTTP_OK)
                    .withHeader("Content-Type", "application/json")
                    .withBody(JSON_RUN_RESPONSE)));

        SaltStackRunResults results = client.run("user", "pass", "<PASSWORD>", "local", "*",
                    "test.ping", null, null);

        verify(1, postRequestedFor(urlEqualTo("/run"))
                .withHeader("Accept", equalTo("application/json"))
                .withHeader("Content-Type", equalTo("application/json"))
                .withRequestBody(equalTo(JSON_RUN_REQUEST)));

    }

    @Test
    public void testRunResult() throws SaltStackException {
        final String JSON_RUN_RESPONSE = "{\"return\": [{\"minion-1\": true}]}";

        stubFor(post(urlEqualTo("/run"))
                .willReturn(aResponse()
                    .withStatus(HttpURLConnection.HTTP_OK)
                    .withHeader("Content-Type", "application/json")
                    .withBody(JSON_RUN_RESPONSE)));

        SaltStackRunResults results = client.run("user", "pass", "<PASSWORD>", "local", "*",
                "test.ping", null, null);

        assertNotNull(results);

        List<Map<String,String>> resultList = results.getResults();
        assertEquals(resultList.size(), 1);

        Map resultDetails = resultList.get(0);
        assertTrue(resultDetails.containsKey("minion-1"));
        assertEquals(resultDetails.get("minion-1"), "true");
    }
}
{'fraction_non_alphanumeric': 0.1501610305958132, 'fraction_numerical': 0.020799785292538914, 'mean_word_length': 2.6642084562438546, 'pattern_counts': {'":': 30, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3958037', 'n_tokens_mistral': 2292, 'n_tokens_neox': 2116, 'n_words': 343}