/*
 * Copyright 2016 - Fabio "MrWHO" Torchetti
 *
 * 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.
 */
//CHECKSTYLE:OFF
package net.wedjaa.ansible.vault.crypto;

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

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class VaultHandlerTest {

  final static String TEST_STRING = "This is a test";
  final static String TEST_PASSWORD = "password";
  final static String TEST_WRONG_PASSWORD = "not_this_one";
  final static String WRONG_PASS_EX = "HMAC Digest doesn't match - possibly it's the wrong password.";
  final static String DECODED_VAULT = "!net.wedjaa.ansible.vault.ProvisioningInfo\n"
      + "apiClientId: The provisioner ClientId\n"
      + "apiPassword: The secret password\n"
      + "apiUser: Secret User\n";

  Logger logger = LoggerFactory.getLogger(VaultHandlerTest.class);

  @Test
  public void testByteArrayValidVault() throws Exception {
    logger.info("Testing Byte Array decryption - Valid Password");

    byte[] encryptedTest = VaultHandler.encrypt(TEST_STRING.getBytes(), TEST_PASSWORD);
    logger.debug("Encrypted vault:\n{}", new String(encryptedTest));
    byte[] decryptedTest = VaultHandler.decrypt(encryptedTest, TEST_PASSWORD);
    logger.debug("Decrypted vault:\n{}", new String(decryptedTest));
    assertEquals(TEST_STRING, new String(decryptedTest));
  }

  @Test
  public void testByteArrayInvalidVault() {
    logger.info("Testing Byte Array decryption - Invalid Password");
    try {
      byte[] encryptedTest = VaultHandler.encrypt(TEST_STRING.getBytes(), TEST_PASSWORD);
      logger.debug("Encrypted vault:\n{}", new String(encryptedTest));
      byte[] decryptedTest = VaultHandler.decrypt(encryptedTest, TEST_WRONG_PASSWORD);
      logger.debug("Decrypted vault:\n{}", new String(decryptedTest));
      fail("Should not be able to decrypt text with the wrong password");
    }
    catch (Exception ex) {
      assertEquals(WRONG_PASS_EX, ex.getMessage());
    }
  }

  @Test
  public void testStreamValidVault() throws Exception {
    logger.info("Testing decoding vault Stream - Valid password ");

    ByteArrayOutputStream decodedStream = new ByteArrayOutputStream();
    InputStream encodedStream = getClass().getClassLoader().getResourceAsStream("vault-sample-wedjaa/test-vault.yml");
    VaultHandler.decrypt(encodedStream, decodedStream, TEST_PASSWORD);
    String decoded = new String(decodedStream.toByteArray());
    assertEquals(DECODED_VAULT, decoded);
  }

  @Test
  public void testStreamInvalidVault() {
    logger.info("Testing decoding vault Stream - Invalid password ");
    try {
      ByteArrayOutputStream decodedStream = new ByteArrayOutputStream();
      InputStream encodedStream = getClass().getClassLoader().getResourceAsStream("vault-sample-wedjaa/test-vault.yml");
      VaultHandler.decrypt(encodedStream, decodedStream, TEST_WRONG_PASSWORD);
      new String(decodedStream.toByteArray());
      fail("Should not be able to decrypt text with the wrong password");

    }
    catch (Exception ex) {
      assertEquals(WRONG_PASS_EX, ex.getMessage());
    }
  }

}

@@ -892,17 +892,16 @@
 ctory;%0A%0A
-%0A
 public c
@@ -927,16 +927,24 @@
 st %7B%0A%0A  
+private 
 final st
@@ -985,24 +985,32 @@
  a test%22;%0A  
+private 
 final static
@@ -1043,24 +1043,32 @@
 assword%22;%0A  
+private 
 final static
@@ -1111,24 +1111,32 @@
 his_one%22;%0A  
+private 
 final static
@@ -1226,16 +1226,24 @@
 rd.%22;%0A  
+private 
 final st
@@ -1447,18 +1447,39 @@
 er%5Cn%22;%0A%0A
-
   
+private final static 
 Logger l

fix unit test