Commit 1b29fce1 authored by benjaminParisel's avatar benjaminParisel Committed by abirembaut

feat(migration): Add new migration step to add properties in style.css (#2571)

* feat(migration): Add new migration step to add properties in style.css

Covers[BS-19120](https://bonitasoft.atlassian.net/browse/BS-19120)

* fix(test): create style css for migration test

* missing style.css file for artifact test in version 1_5_51

* renamed method
parent d87941ac
......@@ -14,6 +14,19 @@
*/
package org.bonitasoft.web.designer.controller.asset;
import static com.google.common.base.Preconditions.checkArgument;
import static java.lang.String.format;
import static java.nio.file.Files.exists;
import static java.util.UUID.randomUUID;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
import static org.bonitasoft.web.designer.controller.utils.HttpFile.getOriginalFilename;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
......@@ -34,19 +47,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import static com.google.common.base.Preconditions.checkArgument;
import static java.lang.String.format;
import static java.nio.file.Files.exists;
import static java.util.UUID.randomUUID;
import static org.apache.commons.lang3.StringUtils.isNotEmpty;
import static org.bonitasoft.web.designer.controller.utils.HttpFile.getOriginalFilename;
public class AssetService<T extends Assetable> {
protected static final Logger logger = LoggerFactory.getLogger(AssetService.class);
......@@ -91,7 +91,7 @@ public class AssetService<T extends Assetable> {
.setType(assetType)
.setOrder(getNextOrder(component));
Optional<Asset> existingAsset = Iterables.<Asset>tryFind(component.getAssets(), new Predicate<Asset>() {
Optional<Asset> existingAsset = Iterables.<Asset>tryFind(component.getAssets(), new Predicate<Asset>() {
@Override
......@@ -182,6 +182,10 @@ public class AssetService<T extends Assetable> {
}
}
public String getAssetContent(T component, Asset asset) throws IOException {
return new String(assetRepository.readAllBytes(component.getId(), asset));
}
/**
* Duplicate assets when an artifact is duplicated
*/
......
......@@ -71,7 +71,7 @@ public class MigrationConfig {
StyleAssetMigrationStep styleAssetMigrationStep,
TextWidgetInterpretHTMLMigrationStep<Page> pageTextWidgetInterpretHTMLMigrationStep,
UIBootstrapAssetMigrationStep uiBootstrapAssetMigrationStep,
PageUUIDMigrationStep pageUUIDMigrationStep) {
PageUUIDMigrationStep pageUUIDMigrationStep, StyleAddModalContainerPropertiesMigrationStep styleAddModalContainerPropertiesMigrationStep) {
return asList(
new Migration<>("1.0.2", new AssetIdMigrationStep<Page>()),
new Migration<>("1.0.3", pageBondMigrationStep),
......@@ -79,7 +79,8 @@ public class MigrationConfig {
new Migration<>("1.5.7", styleAssetMigrationStep),
new Migration<>("1.5.10", uiBootstrapAssetMigrationStep),
new Migration<>("1.7.4", pageTextWidgetInterpretHTMLMigrationStep),
new Migration<>("1.7.25", pageUUIDMigrationStep));
new Migration<>("1.7.25", pageUUIDMigrationStep)
,new Migration<>("1.8.29", styleAddModalContainerPropertiesMigrationStep));
}
@Bean
......
/**
* Copyright (C) 2015 Bonitasoft S.A.
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bonitasoft.web.designer.migration;
import static java.lang.String.format;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Named;
import org.apache.commons.io.IOUtils;
import org.bonitasoft.web.designer.controller.asset.AssetService;
import org.bonitasoft.web.designer.model.asset.Asset;
import org.bonitasoft.web.designer.model.page.Page;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Named
public class StyleAddModalContainerPropertiesMigrationStep implements MigrationStep<Page> {
private static final Logger logger = LoggerFactory.getLogger(StyleAddModalContainerPropertiesMigrationStep.class);
private AssetService<Page> assetService;
@Inject
public StyleAddModalContainerPropertiesMigrationStep(@Named("pageAssetService") AssetService<Page> assetService) {
this.assetService = assetService;
}
@Override
public void migrate(Page artifact) {
for (Asset asset : artifact.getAssets()) {
if (asset.getName().equals("style.css")) {
try {
String pageStyleCssContent = assetService.getAssetContent(artifact, asset);
assetService.save(artifact, asset, getMigratedAssetContent(pageStyleCssContent));
logger.info(format(
"[MIGRATION] Adding modalContainer classes in asset [%s] to %s [%s] (introduced in 1.8.28)",
asset.getName(), artifact.getType(), artifact.getName()));
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private byte[] getMigratedAssetContent(String styleCssContent) {
try {
InputStream is = this.getClass().getClassLoader().getResourceAsStream("templates/migration/assets/css/styleAddModalContainerProperties.css");
List<InputStream> streams = Arrays.asList(
new ByteArrayInputStream(styleCssContent.getBytes()),
new ByteArrayInputStream(IOUtils.toByteArray(is)));
SequenceInputStream sis = new SequenceInputStream(Collections.enumeration(streams));
return IOUtils.toByteArray(sis);
} catch (IOException e) {
throw new RuntimeException("Missing templates/page/assets/css/style.css from classpath", e);
}
}
}
.bonita-modal{
overflow-y: auto;
}
.modal-open .datepicker,
.modal-open .timepicker {
z-index: 9999;
}
......@@ -486,4 +486,19 @@ public class AssetServiceTest {
verify(assetRepository).refreshAssets(page);
}
@Test
public void should_read_asset_content() throws IOException {
Asset myAsset = anAsset().withType(CSS).withName("style.css").build();
Page page = aPage()
.withVersion("1.7.9").withAsset(myAsset).build();
when(assetRepository.readAllBytes(page.getId(), myAsset)).thenReturn("myContentOfAsset".getBytes());
String content = assetService.getAssetContent(page, myAsset);
assertThat(content).isEqualTo("myContentOfAsset");
}
}
/**
* Copyright (C) 2015 Bonitasoft S.A.
* Bonitasoft, 32 rue Gustave Eiffel - 38000 Grenoble
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.0 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.bonitasoft.web.designer.migration;
import static org.bonitasoft.web.designer.builder.AssetBuilder.anAsset;
import static org.bonitasoft.web.designer.builder.PageBuilder.aPage;
import static org.bonitasoft.web.designer.model.asset.AssetType.CSS;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.InputStream;
import org.apache.commons.io.IOUtils;
import org.bonitasoft.web.designer.controller.asset.AssetService;
import org.bonitasoft.web.designer.model.asset.Asset;
import org.bonitasoft.web.designer.model.page.Page;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
@RunWith(MockitoJUnitRunner.class)
public class StyleAddModalContainerPropertiesMigrationStepTest {
@Mock
private AssetService<Page> pageAssetService;
@InjectMocks
private StyleAddModalContainerPropertiesMigrationStep step;
@Before
public void setUp() throws Exception {
step = new StyleAddModalContainerPropertiesMigrationStep(pageAssetService);
}
private Asset expectedAsset(String name) {
return anAsset().withType(CSS).withName(name).build();
}
@Test
public void should_migrate_style_asset_to_add_new_modal_container_css_classes() throws Exception {
Asset style = anAsset().withType(CSS).withName("style.css").build();
String initContent = ".my-content {background: red}";
Page page = aPage()
.withVersion("1.7.9").withAsset(style).build();
when(pageAssetService.getAssetContent(page, style)).thenReturn(initContent);
InputStream is = this.getClass().getClassLoader().getResourceAsStream("templates/migration/assets/css/styleAddModalContainerProperties.css");
String content = initContent.concat(IOUtils.toString(is));
step.migrate(page);
verify(pageAssetService).save(page, expectedAsset("style.css"), content.getBytes());
}
}
......@@ -20,9 +20,9 @@ import static com.google.common.collect.Iterables.transform;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Map;
import javax.inject.Inject;
import com.google.common.base.Function;
import org.bonitasoft.web.designer.ApplicationConfig;
import org.bonitasoft.web.designer.migration.page.UIBootstrapAssetMigrationStep;
import org.bonitasoft.web.designer.model.asset.Asset;
......@@ -40,8 +40,6 @@ import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import com.google.common.base.Function;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { ApplicationConfig.class })
@WebAppConfiguration("file:target/test-classes")
......@@ -59,7 +57,7 @@ public class WorkspaceMigrationTest {
@Inject
WidgetRepository widgetRepository;
String PAGE_HIGHER_MIGRATION_VERSION = "1.7.25";
String PAGE_HIGHER_MIGRATION_VERSION = "1.8.29";
String WIDGET_HIGHER_MIGRATION_VERSION = "1.2.9";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment