Search in sources :

Example 1 with QualityRule

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

the class QualityRuleServiceTest method shouldFindAll.

@Test
public void shouldFindAll() throws TechnicalException {
    final QualityRule qualityRule = mock(QualityRule.class);
    when(qualityRule.getId()).thenReturn(QUALITY_RULE_ID);
    when(qualityRule.getName()).thenReturn("NAME");
    when(qualityRule.getDescription()).thenReturn("DESC");
    when(qualityRule.getWeight()).thenReturn(1);
    when(qualityRule.getCreatedAt()).thenReturn(new Date(1));
    when(qualityRule.getUpdatedAt()).thenReturn(new Date(2));
    when(qualityRuleRepository.findAll()).thenReturn(singleton(qualityRule));
    final List<QualityRuleEntity> qualityRules = qualityRuleService.findAll();
    final QualityRuleEntity qualityRuleEntity = qualityRules.iterator().next();
    assertEquals(QUALITY_RULE_ID, qualityRuleEntity.getId());
    assertEquals("NAME", qualityRuleEntity.getName());
    assertEquals("DESC", qualityRuleEntity.getDescription());
    assertEquals(1, qualityRuleEntity.getWeight());
    assertEquals(new Date(1), qualityRuleEntity.getCreatedAt());
    assertEquals(new Date(2), qualityRuleEntity.getUpdatedAt());
}
Also used : QualityRule(io.gravitee.repository.management.model.QualityRule) Date(java.util.Date) Test(org.junit.Test)

Example 2 with QualityRule

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

the class QualityRuleServiceTest method shouldCreate.

@Test
public void shouldCreate() throws TechnicalException {
    final NewQualityRuleEntity newQualityRuleEntity = new NewQualityRuleEntity();
    newQualityRuleEntity.setName("NAME");
    newQualityRuleEntity.setDescription("DESC");
    newQualityRuleEntity.setWeight(1);
    final QualityRule createdQualityRule = new QualityRule();
    createdQualityRule.setId(QUALITY_RULE_ID);
    createdQualityRule.setName("NAME");
    createdQualityRule.setDescription("DESC");
    createdQualityRule.setWeight(1);
    createdQualityRule.setCreatedAt(new Date());
    createdQualityRule.setUpdatedAt(new Date());
    when(qualityRuleRepository.create(any())).thenReturn(createdQualityRule);
    final QualityRuleEntity qualityRuleEntity = qualityRuleService.create(newQualityRuleEntity);
    assertNotNull(qualityRuleEntity.getId());
    assertEquals("NAME", qualityRuleEntity.getName());
    assertEquals("DESC", qualityRuleEntity.getDescription());
    assertEquals(1, qualityRuleEntity.getWeight());
    assertNotNull(qualityRuleEntity.getCreatedAt());
    assertNotNull(qualityRuleEntity.getUpdatedAt());
    final QualityRule qualityRule = new QualityRule();
    qualityRule.setName("NAME");
    qualityRule.setDescription("DESC");
    qualityRule.setWeight(1);
    verify(qualityRuleRepository, times(1)).create(argThat(argument -> "NAME".equals(argument.getName()) && "DESC".equals(argument.getDescription()) && Integer.valueOf(1).equals(argument.getWeight()) && !argument.getId().isEmpty() && argument.getCreatedAt() != null && argument.getUpdatedAt() != null));
    verify(auditService, times(1)).createEnvironmentAuditLog(eq(ImmutableMap.of(QUALITY_RULE, QUALITY_RULE_ID)), eq(QualityRule.AuditEvent.QUALITY_RULE_CREATED), any(Date.class), isNull(), any());
}
Also used : QualityRule(io.gravitee.repository.management.model.QualityRule) InjectMocks(org.mockito.InjectMocks) Optional.empty(java.util.Optional.empty) QualityRuleRepository(io.gravitee.repository.management.api.QualityRuleRepository) ApiQualityRuleRepository(io.gravitee.repository.management.api.ApiQualityRuleRepository) ImmutableMap(com.google.common.collect.ImmutableMap) Assert.assertNotNull(org.junit.Assert.assertNotNull) Date(java.util.Date) Mock(org.mockito.Mock) Optional.of(java.util.Optional.of) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) io.gravitee.rest.api.model.quality(io.gravitee.rest.api.model.quality) Mockito(org.mockito.Mockito) List(java.util.List) Collections.singleton(java.util.Collections.singleton) QualityRuleNotFoundException(io.gravitee.rest.api.service.exceptions.QualityRuleNotFoundException) QUALITY_RULE(io.gravitee.repository.management.model.Audit.AuditProperties.QUALITY_RULE) QualityRuleServiceImpl(io.gravitee.rest.api.service.impl.QualityRuleServiceImpl) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Assert.assertEquals(org.junit.Assert.assertEquals) QualityRule(io.gravitee.repository.management.model.QualityRule) Date(java.util.Date) Test(org.junit.Test)

Example 3 with QualityRule

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

the class QualityRuleServiceTest method shouldUpdate.

