Search in sources :

Example 1 with ThemeDefinitionMapper

use of io.gravitee.rest.api.service.impl.ThemeServiceImpl.ThemeDefinitionMapper in project gravitee-management-rest-api by gravitee-io.

the class ThemeServiceTest method shouldMergeThemeDefinitionWithLegacy.

@Test
public void shouldMergeThemeDefinitionWithLegacy() throws IOException, TechnicalException {
    ThemeDefinitionMapper mapper = new ThemeDefinitionMapper();
    String def = themeServiceImpl.getDefaultDefinition();
    ThemeDefinition themeDefinition = mapper.readValue(def, ThemeDefinition.class);
    String customDef = themeServiceImpl.getDefinition(THEMES_PATH + "/legacy-definition.json");
    ThemeDefinition legacyDefinition = mapper.readValue(customDef, ThemeDefinition.class);
    assertEquals(38, themeDefinition.getData().size());
    assertEquals(35, legacyDefinition.getData().size());
    ThemeDefinition mergedDefinition = mapper.merge(def, customDef);
    assertNotNull(mergedDefinition);
    assertEquals(38, mergedDefinition.getData().size());
    assertNotNull(mapper.getThemeCssDefinition(legacyDefinition, "gv-theme", "--gv-theme--c"));
    assertNull(mapper.getThemeCssDefinition(themeDefinition, "gv-theme", "--gv-theme--c"));
    assertNull(mapper.getThemeCssDefinition(mergedDefinition, "gv-theme", "--gv-theme--c"));
    assertNull(mapper.getThemeCssDefinition(legacyDefinition, "gv-theme", "--gv-theme-color"));
    assertNotNull(mapper.getThemeCssDefinition(themeDefinition, "gv-theme", "--gv-theme-color"));
    assertNotNull(mapper.getThemeCssDefinition(mergedDefinition, "gv-theme", "--gv-theme-color"));
}
Also used : ThemeDefinitionMapper(io.gravitee.rest.api.service.impl.ThemeServiceImpl.ThemeDefinitionMapper) Test(org.junit.Test)

Example 2 with ThemeDefinitionMapper

use of io.gravitee.rest.api.service.impl.ThemeServiceImpl.ThemeDefinitionMapper in project gravitee-management-rest-api by gravitee-io.

the class ThemeServiceTest method shouldCompareDefinition.

@Test
public void shouldCompareDefinition() throws IOException, TechnicalException {
    ThemeDefinitionMapper definitionMapper = new ThemeDefinitionMapper();
    String definition = themeServiceImpl.getDefaultDefinition();
    ThemeDefinition themeDefinition = definitionMapper.readDefinition(definition);
    String formattedDefinition = definitionMapper.writerWithDefaultPrettyPrinter().writeValueAsString(themeDefinition);
    assertNotEquals(definition, formattedDefinition);
    assertTrue(definitionMapper.isSame(definition, formattedDefinition));
    assertFalse(definitionMapper.isSame(definition, themeServiceImpl.getDefinition(THEMES_PATH + "/custom-definition.json")));
}
Also used : ThemeDefinitionMapper(io.gravitee.rest.api.service.impl.ThemeServiceImpl.ThemeDefinitionMapper) Test(org.junit.Test)

Example 3 with ThemeDefinitionMapper

use of io.gravitee.rest.api.service.impl.ThemeServiceImpl.ThemeDefinitionMapper 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 4 with ThemeDefinitionMapper

use of io.gravitee.rest.api.service.impl.ThemeServiceImpl.ThemeDefinitionMapper 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 5 with ThemeDefinitionMapper

use of io.gravitee.rest.api.service.impl.ThemeServiceImpl.ThemeDefinitionMapper 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

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