Search in sources :

Example 1 with ThemeProperty

use of run.halo.app.handler.theme.config.support.ThemeProperty in project halo by halo-dev.

the class ThemeRepositoryImplTest method getActivatedThemeBySingleThread.

@Test
void getActivatedThemeBySingleThread() {
    ThemeProperty expectedTheme = new ThemeProperty();
    expectedTheme.setId(HaloConst.DEFAULT_THEME_ID);
    expectedTheme.setActivated(true);
    given(optionRepository.findByKey(THEME.getValue())).willReturn(Optional.empty());
    doReturn(Optional.of(expectedTheme)).when(themeRepository).fetchThemePropertyByThemeId(HaloConst.DEFAULT_THEME_ID);
    ThemeProperty resultTheme = themeRepository.getActivatedThemeProperty();
    assertEquals(expectedTheme, resultTheme);
    verify(optionRepository, times(1)).findByKey(any());
    verify(themeRepository, times(1)).fetchThemePropertyByThemeId(any());
}
Also used : ThemeProperty(run.halo.app.handler.theme.config.support.ThemeProperty) Test(org.junit.jupiter.api.Test)

Example 2 with ThemeProperty

use of run.halo.app.handler.theme.config.support.ThemeProperty in project halo by halo-dev.

the class ThemeRepositoryImplTest method getActivatedThemeByMultiThread.

@Test
void getActivatedThemeByMultiThread() throws InterruptedException {
    ThemeProperty expectedTheme = new ThemeProperty();
    expectedTheme.setId(HaloConst.DEFAULT_THEME_ID);
    expectedTheme.setActivated(true);
    given(optionRepository.findByKey(THEME.getValue())).willReturn(Optional.empty());
    doReturn(Optional.of(expectedTheme)).when(themeRepository).fetchThemePropertyByThemeId(HaloConst.DEFAULT_THEME_ID);
    ExecutorService executorService = Executors.newFixedThreadPool(10);
    // define tasks
    List<Callable<ThemeProperty>> tasks = IntStream.range(0, 10).mapToObj(i -> (Callable<ThemeProperty>) () -> themeRepository.getActivatedThemeProperty()).collect(Collectors.toList());
    // invoke and get results
    executorService.invokeAll(tasks).forEach(future -> {
        try {
            assertEquals(expectedTheme, future.get(100, TimeUnit.MILLISECONDS));
        } catch (Exception e) {
            throw new RuntimeException("Failed to get task result!", e);
        }
    });
    verify(optionRepository, times(1)).findByKey(any());
    verify(themeRepository, times(1)).fetchThemePropertyByThemeId(any());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) IntStream(java.util.stream.IntStream) BeforeEach(org.junit.jupiter.api.BeforeEach) Mock(org.mockito.Mock) Callable(java.util.concurrent.Callable) MockitoAnnotations(org.mockito.MockitoAnnotations) BDDMockito.given(org.mockito.BDDMockito.given) Spy(org.mockito.Spy) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) THEME(run.halo.app.model.properties.PrimaryProperties.THEME) Mockito.doReturn(org.mockito.Mockito.doReturn) ExecutorService(java.util.concurrent.ExecutorService) InjectMocks(org.mockito.InjectMocks) Mockito.times(org.mockito.Mockito.times) HaloProperties(run.halo.app.config.properties.HaloProperties) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) Mockito.verify(org.mockito.Mockito.verify) ThemeProperty(run.halo.app.handler.theme.config.support.ThemeProperty) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) Optional(java.util.Optional) HaloConst(run.halo.app.model.support.HaloConst) ExecutorService(java.util.concurrent.ExecutorService) Callable(java.util.concurrent.Callable) ThemeProperty(run.halo.app.handler.theme.config.support.ThemeProperty) Test(org.junit.jupiter.api.Test)

Example 3 with ThemeProperty

use of run.halo.app.handler.theme.config.support.ThemeProperty in project halo by halo-dev.

the class ThemeServiceImpl method fetchConfig.

