Skip to content

Commit

Permalink
EPMRPP-68417 || receive cluster items mock impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan_Budayeu authored and Ivan_Budayeu committed Nov 9, 2021
1 parent 185ed25 commit 4cd0182
Show file tree
Hide file tree
Showing 7 changed files with 301 additions and 9 deletions.
3 changes: 2 additions & 1 deletion codecov.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ ignore:
- "**/exception/**"
- "**/job/FlushingDataJob**"
- "**/core/analyzer/auto/client/model/**"
- "**/core/analyzer/auto/impl/SuggestedItem**"
- "**/core/analyzer/auto/impl/SuggestedItem**"
- "**/core/item/impl/provider/impl/mock/**"
3 changes: 2 additions & 1 deletion project-properties.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ project.ext {
'**/demodata/**',
'**/exception/**',
'**/job/FlushingDataJob**',
"**/core/analyzer/auto/client/model/**"
"**/core/analyzer/auto/client/model/**",
'**/core/item/impl/provider/impl/mock/**'
]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public enum DataProviderType {

WIDGET_BASED("widget"),
LAUNCH_BASED("launch"),
FILTER_BASED("filter");
FILTER_BASED("filter"),
CLUSTER_BASED("cluster");

private final String type;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
package com.epam.ta.reportportal.core.item.impl.provider;

import com.epam.ta.reportportal.core.item.impl.provider.impl.CumulativeTestItemDataProviderImpl;
import com.epam.ta.reportportal.core.item.impl.provider.impl.DelegatingClusterDataProviderHandler;
import com.epam.ta.reportportal.core.item.impl.provider.impl.LaunchDataProviderHandlerImpl;
import com.epam.ta.reportportal.core.item.impl.provider.impl.MaterializedWidgetProviderHandlerImpl;
import com.epam.ta.reportportal.core.item.impl.provider.impl.mock.ClusterItemDataProviderMock;
import com.epam.ta.reportportal.entity.widget.WidgetType;
import com.google.common.collect.ImmutableMap;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -42,21 +45,28 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
this.applicationContext = applicationContext;
}

@Bean
public DelegatingClusterDataProviderHandler delegatingClusterDataProviderHandler(
@Value("${rp.environment.variable.cluster.item.page-size}") Integer maxPageSize,
@Autowired ClusterItemDataProviderMock clusterItemDataProviderMock) {
return new DelegatingClusterDataProviderHandler(maxPageSize, clusterItemDataProviderMock);
}

@Bean("testItemDataProviders")
public Map<DataProviderType, DataProviderHandler> testItemDataProviders() {
return ImmutableMap.<DataProviderType, DataProviderHandler>builder().put(DataProviderType.WIDGET_BASED,
applicationContext.getBean(MaterializedWidgetProviderHandlerImpl.class)
)
return ImmutableMap.<DataProviderType, DataProviderHandler>builder()
.put(DataProviderType.WIDGET_BASED, applicationContext.getBean(MaterializedWidgetProviderHandlerImpl.class))
.put(DataProviderType.LAUNCH_BASED, applicationContext.getBean(LaunchDataProviderHandlerImpl.class))
.put(DataProviderType.FILTER_BASED, applicationContext.getBean(FilterDataProviderImpl.class))
.put(DataProviderType.CLUSTER_BASED,applicationContext.getBean(DelegatingClusterDataProviderHandler.class))
.build();
}

