Search in sources :

Example 6 with Theme

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

the class ThemeServiceTest method shouldThrowDuplicateThemeNameExceptionOnUpdate.

@Test(expected = DuplicateThemeNameException.class)
public void shouldThrowDuplicateThemeNameExceptionOnUpdate() throws TechnicalException {
    final Theme theme = mock(Theme.class);
    when(theme.getId()).thenReturn(THEME_ID);
    when(theme.getName()).thenReturn("NAME");
    when(theme.getDefinition()).thenReturn(themeServiceImpl.getDefaultDefinition());
    final Theme theme2 = mock(Theme.class);
    when(theme2.getId()).thenReturn("foobar");
    when(theme2.getName()).thenReturn("NAME");
    when(theme2.getDefinition()).thenReturn(themeServiceImpl.getDefaultDefinition());
    when(themeRepository.findById(THEME_ID)).thenReturn(of(theme));
    when(themeRepository.findByReferenceIdAndReferenceType(GraviteeContext.getCurrentEnvironment(), ENVIRONMENT.name())).thenReturn(new HashSet(asList(theme, theme2)));
    final UpdateThemeEntity updateThemeEntity = new UpdateThemeEntity();
    updateThemeEntity.setId(THEME_ID);
    updateThemeEntity.setName("NAME");
    themeService.update(updateThemeEntity);
}
Also used : Theme(io.gravitee.repository.management.model.Theme) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with Theme

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

the class ThemeServiceTest method shouldUpdateDefaultTheme.

