Search in sources :

Example 6 with QualityRule

use of io.gravitee.repository.management.model.QualityRule in project gravitee-management-rest-api by gravitee-io.

the class QualityRuleServiceImpl method findById.

@Override
public QualityRuleEntity findById(String id) {
    try {
        LOGGER.debug("Find quality rule by id : {}", id);
        Optional<QualityRule> qualityRule = qualityRuleRepository.findById(id);
        if (qualityRule.isPresent()) {
            return convert(qualityRule.get());
        }
        throw new QualityRuleNotFoundException(id);
    } catch (TechnicalException ex) {
        final String error = "An error occurs while trying to find a quality rule using its ID: " + id;
        LOGGER.error(error, ex);
        throw new TechnicalManagementException(error, ex);
    }
}
Also used : QualityRule(io.gravitee.repository.management.model.QualityRule) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) UuidString(io.gravitee.rest.api.service.common.UuidString) QualityRuleNotFoundException(io.gravitee.rest.api.service.exceptions.QualityRuleNotFoundException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 7 with QualityRule

use of io.gravitee.repository.management.model.QualityRule in project gravitee-management-rest-api by gravitee-io.

the class QualityRuleServiceImpl method convert.

private QualityRule convert(final NewQualityRuleEntity qualityRuleEntity) {
    final QualityRule qualityRule = new QualityRule();
    qualityRule.setId(UuidString.generateRandom());
    qualityRule.setName(qualityRuleEntity.getName());
    qualityRule.setDescription(qualityRuleEntity.getDescription());
    qualityRule.setWeight(qualityRuleEntity.getWeight());
    final Date now = new Date();
    qualityRule.setCreatedAt(now);
    qualityRule.setUpdatedAt(now);
    return qualityRule;
}
Also used : QualityRule(io.gravitee.repository.management.model.QualityRule) Date(java.util.Date)

Example 8 with QualityRule

use of io.gravitee.repository.management.model.QualityRule in project gravitee-management-rest-api by gravitee-io.

the class QualityRuleServiceImpl method convert.

private QualityRule convert(final UpdateQualityRuleEntity qualityRuleEntity, final QualityRule qr) {
    final QualityRule qualityRule = new QualityRule();
    qualityRule.setId(qualityRuleEntity.getId());
    qualityRule.setName(qualityRuleEntity.getName());
    qualityRule.setDescription(qualityRuleEntity.getDescription());
    qualityRule.setWeight(qualityRuleEntity.getWeight());
    qualityRule.setCreatedAt(qr.getCreatedAt());
    qualityRule.setUpdatedAt(new Date());
    return qualityRule;
}
Also used : QualityRule(io.gravitee.repository.management.model.QualityRule) Date(java.util.Date)

Example 9 with QualityRule

use of io.gravitee.repository.management.model.QualityRule in project gravitee-management-rest-api by gravitee-io.

the class QualityRuleServiceImpl method update.

@Override
public QualityRuleEntity update(UpdateQualityRuleEntity updateEntity) {
    try {
        final Optional<QualityRule> optionalQualityRule = qualityRuleRepository.findById(updateEntity.getId());
        if (!optionalQualityRule.isPresent()) {
            throw new QualityRuleNotFoundException(updateEntity.getId());
        }
        final QualityRule qualityRule = qualityRuleRepository.update(convert(updateEntity, optionalQualityRule.get()));
        auditService.createEnvironmentAuditLog(singletonMap(QUALITY_RULE, qualityRule.getId()), QUALITY_RULE_UPDATED, qualityRule.getUpdatedAt(), optionalQualityRule.get(), qualityRule);
        return convert(qualityRule);
    } catch (TechnicalException e) {
        LOGGER.error("An error occurs while trying to update quality rule {}", updateEntity, e);
        throw new TechnicalManagementException("An error occurs while trying to update quality rule " + updateEntity, e);
    }
}
Also used : QualityRule(io.gravitee.repository.management.model.QualityRule) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) QualityRuleNotFoundException(io.gravitee.rest.api.service.exceptions.QualityRuleNotFoundException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 10 with QualityRule

use of io.gravitee.repository.management.model.QualityRule in project gravitee-management-rest-api by gravitee-io.

the class QualityRuleServiceTest method shouldDelete.

@Test
public void shouldDelete() throws TechnicalException {
    final QualityRule qualityRule = mock(QualityRule.class);
    when(qualityRuleRepository.findById(QUALITY_RULE_ID)).thenReturn(of(qualityRule));
    qualityRuleService.delete(QUALITY_RULE_ID);
    verify(qualityRuleRepository, times(1)).delete(QUALITY_RULE_ID);
    verify(auditService, times(1)).createEnvironmentAuditLog(eq(ImmutableMap.of(QUALITY_RULE, QUALITY_RULE_ID)), eq(QualityRule.AuditEvent.QUALITY_RULE_DELETED), any(Date.class), isNull(), eq(qualityRule));
    verify(apiQualityRuleRepository, times(1)).deleteByQualityRule(QUALITY_RULE_ID);
}
Also used : QualityRule(io.gravitee.repository.management.model.QualityRule) Date(java.util.Date) Test(org.junit.Test)

Aggregations

QualityRule (io.gravitee.repository.management.model.QualityRule)12 Date (java.util.Date)8 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)6 Test (org.junit.Test)6 QualityRuleNotFoundException (io.gravitee.rest.api.service.exceptions.QualityRuleNotFoundException)4 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)4 ImmutableMap (com.google.common.collect.ImmutableMap)2 ApiQualityRuleRepository (io.gravitee.repository.management.api.ApiQualityRuleRepository)2 QualityRuleRepository (io.gravitee.repository.management.api.QualityRuleRepository)2 QUALITY_RULE (io.gravitee.repository.management.model.Audit.AuditProperties.QUALITY_RULE)2 io.gravitee.rest.api.model.quality (io.gravitee.rest.api.model.quality)2 QualityRuleServiceImpl (io.gravitee.rest.api.service.impl.QualityRuleServiceImpl)2 Collections.singleton (java.util.Collections.singleton)2 List (java.util.List)2 Optional.empty (java.util.Optional.empty)2 Optional.of (java.util.Optional.of)2 Assert.assertEquals (org.junit.Assert.assertEquals)2 Assert.assertNotNull (org.junit.Assert.assertNotNull)2 RunWith (org.junit.runner.RunWith)2 InjectMocks (org.mockito.InjectMocks)2