Skip to content

Commit

Permalink
fix(Rest): Allowing search for externalIds without encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
eldrin30 committed Jul 7, 2023
1 parent ed8ce98 commit f237ecd
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@
import org.eclipse.sw360.http.RequestBuilder;
import org.eclipse.sw360.http.utils.HttpUtils;

import org.eclipse.sw360.http.RequestBuilder;
import org.eclipse.sw360.http.utils.HttpUtils;
import org.eclipse.sw360.clients.config.SW360ClientConfig;
import org.eclipse.sw360.clients.auth.AccessTokenProvider;
import org.eclipse.sw360.clients.utils.SW360ResourceUtils;
import org.eclipse.sw360.clients.rest.resource.releases.SW360Release;
import org.eclipse.sw360.clients.rest.resource.releases.SW360ReleaseList;
import org.eclipse.sw360.clients.rest.resource.releases.SW360SparseRelease;

/**
* <p>
* An SW360 REST client implementation providing basic functionality related to
Expand Down Expand Up @@ -102,11 +111,16 @@ public CompletableFuture<SW360Release> getRelease(String releaseId) {
// which are mapped to numbered keys like `hash_1=...`, `hash_2=...`, ...
// but can change in the order of the values
public CompletableFuture<List<SW360SparseRelease>> getReleasesByExternalIds(Map<String, ?> externalIds) {
return executeJsonRequestNew(builder -> builder.method(RequestBuilder.Method.GET)
.uri(resourceUrl(RELEASES_ENDPOINT_APPENDIX, PATH_SEARCH_EXT_IDS)).body(body -> body.json(externalIds)),
SW360ReleaseList.class, TAG_GET_RELEASES_BY_EXTERNAL_IDS)
String url = getExternalIdUrl(externalIds);
return executeJsonRequestWithDefault(HttpUtils.get(url), SW360ReleaseList.class,
TAG_GET_RELEASES_BY_EXTERNAL_IDS, SW360ReleaseList::new)
.thenApply(SW360ResourceUtils::getSw360SparseReleases);
}

private String getExternalIdUrl(Map<String, ?> externalIds) {
return HttpUtils.addQueryParameters(resourceUrl(RELEASES_ENDPOINT_APPENDIX, PATH_SEARCH_EXT_IDS),
externalIds);
}

/**
* Creates a new release resource based on the data object passed in and
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl;
import static com.github.tomakehurst.wiremock.client.WireMock.delete;
Expand All @@ -53,6 +52,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;


public class SW360ReleaseClientIT extends AbstractMockServerTest {
/**
* Defines the expected test release data from the test JSON file.
Expand Down Expand Up @@ -159,14 +159,15 @@ public void testGetReleaseEmptyBody() {
public void testGetReleasesByExternalIds() throws IOException {
if(!RUN_REST_INTEGRATION_TEST) {
Map<String, Object> idMap = new LinkedHashMap<>();
idMap.put("id+1", "testRelease");
idMap.put("id1", "testRelease");
idMap.put("id2", "otherFilter");
wireMockRule.stubFor(get(urlPathEqualTo("/releases/searchByExternalIds"))
.withRequestBody(equalToJson("{\"id+1\": \"testRelease\", \"id2\": \"otherFilter\"}"))
.withQueryParam("id1", equalTo("testRelease"))
.withQueryParam("id2", equalTo("otherFilter"))
.willReturn(aJsonResponse(HttpConstants.STATUS_OK)
.withBodyFile("all_releases.json")));

List<SW360SparseRelease> releases = waitFor(releaseClientAlt.getReleasesByExternalIds(idMap));
List<SW360SparseRelease> releases = waitFor(releaseClient.getReleasesByExternalIds(idMap));
checkReleaseData(releases);
} else {
Map<String, List<String>> externalIds = new LinkedHashMap<>();
Expand All @@ -183,7 +184,7 @@ public void testGetReleasesByExternalIds() throws IOException {
sw360Release.setComponentId(createdComponent.getId());
sw360Release.setVersion("1.1");
SW360Release release = waitFor(releaseClient.createRelease(sw360Release));
List<SW360SparseRelease> releases = waitFor(releaseClientAlt.getReleasesByExternalIds(externalIds));
List<SW360SparseRelease> releases = waitFor(releaseClient.getReleasesByExternalIds(idMap));
assertEquals(releases.size(), 1);
cleanupRelease(release, releaseClient);
cleanupComponent(componentClient);
Expand All @@ -195,7 +196,7 @@ public void testGetReleasesByExternalIdsStatusNoContent() throws IOException {
wireMockRule.stubFor(get(urlPathEqualTo("/releases/searchByExternalIds"))
.willReturn(aResponse().withStatus(HttpConstants.STATUS_NO_CONTENT)));

List<SW360SparseRelease> releases = waitFor(releaseClientAlt.getReleasesByExternalIds(new HashMap<String, List<String>>()));
List<SW360SparseRelease> releases = waitFor(releaseClient.getReleasesByExternalIds(new HashMap<>()));
assertThat(releases).isEmpty();
}

Expand All @@ -206,11 +207,11 @@ public void testGetReleasesByExternalIdsError() throws IOException {
.willReturn(aJsonResponse(HttpConstants.STATUS_ERR_BAD_REQUEST)));

FailedRequestException exception = expectFailedRequest(
releaseClientAlt.getReleasesByExternalIds(new HashMap<String, List<String>>()), HttpConstants.STATUS_ERR_BAD_REQUEST);
releaseClient.getReleasesByExternalIds(new HashMap<>()), HttpConstants.STATUS_ERR_BAD_REQUEST);
assertThat(exception.getTag()).isEqualTo(SW360ReleaseClient.TAG_GET_RELEASES_BY_EXTERNAL_IDS);
} else {
cleanupComponent(componentClient);
List<SW360SparseRelease> releases = waitFor(releaseClientAlt.getReleasesByExternalIds(new HashMap<String, List<String>>()));
List<SW360SparseRelease> releases = waitFor(releaseClient.getReleasesByExternalIds(new HashMap<>()));
assertEquals(releases.size(), 0);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import org.springframework.http.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
Expand Down Expand Up @@ -247,10 +248,30 @@ public ResponseEntity<CollectionModel<EntityModel>> getUsedByResourceDetails(@Pa
}

@GetMapping(value = RELEASES_URL + "/searchByExternalIds")
public ResponseEntity searchByExternalIds(@RequestBody(required = false) Map<String, List<String>> externalIdsMultiMap) throws TException {
public ResponseEntity searchByExternalIds(HttpServletRequest request) throws TException {
String queryString = request.getQueryString();
MultiValueMap<String, String> externalIdsMultiMap = parseQueryString(queryString);
return restControllerHelper.searchByExternalIds(new LinkedMultiValueMap<String, String>(externalIdsMultiMap), releaseService, null);
}

private MultiValueMap<String, String> parseQueryString(String queryString) {
MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();

if (queryString != null && !queryString.isEmpty()) {
String[] params = queryString.split("&");
for (String param : params) {
String[] keyValue = param.split("=");
if (keyValue.length == 2) {
String key = keyValue[0];
String value = keyValue[1];
parameters.add(key, value);
}
}
}

return parameters;
}

@PreAuthorize("hasAuthority('WRITE')")
@DeleteMapping(value = RELEASES_URL + "/{ids}")
public ResponseEntity<List<MultiStatus>> deleteReleases(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -744,7 +744,7 @@ public void should_document_get_releases_by_externalIds() throws Exception {
MultiValueMap<String, String> externalIds = new LinkedMultiValueMap<>();
externalIds.put("mainline-id-component", List.of("1432","4876"));
String accessToken = TestHelper.getAccessToken(mockMvc, testUserId, testUserPassword);
mockMvc.perform(get("/api/releases/searchByExternalIds")
mockMvc.perform(get("/api/releases/searchByExternalIds?mainline-id-component=1432&mainline-id-component=4876")
.contentType(MediaTypes.HAL_JSON)
.content(this.objectMapper.writeValueAsString(externalIds))
.header("Authorization", "Bearer " + accessToken))
Expand Down

0 comments on commit f237ecd

Please sign in to comment.