/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.nifi.toolkit.cli.impl.result;

import org.apache.nifi.registry.flow.VersionedFlowSnapshotMetadata;
import org.apache.nifi.toolkit.cli.api.ResultType;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class TestVersionedFlowSnapshotMetadataResult {

    private ByteArrayOutputStream outputStream;
    private PrintStream printStream;

    @Before
    public void setup() {
        this.outputStream = new ByteArrayOutputStream();
        this.printStream = new PrintStream(outputStream, true);
    }

    @Test
    public void testWriteSimpleVersionedFlowSnapshotResult() throws ParseException, IOException {
        final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

        final VersionedFlowSnapshotMetadata vfs1 = new VersionedFlowSnapshotMetadata();
        vfs1.setVersion(1);
        vfs1.setAuthor("user1");
        vfs1.setTimestamp(dateFormat.parse("2018-02-14T12:00:00").getTime());
        vfs1.setComments("This is a long comment, longer than the display limit for comments");

        final VersionedFlowSnapshotMetadata vfs2 = new VersionedFlowSnapshotMetadata();
        vfs2.setVersion(2);
        vfs2.setAuthor("user2");
        vfs2.setTimestamp(dateFormat.parse("2018-02-14T12:30:00").getTime());
        vfs2.setComments("This is v2");

        final List<VersionedFlowSnapshotMetadata> versions = new ArrayList<>();
        versions.add(vfs1);
        versions.add(vfs2);

        final VersionedFlowSnapshotMetadataResult result = new VersionedFlowSnapshotMetadataResult(ResultType.SIMPLE, versions);
        result.write(printStream);

        final String resultOut = new String(outputStream.toByteArray(), StandardCharsets.UTF_8);
        //System.out.println(resultOut);

        // can't get the time zone to line up on travis, so ignore this for now
        final String expected = "\n" +
                "Ver   Date                         Author   Message                                    \n" +
                "---   --------------------------   ------   ----------------------------------------   \n" ;//+
                //"1     Wed, Feb 14 2018 12:00 EST   user1    This is a long comment, longer than t...   \n" +
                //"2     Wed, Feb 14 2018 12:30 EST   user2    This is v2                                 \n" +
                //"\n";

        Assert.assertTrue(resultOut.startsWith(expected));
    }
}

@@ -2948,12 +2948,21 @@
 cted
+Pattern
  = %22
+%5E%5C
 %5Cn%22 
@@ -2988,46 +2988,23 @@
 Ver 
-  
++
 Date 
-                       
++
  Author 
   Me
@@ -2999,17 +2999,17 @@
  Author 
- 
++
  Message
@@ -3013,160 +3013,54 @@
 age 
-                                   %5Cn%22 +%0A                %22---   --------------------------   ------   ----------------------------------------   
++%5C%5Cn%22 +%0A                %22-+ +-+ +-+ +-+ +%5C
 %5Cn%22 
-;//
 +%0A  
@@ -3301,13 +3301,18 @@
     
-//%22%5Cn
+%22(.%7C%5C%5Cn)+$
 %22;%0A%0A
@@ -3351,18 +3351,15 @@
 Out.
-startsWith
+matches
 (exp
@@ -3363,16 +3363,23 @@
 expected
+Pattern
 ));%0A    

Fix a locale-dependent test in TestVersionedFlowSnapshotMetadataResult