@Test
public void shouldUpdate() throws TechnicalException {
    final UpdateQualityRuleEntity updateQualityRuleEntity = new UpdateQualityRuleEntity();
    updateQualityRuleEntity.setId(QUALITY_RULE_ID);
    updateQualityRuleEntity.setName("NAME");
    updateQualityRuleEntity.setDescription("DESC");
    updateQualityRuleEntity.setWeight(1);
    final QualityRule updatedQualityRule = new QualityRule();
    updatedQualityRule.setId(QUALITY_RULE_ID);
    updatedQualityRule.setName("NAME");
    updatedQualityRule.setDescription("DESC");
    updatedQualityRule.setWeight(1);
    updatedQualityRule.setCreatedAt(new Date());
    updatedQualityRule.setUpdatedAt(new Date());
    when(qualityRuleRepository.update(any())).thenReturn(updatedQualityRule);
    when(qualityRuleRepository.findById(QUALITY_RULE_ID)).thenReturn(of(updatedQualityRule));
    final QualityRuleEntity qualityRuleEntity = qualityRuleService.update(updateQualityRuleEntity);
    assertNotNull(qualityRuleEntity.getId());
    assertEquals("NAME", qualityRuleEntity.getName());
    assertEquals("DESC", qualityRuleEntity.getDescription());
    assertEquals(1, qualityRuleEntity.getWeight());
    assertNotNull(qualityRuleEntity.getCreatedAt());
    assertNotNull(qualityRuleEntity.getUpdatedAt());
    final QualityRule qualityRule = new QualityRule();
    qualityRule.setName("NAME");
    qualityRule.setDescription("DESC");
    qualityRule.setWeight(1);
    verify(qualityRuleRepository, times(1)).update(argThat(argument -> "NAME".equals(argument.getName()) && "DESC".equals(argument.getDescription()) && Integer.valueOf(1).equals(argument.getWeight()) && QUALITY_RULE_ID.equals(argument.getId()) && argument.getCreatedAt() != null && argument.getUpdatedAt() != null));
    verify(auditService, times(1)).createEnvironmentAuditLog(eq(ImmutableMap.of(QUALITY_RULE, QUALITY_RULE_ID)), eq(QualityRule.AuditEvent.QUALITY_RULE_UPDATED), any(Date.class), any(), any());
}
Also used : QualityRule(io.gravitee.repository.management.model.QualityRule) InjectMocks(org.mockito.InjectMocks) Optional.empty(java.util.Optional.empty) QualityRuleRepository(io.gravitee.repository.management.api.QualityRuleRepository) ApiQualityRuleRepository(io.gravitee.repository.management.api.ApiQualityRuleRepository) ImmutableMap(com.google.common.collect.ImmutableMap) Assert.assertNotNull(org.junit.Assert.assertNotNull) Date(java.util.Date) Mock(org.mockito.Mock) Optional.of(java.util.Optional.of) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) io.gravitee.rest.api.model.quality(io.gravitee.rest.api.model.quality) Mockito(org.mockito.Mockito) List(java.util.List) Collections.singleton(java.util.Collections.singleton) QualityRuleNotFoundException(io.gravitee.rest.api.service.exceptions.QualityRuleNotFoundException) QUALITY_RULE(io.gravitee.repository.management.model.Audit.AuditProperties.QUALITY_RULE) QualityRuleServiceImpl(io.gravitee.rest.api.service.impl.QualityRuleServiceImpl) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Assert.assertEquals(org.junit.Assert.assertEquals) QualityRule(io.gravitee.repository.management.model.QualityRule) Date(java.util.Date) Test(org.junit.Test)

Example 4 with QualityRule

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

the class QualityRuleServiceImpl method create.

@Override
public QualityRuleEntity create(NewQualityRuleEntity newEntity) {
    try {
        final QualityRule qualityRule = convert(newEntity);
        final QualityRule createdQualityRule = qualityRuleRepository.create(qualityRule);
        auditService.createEnvironmentAuditLog(Collections.singletonMap(QUALITY_RULE, createdQualityRule.getId()), QualityRule.AuditEvent.QUALITY_RULE_CREATED, qualityRule.getCreatedAt(), null, qualityRule);
        return convert(createdQualityRule);
    } catch (TechnicalException e) {
        LOGGER.error("An error occurs while trying to create a quality rule {}", newEntity, e);
        throw new TechnicalManagementException("An error occurs while trying to create a quality rule " + newEntity, e);
    }
}
Also used : QualityRule(io.gravitee.repository.management.model.QualityRule) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

Example 5 with QualityRule

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

the class QualityRuleServiceImpl method delete.

@Override
public void delete(final String qualityRule) {
    try {
        final Optional<QualityRule> qualityRuleOptional = qualityRuleRepository.findById(qualityRule);
        if (qualityRuleOptional.isPresent()) {
            qualityRuleRepository.delete(qualityRule);
            // delete all reference on api quality rule
            apiQualityRuleRepository.deleteByQualityRule(qualityRule);
            auditService.createEnvironmentAuditLog(Collections.singletonMap(QUALITY_RULE, qualityRule), QualityRule.AuditEvent.QUALITY_RULE_DELETED, new Date(), null, qualityRuleOptional.get());
        }
    } catch (TechnicalException ex) {
        LOGGER.error("An error occurs while trying to delete quality rule {}", qualityRule, ex);
        throw new TechnicalManagementException("An error occurs while trying to delete quality rule " + qualityRule, ex);
    }
}
Also used : QualityRule(io.gravitee.repository.management.model.QualityRule) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) Date(java.util.Date) TechnicalManagementException(io.gravitee.rest.api.service.exceptions.TechnicalManagementException)

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