@Override
@NonNull
public List<Group> fetchConfig(@NonNull String themeId) {
    Assert.hasText(themeId, "Theme id must not be blank");
    // Get theme property
    ThemeProperty themeProperty = getThemeOfNonNullBy(themeId);
    if (!themeProperty.isHasOptions()) {
        // If this theme dose not has an option, then return empty list
        return Collections.emptyList();
    }
    try {
        for (String optionsName : SETTINGS_NAMES) {
            // Resolve the options path
            Path optionsPath = Paths.get(themeProperty.getThemePath(), optionsName);
            log.debug("Finding options in: [{}]", optionsPath.toString());
            // Check existence
            if (!Files.exists(optionsPath)) {
                continue;
            }
            // Read the yaml file
            String optionContent = Files.readString(optionsPath);
            // Resolve it
            return themeConfigResolver.resolve(optionContent);
        }
        return Collections.emptyList();
    } catch (IOException e) {
        throw new ServiceException("读取主题配置文件失败", e);
    }
}
Also used : Path(java.nio.file.Path) ServiceException(run.halo.app.exception.ServiceException) IOException(java.io.IOException) ThemeProperty(run.halo.app.handler.theme.config.support.ThemeProperty) NonNull(org.springframework.lang.NonNull)

Example 4 with ThemeProperty

use of run.halo.app.handler.theme.config.support.ThemeProperty in project halo by halo-dev.

the class ThemeServiceImpl method checkDirectory.

/**
 * Check if directory is valid or not.
 *
 * @param themeId themeId must not be blank
 * @param absoluteName throws when the given absolute directory name is invalid
 */
private void checkDirectory(@NonNull String themeId, @NonNull String absoluteName) {
    ThemeProperty themeProperty = getThemeOfNonNullBy(themeId);
    FileUtils.checkDirectoryTraversal(themeProperty.getThemePath(), absoluteName);
}
Also used : ThemeProperty(run.halo.app.handler.theme.config.support.ThemeProperty)

Example 5 with ThemeProperty

use of run.halo.app.handler.theme.config.support.ThemeProperty in project halo by halo-dev.

the class ThemeServiceImpl method deleteTheme.

@Transactional
@Override
public void deleteTheme(@NonNull String themeId, @NonNull Boolean deleteSettings) {
    // Get the theme property
    ThemeProperty themeProperty = getThemeOfNonNullBy(themeId);
    if (themeId.equals(getActivatedThemeId())) {
        // Prevent to delete the activated theme
        throw new BadRequestException("无法删除正在使用的主题!").setErrorData(themeId);
    }
    try {
        // Delete the folder
        FileUtils.deleteFolder(Paths.get(themeProperty.getThemePath()));
        if (deleteSettings) {
            // Delete theme settings
            themeSettingRepository.deleteByThemeId(themeId);
        }
        // Delete theme cache
        eventPublisher.publishEvent(new ThemeUpdatedEvent(this));
    } catch (Exception e) {
        throw new ServiceException("主题删除失败", e).setErrorData(themeId);
    }
}
Also used : ServiceException(run.halo.app.exception.ServiceException) ThemeUpdatedEvent(run.halo.app.event.theme.ThemeUpdatedEvent) BadRequestException(run.halo.app.exception.BadRequestException) NotFoundException(run.halo.app.exception.NotFoundException) ServiceException(run.halo.app.exception.ServiceException) ThemeNotFoundException(run.halo.app.exception.ThemeNotFoundException) ThemePropertyMissingException(run.halo.app.exception.ThemePropertyMissingException) ThemeNotSupportException(run.halo.app.exception.ThemeNotSupportException) ThemeUpdateException(run.halo.app.exception.ThemeUpdateException) IOException(java.io.IOException) ForbiddenException(run.halo.app.exception.ForbiddenException) BadRequestException(run.halo.app.exception.BadRequestException) ThemeProperty(run.halo.app.handler.theme.config.support.ThemeProperty) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ThemeProperty (run.halo.app.handler.theme.config.support.ThemeProperty)18 IOException (java.io.IOException)6 Test (org.junit.jupiter.api.Test)6 ServiceException (run.halo.app.exception.ServiceException)6 ThemeNotFoundException (run.halo.app.exception.ThemeNotFoundException)6 Path (java.nio.file.Path)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 NonNull (org.springframework.lang.NonNull)3 Transactional (org.springframework.transaction.annotation.Transactional)3 ThemeUpdatedEvent (run.halo.app.event.theme.ThemeUpdatedEvent)3 BadRequestException (run.halo.app.exception.BadRequestException)3 ForbiddenException (run.halo.app.exception.ForbiddenException)3 NotFoundException (run.halo.app.exception.NotFoundException)3 ThemeNotSupportException (run.halo.app.exception.ThemeNotSupportException)3 ThemePropertyMissingException (run.halo.app.exception.ThemePropertyMissingException)3 ThemeUpdateException (run.halo.app.exception.ThemeUpdateException)3 List (java.util.List)2 Optional (java.util.Optional)2 Callable (java.util.concurrent.Callable)2 ExecutorService (java.util.concurrent.ExecutorService)2