Skip to content

Commit

Permalink
refactor method name
Browse files Browse the repository at this point in the history
  • Loading branch information
abhvsn committed Dec 6, 2023
1 parent fb606c2 commit aadb679
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ Mono<UpdateResult> setAppTheme(

Mono<Void> updateProtectedBranches(String applicationId, List<String> protectedBranches);

Flux<Application> filterByFields(
List<String> fieldNames, String searchString, Pageable pageable, Sort sort, AclPermission permission);
Flux<Application> filterByEntityFields(
List<String> searchableEntityFields,
String searchString,
Pageable pageable,
Sort sort,
AclPermission permission);
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,12 @@ public void addIndexInWorkspaceAndApplicationsCollection() {
dropIndexIfExists(mongoTemplate, Application.class, NAME_DELETED_COMPOUND_INDEX);
ensureIndexes(mongoTemplate, Application.class, namedDeletedAtIndex);
} catch (UncategorizedMongoDbException mongockException) {
log.debug(
log.error(
"An error occurred while creating the index : {}, skipping the addition of index because of {}.",
NAME_DELETED_COMPOUND_INDEX,
mongockException.getMessage());
} catch (Exception exception) {
log.debug("An error occurred while creating the index : {}", NAME_DELETED_COMPOUND_INDEX);
exception.printStackTrace();
log.error("An error occurred while creating the index : {}", NAME_DELETED_COMPOUND_INDEX, exception);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public Mono<SearchEntityDTO> searchEntity(
Mono<List<Workspace>> workspacesMono = Mono.just(new ArrayList<>());
if (shouldSearchEntity(Workspace.class, entities)) {
workspacesMono = workspaceService
.filterByFields(
.filterByEntityFields(
List.of(FieldName.NAME),
searchString,
pageable,
Expand All @@ -74,7 +74,7 @@ public Mono<SearchEntityDTO> searchEntity(
Mono<List<Application>> applicationsMono = Mono.just(new ArrayList<>());
if (shouldSearchEntity(Application.class, entities)) {
applicationsMono = applicationService
.filterByFields(
.filterByEntityFields(
List.of(FieldName.NAME),
searchString,
pageable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toSet;
Expand All @@ -52,6 +53,8 @@ public abstract class BaseService<

protected final AnalyticsService analyticsService;

private static final String ENTITY_FIELDS = "entity_fields";

public BaseService(
Scheduler scheduler,
Validator validator,
Expand Down Expand Up @@ -183,16 +186,34 @@ public Map<String, Object> getAnalyticsProperties(T savedResource) {
return null;
}

// Ensure the indexes are present for the fields that are being searched on.
public Flux<T> filterByFields(
List<String> fields, String searchString, Pageable pageable, Sort sort, AclPermission permission) {
List<Criteria> criteriaList = fields.stream()
.map(fieldName -> Criteria.where(fieldName).regex(".*" + searchString + ".*", "i"))
/**
* This function is used to filter the entities based on the entity fields and the search string.
* The search is performed with contains operator on the entity fields and is case-insensitive.
* @param searchableEntityFields The list of entity fields to search for. If null or empty, all entities are searched.
* @param searchString The string to search for in the entity fields.
* @param pageable The page number of the results to return.
* @param sort The sort order of the results to return.
* @param permission The permission to check for the entity.
* @return A Flux of entities.
*/
public Flux<T> filterByEntityFields(
List<String> searchableEntityFields,
String searchString,
Pageable pageable,
Sort sort,
AclPermission permission) {

if (searchableEntityFields == null || searchableEntityFields.isEmpty()) {
return Flux.error(new AppsmithException(AppsmithError.INVALID_PARAMETER, ENTITY_FIELDS));
}
List<Criteria> criteriaList = searchableEntityFields.stream()
.map(fieldName -> Criteria.where(fieldName).regex(".*" + Pattern.quote(searchString) + ".*", "i"))
.toList();
Criteria criteria = new Criteria().orOperator(criteriaList);
return repository
.queryAll(List.of(criteria), permission, sort)
.skip(pageable.getOffset())
.take(pageable.getPageSize());
Flux<T> result = repository.queryAll(List.of(criteria), permission, sort);
if (pageable != null) {
return result.skip(pageable.getOffset()).take(pageable.getPageSize());
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ public interface WorkspaceServiceCE extends CrudService<Workspace, String> {

Mono<String> getDefaultEnvironmentId(String workspaceId, AclPermission aclPermission);

Flux<Workspace> filterByFields(
List<String> fields, String searchString, Pageable pageable, Sort sort, AclPermission permission);
Flux<Workspace> filterByEntityFields(
List<String> searchableEntityFields,
String searchString,
Pageable pageable,
Sort sort,
AclPermission permission);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,23 @@
import com.appsmith.server.domains.GitApplicationMetadata;
import com.appsmith.server.domains.Workspace;
import com.appsmith.server.dtos.SearchEntityDTO;
import com.appsmith.server.helpers.CollectionUtils;
import com.appsmith.server.services.ApplicationPageService;
import com.appsmith.server.services.WorkspaceService;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.test.context.support.WithUserDetails;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@SpringBootTest
class SearchEntitySolutionTest {
Expand All @@ -33,6 +38,24 @@ class SearchEntitySolutionTest {
@Autowired
SearchEntitySolution searchEntitySolution;

private final List<String> applicationIds = new ArrayList<>();

private final List<String> workspaceIds = new ArrayList<>();

@AfterEach
public void cleanup() {
if (!CollectionUtils.isNullOrEmpty(applicationIds)) {
applicationIds.forEach(applicationId ->
applicationPageService.deleteApplication(applicationId).block());
applicationIds.clear();
}
if (!CollectionUtils.isNullOrEmpty(workspaceIds)) {
workspaceIds.forEach(
workspaceId -> workspaceService.archiveById(workspaceId).block());
workspaceIds.clear();
}
}

@Test
@WithUserDetails("api_user")
public void searchEntity_noEntitiesWithSearchString_returnsEmptyList() {
Expand Down Expand Up @@ -69,9 +92,14 @@ public void searchEntity_fetchAllEntities_entriesPresentWithSearchString_returns
final String searchString = UUID.randomUUID().toString();
Workspace workspace =
workspaceService.create(mockWorkspace(searchString)).block();
assert workspace != null;
assertNotNull(workspace, "Workspace should not be null");
workspaceIds.add(workspace.getId());
Application application = mockNonGitConnectedApplication(searchString, workspace);
applicationPageService.createApplication(application, workspace.getId()).block();
application = applicationPageService
.createApplication(application, workspace.getId())
.block();
assertNotNull(application, "Application should not be null");
applicationIds.add(application.getId());
Mono<SearchEntityDTO> searchEntityDTOMono =
searchEntitySolution.searchEntity(new String[] {}, searchString, 0, 10, true);

Expand All @@ -87,10 +115,6 @@ public void searchEntity_fetchAllEntities_entriesPresentWithSearchString_returns
assertThat(workspace1.getName()).contains(searchString);
})
.verifyComplete();

// Clean up
applicationPageService.deleteApplication(application.getId()).block();
workspaceService.archiveById(workspace.getId()).block();
}

@Test
Expand All @@ -99,10 +123,15 @@ public void searchEntity_fetchAllEntities_nullSearchString_returnsPaginatedResul
Workspace workspace = workspaceService
.create(mockWorkspace(UUID.randomUUID().toString()))
.block();
assert workspace != null;
assertNotNull(workspace, "Workspace should not be null");
workspaceIds.add(workspace.getId());
Application application =
mockNonGitConnectedApplication(UUID.randomUUID().toString(), workspace);
applicationPageService.createApplication(application, workspace.getId()).block();
application = applicationPageService
.createApplication(application, workspace.getId())
.block();
assertNotNull(application, "Application should not be null");
applicationIds.add(application.getId());
Mono<SearchEntityDTO> searchEntityDTOMono =
searchEntitySolution.searchEntity(new String[] {}, null, 0, 10, true);

Expand All @@ -112,10 +141,6 @@ public void searchEntity_fetchAllEntities_nullSearchString_returnsPaginatedResul
assertThat(searchEntityDTO.getWorkspaces()).hasSize(1);
})
.verifyComplete();

// Clean up
applicationPageService.deleteApplication(application.getId()).block();
workspaceService.archiveById(workspace.getId()).block();
}

@Test
Expand All @@ -124,9 +149,14 @@ public void searchEntity_fetchWorkspaces_entriesPresentWithSearchString_returnsP
final String searchString = UUID.randomUUID().toString();
Workspace workspace =
workspaceService.create(mockWorkspace(searchString)).block();
assert workspace != null;
assertNotNull(workspace, "Workspace should not be null");
workspaceIds.add(workspace.getId());
Application application = mockNonGitConnectedApplication(searchString, workspace);
applicationPageService.createApplication(application, workspace.getId()).block();
application = applicationPageService
.createApplication(application, workspace.getId())
.block();
assertNotNull(application, "Application should not be null");
applicationIds.add(application.getId());
Mono<SearchEntityDTO> searchEntityDTOMono = searchEntitySolution.searchEntity(
new String[] {Workspace.class.getSimpleName()}, searchString, 0, 10, true);

Expand All @@ -139,10 +169,6 @@ public void searchEntity_fetchWorkspaces_entriesPresentWithSearchString_returnsP
assertThat(workspace1.getName()).contains(searchString);
})
.verifyComplete();

// Clean up
applicationPageService.deleteApplication(application.getId()).block();
workspaceService.archiveById(workspace.getId()).block();
}

@Test
Expand All @@ -151,9 +177,15 @@ public void searchEntity_fetchApplications_entriesPresentWithSearchString_return
final String searchString = UUID.randomUUID().toString();
Workspace workspace =
workspaceService.create(mockWorkspace(searchString)).block();
assert workspace != null;
assertNotNull(workspace, "Workspace should not be null");
workspaceIds.add(workspace.getId());
Application application = mockNonGitConnectedApplication(searchString, workspace);
applicationPageService.createApplication(application, workspace.getId()).block();
application = applicationPageService
.createApplication(application, workspace.getId())
.block();
assertNotNull(application, "Application should not be null");
applicationIds.add(application.getId());

Mono<SearchEntityDTO> searchEntityDTOMono = searchEntitySolution.searchEntity(
new String[] {Application.class.getSimpleName()}, searchString, 0, 10, true);

Expand All @@ -166,10 +198,6 @@ public void searchEntity_fetchApplications_entriesPresentWithSearchString_return
assertThat(application1.getName()).isEqualTo(searchString + "_application");
})
.verifyComplete();

// Clean up
applicationPageService.deleteApplication(application.getId()).block();
workspaceService.archiveById(workspace.getId()).block();
}

@Test
Expand All @@ -178,12 +206,15 @@ public void searchEntity_fetchApplicationWithDefaultBranch_entriesPresentWithSea
final String searchString = UUID.randomUUID().toString();
Workspace workspace =
workspaceService.create(mockWorkspace(searchString)).block();
assert workspace != null;
assertNotNull(workspace, "Workspace should not be null");
workspaceIds.add(workspace.getId());

Application defaultApplication = mockGitConnectedApplication("main", "main", searchString, workspace);
defaultApplication = applicationPageService
.createApplication(defaultApplication, workspace.getId())
.block();
assert defaultApplication != null;
applicationIds.add(defaultApplication.getId());
GitApplicationMetadata metadata = defaultApplication.getGitApplicationMetadata();
metadata.setDefaultApplicationId(defaultApplication.getId());
applicationService.save(defaultApplication).block();
Expand All @@ -209,10 +240,6 @@ public void searchEntity_fetchApplicationWithDefaultBranch_entriesPresentWithSea
assertThat(application1.getName()).contains(searchString);
})
.verifyComplete();

// Clean up
applicationPageService.deleteApplication(defaultApplication.getId()).block();
workspaceService.archiveById(workspace.getId()).block();
}

private static Application mockGitConnectedApplication(
Expand Down

0 comments on commit aadb679

Please sign in to comment.