Skip to content

Commit

Permalink
ReportPortalHook: Tests WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
HardNorth committed Jan 8, 2024
1 parent b00232d commit 2b99260
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 34 deletions.
19 changes: 6 additions & 13 deletions src/main/java/com/epam/reportportal/karate/ReportPortalHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ protected StartTestItemRQ buildStartFeatureRq(@Nonnull FeatureRuntime fr) {

@Override
public boolean beforeFeature(FeatureRuntime fr) {
StartTestItemRQ rq = buildStartFeatureRq(fr);
Maybe<String> featureId = launch.get().startTestItem(rq);
Maybe<String> featureId = launch.get().startTestItem(buildStartFeatureRq(fr));
Feature feature = fr.featureCall.feature;
featureIdMap.put(feature.getNameForReport(), featureId);
return true;
Expand Down Expand Up @@ -267,7 +266,7 @@ public void finishBackground(@Nullable Step step, @Nonnull ScenarioRuntime sr) {

@Override
public void afterScenario(ScenarioRuntime sr) {
Maybe<String> scenarioId = featureIdMap.remove(sr.scenario.getUniqueId());
Maybe<String> scenarioId = scenarioIdMap.remove(sr.scenario.getUniqueId());
if (scenarioId == null) {
LOGGER.error("ERROR: Trying to finish unspecified scenario.");
}
Expand Down Expand Up @@ -352,6 +351,10 @@ public boolean beforeStep(Step step, ScenarioRuntime sr) {
ofNullable(step.getTable())
.ifPresent(table ->
sendLog(stepId, "Table:\n\n" + formatDataTable(table.getRows()), LogLevel.INFO));
String docString = step.getDocString();
if (isNotBlank(docString)) {
sendLog(stepId, "Docstring:\n\n" + asMarkdownCode(step.getDocString()), LogLevel.INFO);
}
return true;
}

Expand All @@ -364,22 +367,12 @@ public boolean beforeStep(Step step, ScenarioRuntime sr) {
public void sendStepResults(StepResult stepResult, ScenarioRuntime sr) {
Maybe<String> stepId = stepIdMap.get(sr.scenario.getUniqueId());
Step step = stepResult.getStep();
String docString = step.getDocString();
if (isNotBlank(docString)) {
sendLog(stepId, "Docstring:\n\n" + asMarkdownCode(step.getDocString()), LogLevel.INFO);
}

Result result = stepResult.getResult();
String stepLog = stepResult.getStepLog();
if (isNotBlank(stepLog)) {
sendLog(stepId, stepLog, LogLevel.INFO);
}
if (result.isFailed()) {
String fullErrorMessage = step.getPrefix() + " " + step.getText();
String errorMessage = result.getErrorMessage();
if (isNotBlank(errorMessage)) {
fullErrorMessage = fullErrorMessage + "\n" + errorMessage;

}
sendLog(stepId, fullErrorMessage, LogLevel.ERROR);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,8 @@ protected StartTestItemRQ buildStartStepRq(@Nonnull StepResult stepResult, @Nonn
* @param scenarioResult scenario result
*/
public void startStep(StepResult stepResult, ScenarioResult scenarioResult) {
if (stepResult.getStep().isBackground()) {
Step step = stepResult.getStep();
if (step.isBackground()) {
startBackground(stepResult, scenarioResult);
} else {
finishBackground(stepResult, scenarioResult);
Expand All @@ -356,9 +357,13 @@ public void startStep(StepResult stepResult, ScenarioResult scenarioResult) {
.ifPresent(params ->
sendLog(stepId, String.format(PARAMETERS_PATTERN, formatParametersAsTable(params)),
LogLevel.INFO));
ofNullable(stepResult.getStep().getTable())
ofNullable(step.getTable())
.ifPresent(table ->
sendLog(stepId, "Table:\n\n" + formatDataTable(table.getRows()), LogLevel.INFO));
String docString = step.getDocString();
if (isNotBlank(docString)) {
sendLog(stepId, "Docstring:\n\n" + asMarkdownCode(step.getDocString()), LogLevel.INFO);
}
}

/**
Expand Down Expand Up @@ -409,15 +414,10 @@ protected void sendLog(Maybe<String> itemId, String message, LogLevel level) {
*/
public void sendStepResults(StepResult stepResult) {
Step step = stepResult.getStep();
String docString = step.getDocString();
if (isNotBlank(docString)) {
sendLog(stepId, "Docstring:\n\n" + asMarkdownCode(step.getDocString()), LogLevel.INFO);
}

Result result = stepResult.getResult();
String stepLog = stepResult.getStepLog();
if (isNotBlank(stepLog)) {
sendLog(stepId, stepLog, LogLevel.INFO);
sendLog(stepId, stepLog, LogLevel.DEBUG);
}
if (result.isFailed()) {
String fullErrorMessage = step.getPrefix() + " " + step.getText();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import com.epam.reportportal.service.ReportPortalClient;
import com.epam.reportportal.util.test.CommonUtils;
import com.epam.ta.reportportal.ws.model.log.SaveLogRQ;
import com.intuit.karate.Results;
import okhttp3.MultipartBody;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;

import java.util.List;
Expand All @@ -37,6 +39,7 @@
import static org.mockito.Mockito.*;

public class HttpRequestLoggingTest {
private static final String TEST_FEATURE = "classpath:feature/http_request.feature";
private final String launchUuid = CommonUtils.namedId("launch_");
private final String featureId = CommonUtils.namedId("feature_");
private final String scenarioId = CommonUtils.namedId("scenario_");
Expand All @@ -52,10 +55,16 @@ public void setupMock() {
mockBatchLogging(client);
}

@Test
@ParameterizedTest
@ValueSource(booleans = {true, false})
@SuppressWarnings({"unchecked", "rawtypes"})
public void test_http_request_logging() {
var results = TestUtils.runAsReport(rp, "classpath:feature/http_request.feature");
public void test_http_request_logging(boolean report) {
Results results;
if (report) {
results = TestUtils.runAsReport(rp, TEST_FEATURE);
} else {
results = TestUtils.runAsHook(rp, TEST_FEATURE);
}
assertThat(results.getFailCount(), equalTo(0));

ArgumentCaptor<List> logCaptor = ArgumentCaptor.forClass(List.class);
Expand All @@ -64,12 +73,12 @@ public void test_http_request_logging() {
.getAllValues().
stream()
.flatMap(rq -> extractJsonParts((List<MultipartBody.Part>) rq).stream())
.filter(rq -> LogLevel.INFO.name().equals(rq.getLevel()))
.filter(rq -> LogLevel.INFO.name().equals(rq.getLevel()) || LogLevel.DEBUG.name().equals(rq.getLevel()))
.collect(Collectors.toList());

assertThat(logs, hasSize(2));
assertThat(logs, hasSize(greaterThanOrEqualTo(2)));
List<String> messages = logs.stream().map(SaveLogRQ::getMessage).collect(Collectors.toList());
assertThat(messages, containsInAnyOrder(
assertThat(messages, hasItems(
equalTo("Docstring:\n\n```\n{\n" +
" username: 'user',\n" +
" password: 'password',\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
import com.epam.reportportal.service.ReportPortalClient;
import com.epam.reportportal.util.test.CommonUtils;
import com.epam.ta.reportportal.ws.model.log.SaveLogRQ;
import com.intuit.karate.Results;
import okhttp3.MultipartBody;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;

import java.util.List;
Expand All @@ -37,6 +39,7 @@
import static org.mockito.Mockito.*;

public class SimpleFailureLoggingTest {
private static final String TEST_FEATURE = "classpath:feature/simple_failed.feature";
private final String launchUuid = CommonUtils.namedId("launch_");
private final String featureId = CommonUtils.namedId("feature_");
private final String scenarioId = CommonUtils.namedId("scenario_");
Expand All @@ -52,10 +55,16 @@ public void setupMock() {
mockBatchLogging(client);
}

@Test
@ParameterizedTest
@ValueSource(booleans = {true, false})
@SuppressWarnings({"unchecked", "rawtypes"})
public void test_simple_one_step_failed_error_log() {
var results = TestUtils.runAsReport(rp, "classpath:feature/simple_failed.feature");
public void test_simple_one_step_failed_error_log(boolean report) {
Results results;
if (report) {
results = TestUtils.runAsReport(rp, TEST_FEATURE);
} else {
results = TestUtils.runAsHook(rp, TEST_FEATURE);
}
assertThat(results.getFailCount(), equalTo(1));

ArgumentCaptor<List> logCaptor = ArgumentCaptor.forClass(List.class);
Expand All @@ -67,8 +76,8 @@ public void test_simple_one_step_failed_error_log() {
.filter(rq -> LogLevel.ERROR.name().equals(rq.getLevel()))
.collect(Collectors.toList());

assertThat(logs, hasSize(1));
SaveLogRQ log = logs.get(0);
assertThat(logs, hasSize(greaterThan(0)));
SaveLogRQ log = logs.get(logs.size() - 1);
assertThat(log.getItemUuid(), oneOf(stepIds.toArray(new String[0])));
assertThat(log.getLaunchUuid(), equalTo(launchUuid));
assertThat(log.getMessage(), equalTo("Then assert actualFour != four\n"
Expand Down

0 comments on commit 2b99260

Please sign in to comment.