Skip to content

Commit

Permalink
EPMRPP-90446 || Add new rerun attributes instead of rewriting (#1984)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanKustau authored May 2, 2024
1 parent 713d195 commit 14b483c
Showing 1 changed file with 37 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
import static com.epam.ta.reportportal.commons.querygen.constant.GeneralCriteriaConstant.CRITERIA_NAME;
import static com.epam.ta.reportportal.commons.querygen.constant.TestItemCriteriaConstant.CRITERIA_PARENT_ID;
import static com.epam.ta.reportportal.commons.querygen.constant.TestItemCriteriaConstant.CRITERIA_TEST_CASE_HASH;
import static com.epam.ta.reportportal.ws.converter.converters.ItemAttributeConverter.TO_LAUNCH_ATTRIBUTE;
import static java.util.Optional.ofNullable;

import com.epam.reportportal.rules.exception.ErrorType;
import com.epam.reportportal.rules.exception.ReportPortalException;
import com.epam.ta.reportportal.commons.ReportPortalUser;
import com.epam.ta.reportportal.commons.querygen.Condition;
import com.epam.ta.reportportal.commons.querygen.Filter;
Expand All @@ -36,22 +37,19 @@
import com.epam.ta.reportportal.core.item.validator.parent.ParentItemValidator;
import com.epam.ta.reportportal.dao.LaunchRepository;
import com.epam.ta.reportportal.dao.TestItemRepository;
import com.epam.ta.reportportal.entity.enums.LaunchModeEnum;
import com.epam.ta.reportportal.entity.enums.StatusEnum;
import com.epam.ta.reportportal.entity.item.TestItem;
import com.epam.ta.reportportal.entity.launch.Launch;
import com.epam.reportportal.rules.exception.ReportPortalException;
import com.epam.ta.reportportal.ws.converter.builders.LaunchBuilder;
import com.epam.ta.reportportal.ws.converter.builders.TestCaseIdEntry;
import com.epam.ta.reportportal.ws.converter.builders.TestItemBuilder;
import com.epam.reportportal.rules.exception.ErrorType;
import com.epam.ta.reportportal.ws.reporting.StartTestItemRQ;
import com.epam.ta.reportportal.ws.reporting.ItemCreatedRS;
import com.epam.ta.reportportal.ws.reporting.StartLaunchRQ;
import com.epam.ta.reportportal.ws.reporting.StartTestItemRQ;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
Expand All @@ -75,9 +73,8 @@ public class RerunHandlerImpl implements RerunHandler {

@Autowired
public RerunHandlerImpl(TestItemRepository testItemRepository, LaunchRepository launchRepository,
UniqueIdGenerator uniqueIdGenerator,
TestCaseHashGenerator testCaseHashGenerator, ApplicationEventPublisher eventPublisher,
RerunSearcher rerunSearcher,
UniqueIdGenerator uniqueIdGenerator, TestCaseHashGenerator testCaseHashGenerator,
ApplicationEventPublisher eventPublisher, RerunSearcher rerunSearcher,
List<ParentItemValidator> parentItemValidators, RetryHandler retryHandler) {
this.testItemRepository = testItemRepository;
this.launchRepository = launchRepository;
Expand All @@ -94,20 +91,20 @@ public Launch handleLaunch(StartLaunchRQ request, Long projectId, ReportPortalUs
Optional<Launch> launchOptional = StringUtils.isEmpty(request.getRerunOf()) ?
launchRepository.findLatestByNameAndProjectId(request.getName(), projectId) :
launchRepository.findByUuid(request.getRerunOf());
Launch launch = launchOptional.orElseThrow(
Launch existingLaunch = launchOptional.orElseThrow(
() -> new ReportPortalException(ErrorType.LAUNCH_NOT_FOUND,
ofNullable(request.getRerunOf()).orElse(request.getName())
));

ofNullable(request.getMode()).map(it -> LaunchModeEnum.valueOf(it.name()))
.ifPresent(launch::setMode);
ofNullable(request.getDescription()).ifPresent(launch::setDescription);
LaunchBuilder launchBuilder =
new LaunchBuilder(existingLaunch).addDescription(request.getDescription())
.addMode(request.getMode()).addAttributes(request.getAttributes());

Launch launch = launchBuilder.get();
launch.setStatus(StatusEnum.IN_PROGRESS);
ofNullable(request.getAttributes()).map(it -> it.stream()
.map(attr -> TO_LAUNCH_ATTRIBUTE.apply(attr, launch))
.collect(Collectors.toSet())).ifPresent(launch::setAttributes);
ofNullable(request.getUuid()).ifPresent(launch::setUuid);
launch.setRerun(true);
ofNullable(request.getUuid()).ifPresent(launch::setUuid);

return launch;
}

Expand All @@ -125,7 +122,8 @@ private Integer getTestCaseHash(StartTestItemRQ request, Launch launch) {
.orElseGet(() -> {
TestItem newItem = new TestItemBuilder().addStartItemRequest(request).get();
return testCaseHashGenerator.generate(newItem, Collections.emptyList(),
launch.getProjectId());
launch.getProjectId()
);
});
}

Expand All @@ -139,29 +137,27 @@ public Optional<ItemCreatedRS> handleChildItem(StartTestItemRQ request, Launch l
final Pair<Long, String> pathName = testItemRepository.selectPath(parentUuid)
.orElseThrow(() -> new ReportPortalException(ErrorType.TEST_ITEM_NOT_FOUND, parentUuid));

TestItem newItem = new TestItemBuilder().addLaunchId(launch.getId())
.addStartItemRequest(request)
.addAttributes(request.getAttributes())
.addParentId(pathName.getFirst())
.get();
TestItem newItem =
new TestItemBuilder().addLaunchId(launch.getId()).addStartItemRequest(request)
.addAttributes(request.getAttributes()).addParentId(pathName.getFirst()).get();

if (Objects.isNull(newItem.getTestCaseId())) {
newItem.setTestCaseHash(testCaseHashGenerator.generate(newItem,
IdentityUtil.getItemTreeIds(pathName.getSecond()),
launch.getProjectId()
));
newItem.setTestCaseHash(
testCaseHashGenerator.generate(newItem, IdentityUtil.getItemTreeIds(pathName.getSecond()),
launch.getProjectId()
));
}

final Filter childItemFilter = getChildItemFilter(launch, newItem.getTestCaseHash(),
pathName.getFirst());
final Filter childItemFilter =
getChildItemFilter(launch, newItem.getTestCaseHash(), pathName.getFirst());

return rerunSearcher.findItem(childItemFilter).flatMap(testItemRepository::findById)
.flatMap(foundItem -> {
if (!foundItem.isHasChildren()) {
final TestItem parent = testItemRepository.findIdByUuidForUpdate(parentUuid)
.map(testItemRepository::getOne)
.orElseThrow(
() -> new ReportPortalException(ErrorType.TEST_ITEM_NOT_FOUND, parentUuid));
final TestItem parent =
testItemRepository.findIdByUuidForUpdate(parentUuid).map(testItemRepository::getOne)
.orElseThrow(
() -> new ReportPortalException(ErrorType.TEST_ITEM_NOT_FOUND, parentUuid));
parentItemValidators.forEach(v -> v.validate(request, parent));
return Optional.of(handleRetry(launch, newItem, foundItem, parent));
}
Expand All @@ -178,31 +174,22 @@ public Optional<ItemCreatedRS> handleChildItem(StartTestItemRQ request, Launch l
}

private Filter getCommonFilter(Long launchId, Integer testCaseHash) {
return Filter.builder()
.withTarget(TestItem.class)
.withCondition(new FilterCondition(Condition.EQUALS, false, String.valueOf(launchId),
CRITERIA_LAUNCH_ID))
return Filter.builder().withTarget(TestItem.class).withCondition(
new FilterCondition(Condition.EQUALS, false, String.valueOf(launchId), CRITERIA_LAUNCH_ID))
.withCondition(new FilterCondition(Condition.EQUALS, false, String.valueOf(testCaseHash),
CRITERIA_TEST_CASE_HASH))
.build();
CRITERIA_TEST_CASE_HASH
)).build();
}

private Filter getRootItemFilter(Launch launch, Integer testCaseHash, String name) {
return getCommonFilter(launch.getId(), testCaseHash).withCondition(
new FilterCondition(Condition.EQUALS,
false,
name,
CRITERIA_NAME
)).withCondition(new FilterCondition(Condition.EXISTS, true, "1", CRITERIA_PARENT_ID));
new FilterCondition(Condition.EQUALS, false, name, CRITERIA_NAME))
.withCondition(new FilterCondition(Condition.EXISTS, true, "1", CRITERIA_PARENT_ID));
}

private Filter getChildItemFilter(Launch launch, Integer testCaseHash, Long parentId) {
return getCommonFilter(launch.getId(), testCaseHash).withCondition(
new FilterCondition(Condition.EQUALS,
false,
String.valueOf(parentId),
CRITERIA_PARENT_ID
));
new FilterCondition(Condition.EQUALS, false, String.valueOf(parentId), CRITERIA_PARENT_ID));
}

private ItemCreatedRS handleRetry(Launch launch, TestItem newItem, TestItem foundItem,
Expand All @@ -224,9 +211,7 @@ private void generateUniqueId(Launch launch, TestItem item) {

private ItemCreatedRS updateRootItem(StartTestItemRQ request, TestItem foundItem) {
foundItem = new TestItemBuilder(foundItem).addDescription(request.getDescription())
.overwriteAttributes(request.getAttributes())
.addStatus(StatusEnum.IN_PROGRESS)
.get();
.overwriteAttributes(request.getAttributes()).addStatus(StatusEnum.IN_PROGRESS).get();
ofNullable(request.getUuid()).ifPresent(foundItem::setUuid);
return new ItemCreatedRS(foundItem.getUuid(), foundItem.getUniqueId());
}
Expand Down

0 comments on commit 14b483c

Please sign in to comment.