Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

control-service: fix graphql team filter not retrieveing special chars #863

Merged
merged 8 commits into from
Jun 21, 2022
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);
}
}