@Test
public void shouldUpdateDefaultTheme() throws TechnicalException, IOException {
    ObjectMapper mapper = new ObjectMapper();
    ThemeDefinitionMapper themeDefinitionMapper = new ThemeDefinitionMapper();
    String definition = themeServiceImpl.getDefaultDefinition();
    final Theme theme = mock(Theme.class);
    when(theme.getDefinition()).thenReturn(definition);
    final Theme theme2 = mock(Theme.class);
    when(theme2.getId()).thenReturn(THEME_ID);
    when(theme2.getName()).thenReturn("NAME");
    String customDefinition = themeServiceImpl.getDefinition(THEMES_PATH + "/custom-definition.json");
    when(theme2.getDefinition()).thenReturn(customDefinition);
    when(theme2.getReferenceType()).thenReturn(ENVIRONMENT.name());
    when(theme2.getReferenceId()).thenReturn("DEFAULT");
    when(theme2.getCreatedAt()).thenReturn(new Date(1));
    when(theme2.getUpdatedAt()).thenReturn(new Date(2));
    when(themeRepository.findByReferenceIdAndReferenceType(GraviteeContext.getCurrentEnvironment(), ENVIRONMENT.name())).thenReturn(new HashSet(asList(theme, theme2)));
    String mergeDefinition = themeDefinitionMapper.writeValueAsString(themeDefinitionMapper.merge(definition, customDefinition));
    themeService.updateDefaultTheme();
    verify(themeRepository, times(1)).update(argThat(argument -> {
        try {
            return ("NAME".equals(argument.getName()) && mapper.readTree(argument.getDefinition()).equals(mapper.readTree(mergeDefinition)) && "DEFAULT".equals(argument.getReferenceId()) && ENVIRONMENT.name().equals(argument.getReferenceType()) && !argument.getId().isEmpty() && argument.getCreatedAt() != null && argument.getUpdatedAt() != null);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }));
    verify(auditService, times(1)).createEnvironmentAuditLog(eq(ImmutableMap.of(THEME, THEME_ID)), eq(Theme.AuditEvent.THEME_UPDATED), any(Date.class), any(), any());
}
Also used : Optional.empty(java.util.Optional.empty) InlinePictureEntity(io.gravitee.rest.api.model.InlinePictureEntity) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Theme(io.gravitee.repository.management.model.Theme) Date(java.util.Date) Mock(org.mockito.Mock) ENVIRONMENT(io.gravitee.repository.management.model.ThemeReferenceType.ENVIRONMENT) Optional.of(java.util.Optional.of) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) RunWith(org.junit.runner.RunWith) GraviteeContext(io.gravitee.rest.api.service.common.GraviteeContext) HashSet(java.util.HashSet) Collections.singleton(java.util.Collections.singleton) Arrays.asList(java.util.Arrays.asList) DuplicateThemeNameException(io.gravitee.rest.api.service.exceptions.DuplicateThemeNameException) ThemeNotFoundException(io.gravitee.rest.api.service.exceptions.ThemeNotFoundException) Before(org.junit.Before) THEME(io.gravitee.repository.management.model.Audit.AuditProperties.THEME) InjectMocks(org.mockito.InjectMocks) PictureEntity(io.gravitee.rest.api.model.PictureEntity) ImmutableMap(com.google.common.collect.ImmutableMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Test(org.junit.Test) ThemeRepository(io.gravitee.repository.management.api.ThemeRepository) UrlPictureEntity(io.gravitee.rest.api.model.UrlPictureEntity) ThemeDefinitionMapper(io.gravitee.rest.api.service.impl.ThemeServiceImpl.ThemeDefinitionMapper) Mockito(org.mockito.Mockito) io.gravitee.rest.api.model.theme(io.gravitee.rest.api.model.theme) ThemeServiceImpl(io.gravitee.rest.api.service.impl.ThemeServiceImpl) Assert(org.junit.Assert) Collections(java.util.Collections) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Theme(io.gravitee.repository.management.model.Theme) IOException(java.io.IOException) ThemeDefinitionMapper(io.gravitee.rest.api.service.impl.ThemeServiceImpl.ThemeDefinitionMapper) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Date(java.util.Date) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 8 with Theme

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

the class ThemeServiceTest method shouldResetToDefaultTheme.

@Test
public void shouldResetToDefaultTheme() throws TechnicalException, JsonProcessingException {
    ThemeDefinition themeDefinition = new ThemeDefinition();
    themeDefinition.setData(Collections.EMPTY_LIST);
    final Theme theme = new Theme();
    theme.setId(THEME_ID);
    theme.setName("NAME");
    theme.setDefinition(themeServiceImpl.getDefinition(THEMES_PATH + "/custom-definition.json"));
    theme.setReferenceId("DEFAULT");
    theme.setReferenceType(ENVIRONMENT.name());
    theme.setCreatedAt(new Date());
    theme.setUpdatedAt(new Date());
    themeService.resetToDefaultTheme(THEME_ID);
    verify(themeRepository, times(1)).delete(THEME_ID);
    verify(auditService, times(1)).createEnvironmentAuditLog(eq(ImmutableMap.of(THEME, THEME_ID)), eq(Theme.AuditEvent.THEME_RESET), any(Date.class), any(), any());
}
Also used : Theme(io.gravitee.repository.management.model.Theme) Date(java.util.Date) Test(org.junit.Test)

Example 9 with Theme

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

the class ThemeServiceTest method shouldCreateDefaultTheme.

@Test
public void shouldCreateDefaultTheme() throws TechnicalException, IOException {
    ThemeDefinitionMapper definitionMapper = new ThemeDefinitionMapper();
    String definition = themeServiceImpl.getDefaultDefinition();
    final UpdateThemeEntity themeToCreate = new UpdateThemeEntity();
    themeToCreate.setId(THEME_ID);
    themeToCreate.setName("Default");
    themeToCreate.setDefinition(definitionMapper.readDefinition(definition));
    final Theme createdTheme = new Theme();
    createdTheme.setId(THEME_ID);
    createdTheme.setName("Default");
    createdTheme.setDefinition(definition);
    createdTheme.setCreatedAt(new Date());
    createdTheme.setUpdatedAt(new Date());
    when(themeRepository.create(any())).thenReturn(createdTheme);
    assertEquals(definitionMapper.readTree(definition), definitionMapper.readTree(definition));
    assertEquals(definition, definition);
    themeService.update(themeToCreate);
    verify(themeRepository, times(1)).create(argThat(argument -> {
        try {
            return ("Default".equals(argument.getName()) && definitionMapper.readTree(argument.getDefinition()).equals(definitionMapper.readTree(definition)) && "DEFAULT".equals(argument.getReferenceId()) && ENVIRONMENT.name().equals(argument.getReferenceType()) && !argument.getId().isEmpty() && argument.getCreatedAt() != null && argument.getUpdatedAt() != null);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }));
    verify(auditService, times(1)).createEnvironmentAuditLog(eq(ImmutableMap.of(THEME, THEME_ID)), eq(Theme.AuditEvent.THEME_CREATED), any(Date.class), isNull(), any());
}
Also used : Optional.empty(java.util.Optional.empty) InlinePictureEntity(io.gravitee.rest.api.model.InlinePictureEntity) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Theme(io.gravitee.repository.management.model.Theme) Date(java.util.Date) Mock(org.mockito.Mock) ENVIRONMENT(io.gravitee.repository.management.model.ThemeReferenceType.ENVIRONMENT) Optional.of(java.util.Optional.of) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) RunWith(org.junit.runner.RunWith) GraviteeContext(io.gravitee.rest.api.service.common.GraviteeContext) HashSet(java.util.HashSet) Collections.singleton(java.util.Collections.singleton) Arrays.asList(java.util.Arrays.asList) DuplicateThemeNameException(io.gravitee.rest.api.service.exceptions.DuplicateThemeNameException) ThemeNotFoundException(io.gravitee.rest.api.service.exceptions.ThemeNotFoundException) Before(org.junit.Before) THEME(io.gravitee.repository.management.model.Audit.AuditProperties.THEME) InjectMocks(org.mockito.InjectMocks) PictureEntity(io.gravitee.rest.api.model.PictureEntity) ImmutableMap(com.google.common.collect.ImmutableMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Test(org.junit.Test) ThemeRepository(io.gravitee.repository.management.api.ThemeRepository) UrlPictureEntity(io.gravitee.rest.api.model.UrlPictureEntity) ThemeDefinitionMapper(io.gravitee.rest.api.service.impl.ThemeServiceImpl.ThemeDefinitionMapper) Mockito(org.mockito.Mockito) io.gravitee.rest.api.model.theme(io.gravitee.rest.api.model.theme) ThemeServiceImpl(io.gravitee.rest.api.service.impl.ThemeServiceImpl) Assert(org.junit.Assert) Collections(java.util.Collections) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Theme(io.gravitee.repository.management.model.Theme) IOException(java.io.IOException) ThemeDefinitionMapper(io.gravitee.rest.api.service.impl.ThemeServiceImpl.ThemeDefinitionMapper) Date(java.util.Date) Test(org.junit.Test)

Example 10 with Theme

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

the class ThemeServiceTest method shouldCreate.

@Test
public void shouldCreate() throws TechnicalException, IOException {
    ThemeDefinitionMapper definitionMapper = new ThemeDefinitionMapper();
    ThemeDefinition themeDefinition = new ThemeDefinition();
    themeDefinition.setData(Collections.EMPTY_LIST);
    String definition = definitionMapper.writeValueAsString(themeDefinition);
    final NewThemeEntity newThemeEntity = new NewThemeEntity();
    newThemeEntity.setName("NAME");
    newThemeEntity.setDefinition(themeDefinition);
    final Theme createdTheme = new Theme();
    createdTheme.setId(THEME_ID);
    createdTheme.setName("NAME");
    createdTheme.setDefinition(definition);
    createdTheme.setCreatedAt(new Date());
    createdTheme.setUpdatedAt(new Date());
    when(themeRepository.create(any())).thenReturn(createdTheme);
    final ThemeEntity themeEntity = themeService.create(newThemeEntity);
    assertNotNull(themeEntity.getId());
    assertEquals("NAME", themeEntity.getName());
    assertNotNull(themeEntity.getDefinition());
    assertEquals(0, themeEntity.getDefinition().getData().size());
    assertNotNull(themeEntity.getCreatedAt());
    assertNotNull(themeEntity.getUpdatedAt());
    final Theme theme = new Theme();
    theme.setName("NAME");
    theme.setDefinition(definition);
    theme.setReferenceId("REF_ID");
    theme.setReferenceType(ENVIRONMENT.name());
    verify(themeRepository, times(1)).create(argThat(argument -> {
        return ("NAME".equals(argument.getName()) && argument.getDefinition() != null && "DEFAULT".equals(argument.getReferenceId()) && ENVIRONMENT.name().equals(argument.getReferenceType()) && !argument.getId().isEmpty() && argument.getCreatedAt() != null && argument.getUpdatedAt() != null);
    }));
    verify(auditService, times(1)).createEnvironmentAuditLog(eq(ImmutableMap.of(THEME, THEME_ID)), eq(Theme.AuditEvent.THEME_CREATED), any(Date.class), isNull(), any());
}
Also used : Optional.empty(java.util.Optional.empty) InlinePictureEntity(io.gravitee.rest.api.model.InlinePictureEntity) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Theme(io.gravitee.repository.management.model.Theme) Date(java.util.Date) Mock(org.mockito.Mock) ENVIRONMENT(io.gravitee.repository.management.model.ThemeReferenceType.ENVIRONMENT) Optional.of(java.util.Optional.of) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) RunWith(org.junit.runner.RunWith) GraviteeContext(io.gravitee.rest.api.service.common.GraviteeContext) HashSet(java.util.HashSet) Collections.singleton(java.util.Collections.singleton) Arrays.asList(java.util.Arrays.asList) DuplicateThemeNameException(io.gravitee.rest.api.service.exceptions.DuplicateThemeNameException) ThemeNotFoundException(io.gravitee.rest.api.service.exceptions.ThemeNotFoundException) Before(org.junit.Before) THEME(io.gravitee.repository.management.model.Audit.AuditProperties.THEME) InjectMocks(org.mockito.InjectMocks) PictureEntity(io.gravitee.rest.api.model.PictureEntity) ImmutableMap(com.google.common.collect.ImmutableMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) ReflectionTestUtils(org.springframework.test.util.ReflectionTestUtils) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Test(org.junit.Test) ThemeRepository(io.gravitee.repository.management.api.ThemeRepository) UrlPictureEntity(io.gravitee.rest.api.model.UrlPictureEntity) ThemeDefinitionMapper(io.gravitee.rest.api.service.impl.ThemeServiceImpl.ThemeDefinitionMapper) Mockito(org.mockito.Mockito) io.gravitee.rest.api.model.theme(io.gravitee.rest.api.model.theme) ThemeServiceImpl(io.gravitee.rest.api.service.impl.ThemeServiceImpl) Assert(org.junit.Assert) Collections(java.util.Collections) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Theme(io.gravitee.repository.management.model.Theme) ThemeDefinitionMapper(io.gravitee.rest.api.service.impl.ThemeServiceImpl.ThemeDefinitionMapper) Date(java.util.Date) Test(org.junit.Test)

Aggregations

Theme (io.gravitee.repository.management.model.Theme)24 Test (org.junit.Test)17 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)10 Date (java.util.Date)10 InlinePictureEntity (io.gravitee.rest.api.model.InlinePictureEntity)8 PictureEntity (io.gravitee.rest.api.model.PictureEntity)8 UrlPictureEntity (io.gravitee.rest.api.model.UrlPictureEntity)8 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)7 DuplicateThemeNameException (io.gravitee.rest.api.service.exceptions.DuplicateThemeNameException)7 ThemeDefinitionMapper (io.gravitee.rest.api.service.impl.ThemeServiceImpl.ThemeDefinitionMapper)7 TechnicalManagementException (io.gravitee.rest.api.service.exceptions.TechnicalManagementException)6 ThemeNotFoundException (io.gravitee.rest.api.service.exceptions.ThemeNotFoundException)6 IOException (java.io.IOException)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 ThemeRepository (io.gravitee.repository.management.api.ThemeRepository)5 THEME (io.gravitee.repository.management.model.Audit.AuditProperties.THEME)5 io.gravitee.rest.api.model.theme (io.gravitee.rest.api.model.theme)5 GraviteeContext (io.gravitee.rest.api.service.common.GraviteeContext)5 HashSet (java.util.HashSet)5 ImmutableMap (com.google.common.collect.ImmutableMap)4