Skip to content

Commit

Permalink
control-service: fix graphql team filter not retrieveing special chars (
Browse files Browse the repository at this point in the history
#863)

why: users of control-service complained that graphQL API calls
for a team with special characters such as '(' don't yeild results
even though the team exists.

what: investigating the issue concluded that the team filter strategy
uses a regex method - String.matches("regex") string to
match the teams and would get the results tripped by special chars.

testing: added unit test.

Signed-off-by: Momchil Zhivkov [email protected]
  • Loading branch information
Momchil Z authored Jun 21, 2022
1 parent 04c3f2d commit eb991a5
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ public Criteria<V2DataJob> computeFilterCriteria(@NonNull Criteria<V2DataJob> cr
// Only the team field currently support the 'equals' and 'like' operator
String part = filter.getPattern();
part = part.replace("%", ".*").trim();
return config.getTeam().toLowerCase().matches(part.toLowerCase());
var configTeamLower = config.getTeam().toLowerCase();
var partLower = part.toLowerCase();

return configTeamLower.matches(partLower) || configTeamLower.trim().equals(partLower.trim());
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright 2021 VMware, Inc.
* SPDX-License-Identifier: Apache-2.0
*/

package com.vmware.taurus.service.graphql;

import com.vmware.taurus.ControlplaneApplication;
import com.vmware.taurus.service.JobsRepository;
import com.vmware.taurus.service.model.DataJob;
import com.vmware.taurus.service.model.JobConfig;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = ControlplaneApplication.class)
@AutoConfigureMockMvc(addFilters = false)
public class GraphQLJobTeamFetcherIT {

@Autowired
JobsRepository jobsRepository;

@Autowired
private MockMvc mockMvc;

@AfterEach
public void cleanup() {
jobsRepository.deleteAll();
}

private static String getJobsUri(String teamName) {
return "/data-jobs/for-team/" + teamName + "/jobs";
}

private static String getQuery(String jobTeam) {
return "{\n" +
" jobs(\n" +
" pageNumber: 1\n" +
" pageSize: 100\n" +
" filter: [\n" +
" { property: \"config.team\", pattern: \"" + jobTeam + "\", sort: ASC }\n" +
" { property: \"jobName\", sort: ASC }\n" +
" ]\n" +
" ) {\n" +
" content {\n" +
" jobName\n" +
" config {\n" +
" team\n" +
" description\n" +
" schedule {\n" +
" scheduleCron\n" +
" }\n" +
" }\n" +
" deployments {\n" +
" enabled\n" +
" status\n" +
" }\n" +
" }\n" +
" }\n" +
"}";
}

@Test
public void testRetrieveJobNameWithParentheses() throws Exception {
testJobApiRetrievalWithTeamName("VIDA (Eco-system)");
}

@Test
public void testRetrieveJobNameWithoutParentheses() throws Exception {
testJobApiRetrievalWithTeamName("VIDA");
}

/**
* Re-usable test that creates a data job with a given
* team name and attempts to retrieve it via the graphQL
* API
*
* @param jobTeam - the team name.
* @throws Exception
*/
private void testJobApiRetrievalWithTeamName(String jobTeam) throws Exception {
createJobWithTeam(jobTeam);
mockMvc.perform(MockMvcRequestBuilders.get(getJobsUri(jobTeam))
.queryParam("query", getQuery(jobTeam))
.with(user("test")))
.andExpect(status().is(200))
.andExpect(jsonPath("$.data.content[0].jobName").value("test-job"))
.andExpect(jsonPath("$.data.content[0].config.team").value(jobTeam));
}

private void createJobWithTeam(String jobTeam) {
DataJob dataJob = new DataJob();
JobConfig jobConfig = new JobConfig();
jobConfig.setTeam(jobTeam);
dataJob.setName("test-job");
dataJob.setJobConfig(jobConfig);
jobsRepository.save(dataJob);
}
}

0 comments on commit eb991a5

Please sign in to comment.