<commit_before>package com.treasure_data.jdbc.command;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.Properties;

import org.junit.Ignore;
import org.junit.Test;

import com.treasure_data.client.ClientException;
import com.treasure_data.client.TreasureDataClient;
import com.treasure_data.model.AuthenticateRequest;
import com.treasure_data.model.AuthenticateResult;
import com.treasure_data.model.Database;
import com.treasure_data.model.Job;
import com.treasure_data.model.JobSummary;
import com.treasure_data.model.ShowJobRequest;
import com.treasure_data.model.ShowJobResult;
import com.treasure_data.model.JobSummary.Debug;
import com.treasure_data.model.ShowJobStatusRequest;
import com.treasure_data.model.ShowJobStatusResult;

public class TestTDClientAPI {

    @Test
    public void testWaitJobResult01() throws Exception {
        Properties props = System.getProperties();
        props.load(this.getClass().getClassLoader().getResourceAsStream("treasure-data.properties.default"));

        TreasureDataClient c = new TreasureDataClient() {
            @Override public AuthenticateResult authenticate(AuthenticateRequest request) {
                return null;
            }

            @Override public ShowJobStatusResult showJobStatus(ShowJobStatusRequest request) {
                return new ShowJobStatusResult(JobSummary.Status.SUCCESS);
            }

            @Override public ShowJobResult showJob(ShowJobRequest request) {
                JobSummary js = new JobSummary("12345", Job.Type.HIVE, null, null, null,
                        JobSummary.Status.SUCCESS, null, null, "query", "rschema");
                return new ShowJobResult(js);
            }
        };
        TDClientAPI api = new TDClientAPI(c, props, new Database("mugadb"));

        Job job = new Job("12345");
        JobSummary js = api.waitJobResult(job);
        assertEquals("12345", js.getJobID());
        assertEquals("query", js.getQuery());
        assertEquals("rschema", js.getResultSchema());
    }

    @Test
    public void testWaitJobResult02() throws Exception {
        Properties props = System.getProperties();
        props.load(this.getClass().getClassLoader().getResourceAsStream("treasure-data.properties.default"));

        { // error occurred
            final String jobID = "12345";
            TreasureDataClient c = new TreasureDataClient() {
                @Override public AuthenticateResult authenticate(AuthenticateRequest request) {
                    return null;
                }

                @Override public ShowJobStatusResult showJobStatus(ShowJobStatusRequest request) {
                    return new ShowJobStatusResult(JobSummary.Status.ERROR);
                }

                @Override public ShowJobResult showJob(ShowJobRequest request) {
                    JobSummary js = new JobSummary(jobID, Job.Type.HIVE, new Database("mugadb"),
                            "url", "resultTable", JobSummary.Status.ERROR, "startAt", "endAt",
                            "query", "rschema", new Debug("cmdout", "stderr"));
                    return new ShowJobResult(js);
                }
            };
            TDClientAPI api = new TDClientAPI(c, props, new Database("mugadb"));

            try {
                Job job = new Job(jobID);
                JobSummary js = api.waitJobResult(job);
                fail();
            } catch (Throwable t) {
                assertTrue(t instanceof ClientException);
            }
        }
        { // job are killed
            final String jobID = "12345";
            TreasureDataClient c = new TreasureDataClient() {
                @Override public AuthenticateResult authenticate(AuthenticateRequest request) {
                    return null;
                }

                @Override public ShowJobStatusResult showJobStatus(ShowJobStatusRequest request) {
                    return new ShowJobStatusResult(JobSummary.Status.ERROR);
                }

                @Override public ShowJobResult showJob(ShowJobRequest request) {
                    JobSummary js = new JobSummary(jobID, Job.Type.HIVE, new Database("mugadb"),
                            "url", "resultTable", JobSummary.Status.ERROR, "startAt", "endAt",
                            "query", "rschema", new Debug("cmdout", "stderr"));
                    return new ShowJobResult(js);
                }
            };
            TDClientAPI api = new TDClientAPI(c, props, new Database("mugadb"));

            try {
                Job job = new Job(jobID);
                JobSummary js = api.waitJobResult(job);
                fail();
            } catch (Throwable t) {
                assertTrue(t instanceof ClientException);
            }
        }
    }

