/*
 * Copyright (c) 2014 the original author or authors
 *
 * 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.
 */
package io.werval.test;

import io.werval.api.Config;
import io.werval.api.Errors;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Field;

import static io.werval.runtime.ConfigKeys.WERVAL_HTTP_ADDRESS;
import static io.werval.runtime.ConfigKeys.WERVAL_HTTP_PORT;

/**
 * Internal Werval Test Helper.
 */
/* package */ final class WervalTestHelper
{
    /**
     * Print Errors traces to STDERR.
     *
     * @param errors Application's Errors
     */
    /* package */ static void printErrorsTrace( Errors errors )
    {
        if( errors.count() > 0 )
        {
            StringWriter writer = new StringWriter();
            PrintWriter pw = new PrintWriter( writer );
            pw.append( String.valueOf( errors.count() ) )
                .append( " Errors recorded by the Werval Application during the test:\n" );
            for( io.werval.api.Error error : errors.asList() )
            {
                pw.append( "\n" )
                    .append( "Error ID  :" ).append( error.errorId() ).append( "\n" )
                    .append( "Request ID:" ).append( error.requestId() ).append( "\n" )
                    .append( "Timestamp :" ).append( String.valueOf( error.timestamp() ) ).append( "\n" )
                    .append( "Message   :" ).append( error.message() ).append( "\n" )
                    .append( "Cause     :" ).append( error.cause().getMessage() ).append( "\n" );
                error.cause().printStackTrace( pw );
            }
            pw.flush();
            System.err.println( writer.toString() );
        }
    }

    /* package */ static void setupRestAssuredDefaults( Config config )
    {
        // Setup RestAssured defaults if present
        try
        {
            Field restAssuredPortField = Class.forName( "com.jayway.restassured.RestAssured" ).getField( "port" );
            restAssuredPortField.set( null, config.intNumber( WERVAL_HTTP_PORT ) );
            Field restAssuredBaseURLField = Class.forName( "com.jayway.restassured.RestAssured" ).getField( "baseURL" );
            restAssuredBaseURLField.set( null, "http://" + config.string( WERVAL_HTTP_ADDRESS ) );
        }
        catch( ClassNotFoundException | NoSuchFieldException |
               IllegalArgumentException | IllegalAccessException noRestAssured )
        {
            // RestAssured is not present, we simply don't configure it.
        }
    }

    private WervalTestHelper()
    {
    }
}

@@ -776,21 +776,41 @@
 reflect.
-Field
+InvocationTargetException
 ;%0A%0Aimpor
@@ -2354,16 +2354,46 @@
 present%0A
+        Class%3C?%3E restAssured;%0A
         
@@ -2410,38 +2410,32 @@
   %7B%0A            
-Field 
 restAssuredPortF
@@ -2429,25 +2429,16 @@
 tAssured
-PortField
  = Class
@@ -2489,28 +2489,83 @@
 d%22 )
-.getField( %22port%22 );
+;%0A        %7D%0A        catch( ClassNotFoundException noRestAssured )%0A        %7B
 %0A   
@@ -2565,33 +2565,36 @@
   %7B%0A            
-r
+// R
 estAssuredPortFi
@@ -2591,68 +2591,54 @@
 ured
-PortField.set( null, config.intNumber( WERVAL_HTTP_PORT ) );
+ is not present, we simply don't configure it.
 %0A   
@@ -2646,22 +2646,16 @@
         
-Field 
 restAssu
@@ -2661,63 +2661,118 @@
 ured
-BaseURLField = Class.forName( %22com.jayway.
+ = null;%0A        %7D%0A        if( 
 rest
-a
+A
 ssured
-.R
+ != null )%0A        %7B%0A            try%0A            %7B%0A                r
 estA
@@ -2777,19 +2777,16 @@
 tAssured
-%22 )
 .getFiel
@@ -2793,16 +2793,63 @@
 d( %22
-baseURL%22
+port%22 ).set( null, config.intNumber( WERVAL_HTTP_PORT )
  );%0A
@@ -2852,32 +2852,36 @@
  );%0A            
+ 
+   
 restAssuredBaseU
@@ -2879,20 +2879,30 @@
 ured
-BaseURLField
+.getField( %22baseURI%22 )
 .set
@@ -2961,34 +2961,152 @@
 SS ) );%0A        
-%7D%0A
+        restAssured.getMethod( %22enableLoggingOfRequestAndResponseIfValidationFails%22 ).invoke( null );%0A            %7D%0A    
         catch( C
@@ -3100,36 +3100,34 @@
      catch( 
-ClassNotFoun
+NoSuchFiel
 dException %7C
@@ -3129,28 +3129,29 @@
 ion %7C NoSuch
-Fiel
+Metho
 dException %7C
@@ -3150,17 +3150,49 @@
 eption %7C
-%0A
+ InvocationTargetException %7C%0A    
         
@@ -3248,32 +3248,25 @@
 ception 
-noRestAssured )%0A
+ex )%0A    
         
@@ -3283,68 +3283,129 @@
     
-// RestAssured is not present, we simply don't configure it.
+    System.err.println( %22Unable to setup rest-assured%22 );%0A                ex.printStackTrace( System.err );%0A            %7D
 %0A   

Test Support: enable request/response logging in rest-assured on failure