Skip to content

Commit

Permalink
fix(advanced application): fix filtering in REST API (#3030)
Browse files Browse the repository at this point in the history
* fix(advanced application): fix filtering
* convert field to boolean in ApplicationSearchDescriptorConverter
  • Loading branch information
abirembaut authored Jun 11, 2024
1 parent b1f40d2 commit e412535
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
package org.bonitasoft.web.rest.server.api.application;

import static org.bonitasoft.web.rest.server.api.page.builder.PageItemBuilder.aPageItem;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.*;
import static org.mockito.Mockito.spy;

import java.io.File;
Expand All @@ -37,11 +36,7 @@
import org.bonitasoft.engine.search.SearchOptions;
import org.bonitasoft.engine.search.SearchOptionsBuilder;
import org.bonitasoft.engine.session.APISession;
import org.bonitasoft.web.rest.model.application.AbstractApplicationItem;
import org.bonitasoft.web.rest.model.application.AdvancedApplicationDefinition;
import org.bonitasoft.web.rest.model.application.AdvancedApplicationItem;
import org.bonitasoft.web.rest.model.application.ApplicationDefinition;
import org.bonitasoft.web.rest.model.application.ApplicationItem;
import org.bonitasoft.web.rest.model.application.*;
import org.bonitasoft.web.rest.model.portal.page.PageItem;
import org.bonitasoft.web.rest.server.api.applicationpage.APIApplicationDataStoreFactory;
import org.bonitasoft.web.rest.server.datastore.application.ApplicationDataStoreCreator;
Expand Down Expand Up @@ -127,27 +122,12 @@ public void add_AdvancedApplication_is_forbidden() {
@Test
public void should_add_LegacyApplication() throws Exception {
// Given
final PageItem pageItem = addPage(HOME_PAGE_ZIP);
final PageItem layout = addPage(LAYOUT_ZIP);
final PageItem theme = addPage(THEME_ZIP);
final ApplicationItem legacyItem = ApplicationDefinition.get().createItem();
legacyItem.setToken("tokenLegacy");
legacyItem.setDisplayName("Legacy");
legacyItem.setVersion("1.0");
legacyItem.setProfileId(2L);
legacyItem.setState("ACTIVATED");
// That's the default and gets saved as -1
// legacyItem.setHomePageId(pageItem.getId().toLong());
legacyItem.setLayoutId(layout.getId().toLong());
legacyItem.setThemeId(theme.getId().toLong());

// When
var createdItem = apiApplication.add(legacyItem);
var legacyApp = createLegacyApplication();

// Then
Map<String, String> attributes = new HashMap<>(legacyItem.getAttributes().size());
legacyItem.getAttributes().keySet().forEach(k -> attributes.put(k, createdItem.getAttributes().get(k)));
Assert.assertEquals(new HashMap<>(legacyItem.getAttributes()), attributes);
Map<String, String> attributes = new HashMap<>(legacyApp.getAttributes().size());
legacyApp.getAttributes().keySet().forEach(k -> attributes.put(k, legacyApp.getAttributes().get(k)));
Assert.assertEquals(new HashMap<>(legacyApp.getAttributes()), attributes);
}

@Test
Expand All @@ -171,6 +151,33 @@ public void update_AdvancedApplication_is_forbidden_and_not_effective() throws E
@Test
public void should_update_LegacyApplication() throws Exception {
// Given
var legacyApp = createLegacyApplication();

// When
Map<String, String> attributes = Map.of(AbstractApplicationItem.ATTRIBUTE_DISPLAY_NAME, "Legacy Updated");
var updatedItem = apiApplication.update(legacyApp.getId(), attributes);

// Then
assertEquals("Legacy Updated", updatedItem.getDisplayName());
}

@Test
public void should_search_filter_AdvancedApplications() throws Exception {
// Given
var legacyApp = createLegacyApplication();

// When
final String search = legacyApp.getDisplayName();
final String orders = ApplicationItem.ATTRIBUTE_TOKEN + " DESC";
final HashMap<String, String> filters = new HashMap<>();
filters.put(ApplicationItem.ATTRIBUTE_ADVANCED, "true");
var searchResult = apiApplication.search(0, 1, search, orders, filters);

// Then
assertTrue(searchResult.getResults().isEmpty());
}

private AbstractApplicationItem createLegacyApplication() throws Exception {
final PageItem pageItem = addPage(HOME_PAGE_ZIP);
final PageItem layout = addPage(LAYOUT_ZIP);
final PageItem theme = addPage(THEME_ZIP);
Expand All @@ -185,14 +192,7 @@ public void should_update_LegacyApplication() throws Exception {
legacyItem.setLayoutId(layout.getId().toLong());
legacyItem.setThemeId(theme.getId().toLong());

var createdItem = apiApplication.add(legacyItem);
Map<String, String> attributes = Map.of(AbstractApplicationItem.ATTRIBUTE_DISPLAY_NAME, "Legacy Updated");

// When
var updatedItem = apiApplication.update(createdItem.getId(), attributes);

// Then
assertEquals("Legacy Updated", updatedItem.getDisplayName());
return apiApplication.add(legacyItem);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
**/
package org.bonitasoft.web.rest.server.datastore.application;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

Expand All @@ -28,6 +27,8 @@ public class ApplicationSearchDescriptorConverter implements AttributeConverter

private final Map<String, String> mapping;

private final Map<String, ItemAttribute.TYPE> valueTypeMapping = new HashMap<>();

public ApplicationSearchDescriptorConverter() {
mapping = createMapping();
}
Expand All @@ -36,6 +37,7 @@ private Map<String, String> createMapping() {
final Map<String, String> mapping = new HashMap<>();
mapping.put(AbstractApplicationItem.ATTRIBUTE_ID, ApplicationSearchDescriptor.ID);
mapping.put(AbstractApplicationItem.ATTRIBUTE_ADVANCED, ApplicationSearchDescriptor.ADVANCED);
valueTypeMapping.put(AbstractApplicationItem.ATTRIBUTE_ADVANCED, ItemAttribute.TYPE.BOOLEAN);
mapping.put(AbstractApplicationItem.ATTRIBUTE_TOKEN, ApplicationSearchDescriptor.TOKEN);
mapping.put(AbstractApplicationItem.ATTRIBUTE_DISPLAY_NAME, ApplicationSearchDescriptor.DISPLAY_NAME);
mapping.put(AbstractApplicationItem.ATTRIBUTE_STATE, ApplicationSearchDescriptor.STATE);
Expand All @@ -60,7 +62,7 @@ public String convert(final String attribute) {

@Override
public Map<String, ItemAttribute.TYPE> getValueTypeMapping() {
return Collections.emptyMap();
return valueTypeMapping;
}

}

0 comments on commit e412535

Please sign in to comment.