    @Test
    public void testWaitJobResult03() throws Exception {
        Properties props = System.getProperties();
        props.load(this.getClass().getClassLoader().getResourceAsStream("treasure-data.properties.default"));

        TreasureDataClient c = new TreasureDataClient() {
            private int count = 0;
            @Override public AuthenticateResult authenticate(AuthenticateRequest request) {
                return null;
            }

            @Override public ShowJobStatusResult showJobStatus(ShowJobStatusRequest request) {
                if (count > 2) {
                    count++;
                    return new ShowJobStatusResult(JobSummary.Status.SUCCESS);
                } else {
                    count++;
                    return new ShowJobStatusResult(JobSummary.Status.RUNNING);
                }
            }

            @Override public ShowJobResult showJob(ShowJobRequest request) {
                JobSummary js = new JobSummary("12345", Job.Type.HIVE, null, null, null,
                        JobSummary.Status.SUCCESS, null, null, "query", "rschema");
                    return new ShowJobResult(js);
            }
        };
        TDClientAPI api = new TDClientAPI(c, props, new Database("mugadb"));
<commit_msg>minor change of test methods<commit_after>package com.treasure_data.jdbc.command;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.Properties;

import org.junit.Ignore;
import org.junit.Test;

import com.treasure_data.client.ClientException;
import com.treasure_data.client.TreasureDataClient;
import com.treasure_data.model.AuthenticateRequest;
import com.treasure_data.model.AuthenticateResult;
import com.treasure_data.model.Database;
import com.treasure_data.model.Job;
import com.treasure_data.model.JobSummary;
import com.treasure_data.model.ShowJobRequest;
import com.treasure_data.model.ShowJobResult;
import com.treasure_data.model.JobSummary.Debug;
import com.treasure_data.model.ShowJobStatusRequest;
import com.treasure_data.model.ShowJobStatusResult;

public class TestTDClientAPI {
    private static void mockProperties(Properties props) {
	props.setProperty("user", "xxxx");
	props.setProperty("password", "<PASSWORD>");
	props.setProperty("password", "<PASSWORD>");
	props.setProperty("td.logger.api.key", "xxxx");
    }

    @Test
    public void testWaitJobResult01() throws Exception {
        Properties props = System.getProperties();
	mockProperties(props);

        TreasureDataClient c = new TreasureDataClient() {
            @Override public AuthenticateResult authenticate(AuthenticateRequest request) {
                return null;
            }

            @Override public ShowJobStatusResult showJobStatus(ShowJobStatusRequest request) {
                return new ShowJobStatusResult(JobSummary.Status.SUCCESS);
            }

            @Override public ShowJobResult showJob(ShowJobRequest request) {
                JobSummary js = new JobSummary("12345", Job.Type.HIVE, null, null, null,
                        JobSummary.Status.SUCCESS, null, null, "query", "rschema");
                return new ShowJobResult(js);
            }
        };
        TDClientAPI api = new TDClientAPI(c, props, new Database("mugadb"));

        Job job = new Job("12345");
        JobSummary js = api.waitJobResult(job);
        assertEquals("12345", js.getJobID());
        assertEquals("query", js.getQuery());
        assertEquals("rschema", js.getResultSchema());
    }

    @Test
    public void testWaitJobResult02() throws Exception {
        Properties props = System.getProperties();
	mockProperties(props);

        { // error occurred
            final String jobID = "12345";
            TreasureDataClient c = new TreasureDataClient() {
                @Override public AuthenticateResult authenticate(AuthenticateRequest request) {
                    return null;
                }

                @Override public ShowJobStatusResult showJobStatus(ShowJobStatusRequest request) {
                    return new ShowJobStatusResult(JobSummary.Status.ERROR);
                }

                @Override public ShowJobResult showJob(ShowJobRequest request) {
                    JobSummary js = new JobSummary(jobID, Job.Type.HIVE, new Database("mugadb"),
                            "url", "resultTable", JobSummary.Status.ERROR, "startAt", "endAt",
                            "query", "rschema", new Debug("cmdout", "stderr"));
                    return new ShowJobResult(js);
                }
            };
            TDClientAPI api = new TDClientAPI(c, props, new Database("mugadb"));

            try {
                Job job = new Job(jobID);
                JobSummary js = api.waitJobResult(job);
                fail();
            } catch (Throwable t) {
                assertTrue(t instanceof ClientException);
            }
        }
        { // job are killed
            final String jobID = "12345";
            TreasureDataClient c = new TreasureDataClient() {
                @Override public AuthenticateResult authenticate(AuthenticateRequest request) {
                    return null;
                }

                @Override public ShowJobStatusResult showJobStatus(ShowJobStatusRequest request) {
                    return new ShowJobStatusResult(JobSummary.Status.ERROR);
                }

                @Override public ShowJobResult showJob(ShowJobRequest request) {
                    JobSummary js = new JobSummary(jobID, Job.Type.HIVE, new Database("mugadb"),
                            "url", "resultTable", JobSummary.Status.ERROR, "startAt", "endAt",
                            "query", "rschema", new Debug("cmdout", "stderr"));
                    return new ShowJobResult(js);
                }
            };
            TDClientAPI api = new TDClientAPI(c, props, new Database("mugadb"));

            try {
                Job job = new Job(jobID);
                JobSummary js = api.waitJobResult(job);
                fail();
            } catch (Throwable t) {
                assertTrue(t instanceof ClientException);
            }
        }
    }

    @Test
    public void testWaitJobResult03() throws Exception {
        Properties props = System.getProperties();
	mockProperties(props);

        TreasureDataClient c = new TreasureDataClient() {
            private int count = 0;
            @Override public AuthenticateResult authenticate(AuthenticateRequest request) {
                return null;
            }

            @Override public ShowJobStatusResult showJobStatus(ShowJobStatusRequest request) {
                if (count > 2) {
                    count++;
                    return new ShowJobStatusResult(JobSummary.Status.SUCCESS);
                } else {
                    count++;
                    return new ShowJobStatusResult(JobSummary.Status.RUNNING);
                }
            }

            @Override public ShowJobResult showJob(ShowJobRequest request) {
                JobSummary js = new JobSummary("12345", Job.Type.HIVE, null, null, null,
                        JobSummary.Status.SUCCESS, null, null, "query", "rschema");
                    return new ShowJobResult(js);
            }
        };
        TDClientAPI api = new TDClientAPI(c, props, new Database("mugadb"));

{'fraction_non_alphanumeric': 0.09246099812137548, 'fraction_numerical': 0.006207628849138283, 'mean_word_length': 2.1179017061370002, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '1088244', 'n_tokens_mistral': 3181, 'n_tokens_neox': 2991, 'n_words': 695}