use of com.epam.ta.reportportal.exception.ReportPortalException in project commons-dao by reportportal.
the class ReportPortalRepositoryImpl method getPageNumber.
@Override
public long getPageNumber(String entityId, Queryable filterable, Pageable pageable) {
ImmutableList.Builder<AggregationOperation> pipelineBuilder = ImmutableList.<AggregationOperation>builder().add(matchOperationFromFilter(filterable, mongoOperations, this.getEntityInformation().getJavaType()));
if (null != pageable.getSort()) {
pipelineBuilder.add(/* sort results as requested */
sort(pageable.getSort()));
}
pipelineBuilder.add(/* group items into one field pushing all results into one array */
group("result").push("$_id").as("array"), /* unwind array adding index to each result */
unwind("array", "ind", false), /* find needed item. How we know its index in query result */
match(where("array").is(ObjectId.isValid(entityId) ? new ObjectId(entityId) : entityId)), /* grab index only */
project("ind"));
/* find items matching an provided filter */
Aggregation a = Aggregation.newAggregation(toArray(pipelineBuilder.build(), AggregationOperation.class));
final AggregationResults<Map> aggregate = mongoOperations.aggregate(a, getEntityInformation().getCollectionName(), Map.class);
if (!aggregate.getUniqueMappedResult().containsKey("ind")) {
throw new ReportPortalException(ErrorType.INCORRECT_FILTER_PARAMETERS, "Unable to calculate page number. Check your input parameters");
}
/* result returned as long. Calculate page number */
return (long) Math.ceil((((Long) aggregate.getUniqueMappedResult().get("ind")).doubleValue() + 1d) / (double) pageable.getPageSize());
}
use of com.epam.ta.reportportal.exception.ReportPortalException in project commons-dao by reportportal.
the class CriteriaHolder method castValue.
/**
* Casting provided criteriaHolder by specified {@link Class} for specified value.
* <p>
* NOTE:<br>
* errorType - error which should be thrown when unable cast value
*
* @param oneValue Value to cast
* @param errorType ErrorType in case of error
* @return Casted value
*/
public Object castValue(String oneValue, ErrorType errorType) {
Object castedValue;
if (Number.class.isAssignableFrom(getDataType())) {
/* Verify correct number */
Long parsedLong = NumberUtils.toLong(oneValue, -1);
BusinessRule.expect(parsedLong, FilterRules.numberIsPositive()).verify(errorType, Suppliers.formattedSupplier("Cannot convert '{}' to valid positive number", oneValue));
castedValue = parsedLong;
} else if (Date.class.isAssignableFrom(getDataType())) {
/* Verify correct date */
BusinessRule.expect(oneValue, FilterRules.dateInMillis()).verify(errorType, Suppliers.formattedSupplier("Cannot convert '{}' to valid date", oneValue));
castedValue = new Date(Long.parseLong(oneValue));
} else if (boolean.class.equals(getDataType()) || Boolean.class.isAssignableFrom(getDataType())) {
castedValue = BooleanUtils.toBoolean(oneValue);
} else if (LogLevel.class.isAssignableFrom(getDataType())) {
castedValue = LogLevel.toLevel(oneValue);
BusinessRule.expect(castedValue, Predicates.notNull()).verify(errorType, Suppliers.formattedSupplier("Cannot convert '{}' to valid 'LogLevel'", oneValue));
} else if (Status.class.isAssignableFrom(getDataType())) {
castedValue = Status.fromValue(oneValue).orElseThrow(() -> new ReportPortalException(errorType, Suppliers.formattedSupplier("Cannot convert '{}' to valid 'Status'", oneValue)));
} else if (TestItemIssueType.class.isAssignableFrom(getDataType())) {
castedValue = TestItemIssueType.validate(oneValue);
BusinessRule.expect(castedValue, Predicates.notNull()).verify(errorType, Suppliers.formattedSupplier("Cannot convert '{}' to valid 'Issue Type'", oneValue));
} else if (Collection.class.isAssignableFrom(getDataType())) {
/* Collection doesn't stores objects as ObjectId */
castedValue = oneValue;
} else if (String.class.isAssignableFrom(getDataType())) {
castedValue = oneValue != null ? oneValue.trim() : null;
} else {
castedValue = ObjectId.isValid(oneValue) ? new ObjectId(oneValue) : oneValue;
}
return castedValue;
}
use of com.epam.ta.reportportal.exception.ReportPortalException in project service-authorization by reportportal.
the class OAuthConfigurationEndpoint method getOAuthSettings.
/**
* Returns oauth integration settings
*
* @param profileId settings ProfileID
* @param oauthProviderName ID of third-party OAuth provider
* @return All defined OAuth integration settings
*/
@RequestMapping(value = "/{authId}", method = { GET })
@ResponseBody
@ResponseStatus(HttpStatus.OK)
@ApiOperation(value = "Returns OAuth Server Settings", notes = "'default' profile is using till additional UI implementations")
public OAuthDetailsResource getOAuthSettings(@PathVariable String profileId, @PathVariable("authId") String oauthProviderName) {
ServerSettings settings = repository.findOne(profileId);
BusinessRule.expect(settings, Predicates.notNull()).verify(ErrorType.SERVER_SETTINGS_NOT_FOUND, profileId);
return Optional.ofNullable(settings.getoAuth2LoginDetails()).flatMap(details -> Optional.ofNullable(details.get(oauthProviderName))).map(OAuthDetailsConverters.TO_RESOURCE).orElseThrow(() -> new ReportPortalException(ErrorType.OAUTH_INTEGRATION_NOT_FOUND, oauthProviderName));
}
use of com.epam.ta.reportportal.exception.ReportPortalException in project service-authorization by reportportal.
the class OAuthConfigurationEndpoint method deleteOAuthSetting.
/**
* Deletes oauth integration settings
*
* @param profileId settings ProfileID
* @param oauthProviderName ID of third-party OAuth provider
* @return All defined OAuth integration settings
*/
@RequestMapping(value = "/{authId}", method = { DELETE })
@ResponseBody
@ResponseStatus(HttpStatus.OK)
@ApiOperation(value = "Deletes OAuth Integration Settings", notes = "'default' profile is using till additional UI implementations")
public OperationCompletionRS deleteOAuthSetting(@PathVariable String profileId, @PathVariable("authId") String oauthProviderName) {
ServerSettings settings = repository.findOne(profileId);
BusinessRule.expect(settings, Predicates.notNull()).verify(ErrorType.SERVER_SETTINGS_NOT_FOUND, profileId);
Map<String, OAuth2LoginDetails> serverOAuthDetails = Optional.of(settings.getoAuth2LoginDetails()).orElse(new HashMap<>());
if (null != serverOAuthDetails.remove(oauthProviderName)) {
settings.setoAuth2LoginDetails(serverOAuthDetails);
repository.save(settings);
} else {
throw new ReportPortalException(ErrorType.OAUTH_INTEGRATION_NOT_FOUND);
}
return new OperationCompletionRS("Auth settings '" + oauthProviderName + "' were successfully removed");
}
use of com.epam.ta.reportportal.exception.ReportPortalException in project commons-dao by reportportal.
the class UserDataStoreServiceTest method saveLoadAndDeleteThumbnailTest.
@Test
void saveLoadAndDeleteThumbnailTest() throws IOException {
InputStream inputStream = new ClassPathResource("meh.jpg").getInputStream();
String thumbnailId = userDataStoreService.saveThumbnail(random.nextLong() + "thmbnail.jpg", inputStream);
Optional<InputStream> loadedData = userDataStoreService.load(thumbnailId);
assertTrue(loadedData.isPresent());
assertTrue(Files.exists(Paths.get(storageRootPath, userDataStoreService.dataEncoder.decode(thumbnailId))));
userDataStoreService.delete(thumbnailId);
ReportPortalException exception = assertThrows(ReportPortalException.class, () -> userDataStoreService.load(thumbnailId));
assertEquals("Unable to load binary data by id 'Unable to find file'", exception.getMessage());
assertFalse(Files.exists(Paths.get(storageRootPath, userDataStoreService.dataEncoder.decode(thumbnailId))));
}
Aggregations