/**
 * Retz
 * Copyright (C) 2016-2017 Nautilus Technologies, Inc.
 * <p>
 * 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
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * 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 io.github.retz.protocol;

import io.github.retz.cli.TimestampHelper;
import io.github.retz.protocol.data.Job;
import io.github.retz.protocol.data.ResourceQuantity;
import org.junit.Before;
import org.junit.Test;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.junit.Assert.assertEquals;

public class JobTest {

    private Job job;

    @Before
    public void before() {
        String scheduled = TimestampHelper.now();
        Properties props = new Properties();
        props.setProperty("LANG", "C");
        props.setProperty("ASAKUSA_HOME", "/var/lib/asakusa/");
        props.setProperty("KEY_SECRET", "deadbeef0096");
        job = new Job(
                "bin/yaess-batch.sh m3bp.example.sales -date ....",
                scheduled,
                null,
                null,
                props,
                -42,
                10000042,
                "https://example.com:5050/path/to/sandbox",
                "hey, aren't you a Mesos task?? (<=reason)",
                0,
                0,
                "sample-app",
                "job-name-foobar",
                new HashSet<String>(),
                new ResourceQuantity(32, 65536, 8, 0, 0, 0),
                Optional.empty(),
                "my-sample-taskid",
                Job.JobState.CREATED);
        job.setName("job=name");
        job.setPriority(-10);
        job.addTags(Arrays.asList("tag1", "tag2"));
    }


    @Test
    public void ppQueued() {
        job.schedule(100000042, TimestampHelper.now());
        System.err.println(job.toString());
        System.err.println(job.pp());
        assertEquals(Job.JobState.QUEUED, job.state());
    }

    @Test
    public void ppStarting() {
        job.starting("retz-new-task-id-2354", Optional.empty(), TimestampHelper.now());
        System.err.println(job.toString());
        System.err.println(job.pp());
        assertEquals(Job.JobState.STARTING, job.state());
    }

    @Test
    public void ppStarted() {
        job.started("retz-new-task-id-2354", Optional.empty(), TimestampHelper.now());
        System.err.println(job.toString());
        System.err.println(job.pp());
        assertEquals(Job.JobState.STARTED, job.state());
    }

    @Test
    public void ppFinished() {
        job.finished(TimestampHelper.now(), Optional.empty(), 0);
        System.err.println(job.toString());
        System.err.println(job.pp());
        assertEquals(Job.JobState.FINISHED, job.state());
    }

    @Test
    public void ppKilled() {
        job.killed(TimestampHelper.now(), Optional.empty(), "deadly important stupid some reason");
        System.err.println(job.toString());
        System.err.println(job.pp());
        assertEquals(Job.JobState.KILLED, job.state());
    }

    @Test
    public void ppRetry() {
        job.doRetry();
        System.err.println(job.toString());
        System.err.println(job.pp());
        assertEquals(Job.JobState.QUEUED, job.state());
    }

    @Test
    public void maskPropertiesTest() {
        Properties props = job.props();
        List<String> secretProperties = Arrays.asList("some_secret_string", "SOME_SECRET_STRING",
                "some_password_string", "SOME_PASSWORD_STRING",
                "some_token_string", "SOME_TOKEN_STRING");
        secretProperties.forEach(k -> {
            props.setProperty(k, "%STRING_TO_REPLACE%");
        });
        List<String> otherProperties = Arrays.asList("some_other_string", "SOME_OTHER_STRING");
        otherProperties.forEach(k -> {
            props.setProperty(k, "%STRING_NOT_TO_REPLACE%");
        });

        Arrays.asList(job.toString(), job.pp()).forEach(s -> {
            System.err.println(s);
            secretProperties.forEach(k -> {
                Pattern p = Pattern.compile(String.format("^.*%s=([^,}]*).*$", k));
                Matcher m = p.matcher(s);
                assert(m.matches());
                assertEquals(m.group(1), "<masked>");
            });
            otherProperties.forEach(k -> {
                Pattern p = Pattern.compile(String.format("^.*%s=([^,}]*).*$", k));
                Matcher m = p.matcher(s);
                assert(m.matches());
                assertEquals(m.group(1), "%STRING_NOT_TO_REPLACE%");
            });
        });
    }
}

@@ -3,16 +3,19 @@
 *%0A *
+   
  Retz%0A *
  Cop
@@ -10,16 +10,19 @@
  Retz%0A *
+   
  Copyrig
@@ -68,23 +68,22 @@
  Inc.%0A *
- %3Cp%3E
 %0A *
+   
  License
@@ -141,16 +141,19 @@
 se%22);%0A *
+   
  you may
@@ -212,16 +212,19 @@
 ense.%0A *
+   
  You may
@@ -258,23 +258,26 @@
 se at%0A *
- %3Cp%3E%0A *
+%0A *       
  http://
@@ -318,15 +318,14 @@
 0%0A *
- %3Cp%3E
 %0A *
+   
  Unl
@@ -391,16 +391,19 @@
 tware%0A *
+   
  distrib
@@ -463,16 +463,19 @@
 ASIS,%0A *
+   
  WITHOUT
@@ -542,16 +542,19 @@
 lied.%0A *
+   
  See the
@@ -616,16 +616,19 @@
 s and%0A *
+   
  limitat

Fix unexpected formatting