0

Hi I using java and testng for my test suite and allure report for reporting , issue is I am not able to figure out how to implement condition where

  1. Blacklist specific header value
  2. Logged response only if test case failed.

I am using Listners at class level

package testListnersT;

import com.aventstack.extentreports.markuputils.ExtentColor;
import com.aventstack.extentreports.markuputils.MarkupHelper;
import configurationAPI.BaseConfiguration;
import io.qameta.allure.Attachment;
import io.restassured.RestAssured;
import io.restassured.filter.log.LogDetail;
import io.restassured.filter.log.RequestLoggingFilter;
import io.restassured.filter.log.ResponseLoggingFilter;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

public class ListenersT extends BaseConfiguration implements ITestListener {

    private final ByteArrayOutputStream request = new ByteArrayOutputStream();

    private final ByteArrayOutputStream reponse = new ByteArrayOutputStream();

    private final PrintStream requestVar = new PrintStream(request, true);

    private final PrintStream responseVar = new PrintStream(reponse, true);


    public void onFinish(ITestContext arg0) {
        // TODO Auto-generated method stub

    }

    public void onStart(ITestContext arg0) {
        RestAssured.filters(new ResponseLoggingFilter(LogDetail.BODY, responseVar),
                new RequestLoggingFilter(LogDetail.BODY, requestVar));
    }

    public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) {
        // TODO Auto-generated method stub

    }

    public void onTestFailure(ITestResult result) {
        childTest.info(result.getThrowable());
        childTest.fail(MarkupHelper.createLabel("FAIL", ExtentColor.RED));
    }

    public void onTestSkipped(ITestResult arg0) {
        // TODO Auto-generated method stub
    }

    public void onTestStart(ITestResult arg0) {
        // TODO Auto-generated method stub

    }

    public void onTestSuccess(ITestResult arg0) {
        logRequest(request);
        logResponse(reponse);
    }


    public byte[] attach(final ByteArrayOutputStream log) {
        final byte[] array = log.toByteArray();
        log.reset();
        return array;
    }

    @Attachment(value = "request")
    public byte[] logRequest(final ByteArrayOutputStream stream) {
        return attach(stream);
    }

    @Attachment(value = "response")
    public byte[] logResponse(final ByteArrayOutputStream stream) {
        return attach(stream);
    }

}

Below is sample test code

 @Epic("Entity")
    @Story("Service")
    @Description("Test Desc")
    @Test(priority = 1, groups = {"testOne"})
    public void verifyTEST(Method method) {
    
        domainURL = env.apiBaseURL();
      
      
        body = JsonBuilderFactory.buildObject()
                .add("id", 12345)
               
                .getJson();

        response = given()
                .config(config.logConfig(LogConfig.logConfig().blacklistHeader("key")))
                .header("Content-Type", "application/json")
                .header("key", Key())
                .body(body.toString())
                .post(domainURL);
        assertThat(response.path("statusMsg").toString()).as("").contains("SUCCESS");
    }
0