@Bean("testItemWidgetDataProviders")
public Map<WidgetType, DataProviderHandler> testItemWidgetDataProviders() {
return ImmutableMap.<WidgetType, DataProviderHandler>builder().put(WidgetType.CUMULATIVE,
applicationContext.getBean(CumulativeTestItemDataProviderImpl.class)
).build();
return ImmutableMap.<WidgetType, DataProviderHandler>builder()
.put(WidgetType.CUMULATIVE, applicationContext.getBean(CumulativeTestItemDataProviderImpl.class))
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2021 EPAM Systems
*
* 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 com.epam.ta.reportportal.core.item.impl.provider.impl;

import com.epam.ta.reportportal.commons.ReportPortalUser;
import com.epam.ta.reportportal.commons.querygen.Queryable;
import com.epam.ta.reportportal.commons.validation.BusinessRule;
import com.epam.ta.reportportal.core.item.impl.provider.DataProviderHandler;
import com.epam.ta.reportportal.entity.item.TestItem;
import com.epam.ta.reportportal.entity.statistics.Statistics;
import com.epam.ta.reportportal.ws.model.ErrorType;
import org.apache.commons.lang3.BooleanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.Map;
import java.util.Set;

/**
* @author <a href="mailto:[email protected]">Ivan Budayeu</a>
*/
public class DelegatingClusterDataProviderHandler implements DataProviderHandler {

public static final String CLUSTER_ID_PARAM = "clusterId";

private final Integer maxPageSize;
private final DataProviderHandler delegate;

public DelegatingClusterDataProviderHandler(Integer maxPageSize, DataProviderHandler dataProviderHandler) {
this.maxPageSize = maxPageSize;
this.delegate = dataProviderHandler;
}

@Override
public Page<TestItem> getTestItems(Queryable filter, Pageable pageable, ReportPortalUser.ProjectDetails projectDetails,
ReportPortalUser user, Map<String, String> params) {
validateClusterCondition(filter);
validatePageSize(pageable);
return delegate.getTestItems(filter, pageable, projectDetails, user, params);
}

@Override
public Set<Statistics> accumulateStatistics(Queryable filter, ReportPortalUser.ProjectDetails projectDetails, ReportPortalUser user,
Map<String, String> params) {
validateClusterCondition(filter);
return delegate.accumulateStatistics(filter, projectDetails, user, params);
}

private void validateClusterCondition(Queryable filter) {
final boolean hasClusterIdCondition = filter.getFilterConditions()
.stream()
.flatMap(c -> c.getAllConditions().stream())
.anyMatch(c -> c.getSearchCriteria().contains(CLUSTER_ID_PARAM));
BusinessRule.expect(hasClusterIdCondition, BooleanUtils::isTrue)
.verify(ErrorType.BAD_REQUEST_ERROR, "Cluster id condition not provided");
}

private void validatePageSize(Pageable pageable) {
BusinessRule.expect(pageable.getPageSize(), pageSize -> pageSize <= maxPageSize)
.verify(ErrorType.BAD_REQUEST_ERROR, "Max page size: " + maxPageSize);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
/*
* Copyright 2021 EPAM Systems
*
* 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 com.epam.ta.reportportal.core.item.impl.provider.impl.mock;

import com.epam.ta.reportportal.commons.ReportPortalUser;
import com.epam.ta.reportportal.commons.querygen.Queryable;
import com.epam.ta.reportportal.core.item.impl.provider.DataProviderHandler;
import com.epam.ta.reportportal.entity.ItemAttribute;
import com.epam.ta.reportportal.entity.enums.StatusEnum;
import com.epam.ta.reportportal.entity.enums.TestItemIssueGroup;
import com.epam.ta.reportportal.entity.enums.TestItemTypeEnum;
import com.epam.ta.reportportal.entity.item.Parameter;
import com.epam.ta.reportportal.entity.item.TestItem;
import com.epam.ta.reportportal.entity.item.TestItemResults;
import com.epam.ta.reportportal.entity.item.issue.IssueEntity;
import com.epam.ta.reportportal.entity.item.issue.IssueGroup;
import com.epam.ta.reportportal.entity.item.issue.IssueType;
import com.epam.ta.reportportal.entity.statistics.Statistics;
import com.epam.ta.reportportal.entity.statistics.StatisticsField;
import com.google.common.base.Suppliers;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static java.util.stream.Collectors.groupingBy;

/**
* @author <a href="mailto:[email protected]">Ivan Budayeu</a>
*/
@Service
public class ClusterItemDataProviderMock implements DataProviderHandler {

private final Supplier<List<TestItem>> itemSupplier = Suppliers.memoize(this::getItems);

@Override
public Page<TestItem> getTestItems(Queryable filter, Pageable pageable, ReportPortalUser.ProjectDetails projectDetails,
ReportPortalUser user, Map<String, String> params) {
final List<TestItem> testItems = itemSupplier.get();
final List<TestItem> content = testItems.stream()
.skip(pageable.getOffset())
.limit(pageable.getPageSize())
.collect(Collectors.toList());
return new PageImpl<>(content, pageable, testItems.size());
}

@Override
public Set<Statistics> accumulateStatistics(Queryable filter, ReportPortalUser.ProjectDetails projectDetails, ReportPortalUser user,
Map<String, String> params) {
final List<TestItem> testItems = itemSupplier.get();

return testItems.stream()
.map(TestItem::getItemResults)
.flatMap(r -> r.getStatistics().stream())
.collect(groupingBy(Statistics::getStatisticsField, LinkedHashMap::new, Collectors.summingInt(Statistics::getCounter)))
.entrySet()
.stream()
.map(entry -> new Statistics(entry.getKey(), entry.getValue()))
.collect(Collectors.toCollection(LinkedHashSet::new));
}

private List<TestItem> getItems() {
return IntStream.range(1, 21).mapToObj(this::getTestItem).collect(Collectors.toList());
}

private TestItem getTestItem(int index) {
final TestItem testItem = new TestItem();
testItem.setItemId((long) index);
testItem.setUuid(String.valueOf(index));
testItem.setUniqueId(String.valueOf(index));
testItem.setTestCaseId(String.valueOf(index));
testItem.setTestCaseHash(index);
testItem.setName("name " + index);
testItem.setCodeRef("ref" + index);
testItem.setDescription("description " + index);
testItem.setHasChildren(false);
testItem.setType(TestItemTypeEnum.STEP);
testItem.setHasRetries(false);
testItem.setHasStats(true);
testItem.setLastModified(LocalDateTime.now(ZoneOffset.UTC));
testItem.setPath(String.valueOf(index));
testItem.setStartTime(LocalDateTime.now(ZoneOffset.UTC));

final Set<Parameter> parameters = getParameters(index);
testItem.setParameters(parameters);

final Set<ItemAttribute> attributes = getItemAttributes(index);
testItem.setAttributes(attributes);

final TestItemResults testItemResults = getTestItemResults((long) index);

testItem.setItemResults(testItemResults);

return testItem;
}

private Set<Parameter> getParameters(int index) {
final Parameter parameter = new Parameter();
parameter.setKey("param key " + index);
parameter.setValue("param value " + index);
return Set.of(parameter);
}

private Set<ItemAttribute> getItemAttributes(int index) {
final ItemAttribute itemAttribute = new ItemAttribute();
itemAttribute.setKey("key" + index);
itemAttribute.setValue("value" + index);
itemAttribute.setSystem(false);

return Set.of(itemAttribute);
}

private TestItemResults getTestItemResults(Long index) {
final TestItemResults testItemResults = new TestItemResults();
testItemResults.setDuration(0.01);
testItemResults.setEndTime(LocalDateTime.now(ZoneOffset.UTC));
testItemResults.setStatus(StatusEnum.FAILED);

final IssueEntity issueEntity = getIssueEntity(index);

testItemResults.setIssue(issueEntity);

final LinkedHashSet<Statistics> statistics = getStatistics();

testItemResults.setStatistics(statistics);
return testItemResults;
}

private IssueEntity getIssueEntity(Long index) {
final IssueEntity issueEntity = new IssueEntity();
issueEntity.setIssueId(index);
issueEntity.setIssueDescription("description " + index);
issueEntity.setAutoAnalyzed(false);
issueEntity.setIgnoreAnalyzer(false);

final IssueType issueType = getIssueType();

issueEntity.setIssueType(issueType);
return issueEntity;
}

private IssueType getIssueType() {
final IssueType issueType = new IssueType();
issueType.setId(1L);
issueType.setLocator("ti001");
issueType.setHexColor("#ffb743");
issueType.setLongName("To Investigate");
issueType.setShortName("TI");

final IssueGroup issueGroup = getIssueGroup();
issueType.setIssueGroup(issueGroup);
return issueType;
}

private IssueGroup getIssueGroup() {
final IssueGroup issueGroup = new IssueGroup();
issueGroup.setId(1);
issueGroup.setTestItemIssueGroup(TestItemIssueGroup.TO_INVESTIGATE);
return issueGroup;
}

private LinkedHashSet<Statistics> getStatistics() {
return Map.of(1L,
"statistics$executions$total",
2L,
"statistics$executions$passed",
3L,
"statistics$executions$skipped",
4L,
"statistics$executions$failed",
12L,
"statistics$defects$to_investigate$ti001"
).entrySet().stream().map(entry -> {
final StatisticsField sf = new StatisticsField(entry.getValue());
sf.setId(entry.getKey());
return new Statistics(sf, 1);
}).collect(Collectors.toCollection(LinkedHashSet::new));
}
}
3 changes: 3 additions & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ rp:
clean:
items:
size: 500
cluster:
item:
page-size: 10
log-index:
batch-size: 20
pattern-analysis:
Expand Down

0 comments on commit 4cd0182

Please sign in to comment.