Merge pull request #1039 from bono007

* pr/1039:
  Polish "Handle metadata resolution failures properly"
  Handle metadata resolution failures properly

Closes gh-1039
This commit is contained in:
Stephane Nicoll
2019-12-27 16:55:01 +01:00
4 changed files with 31 additions and 5 deletions

View File

@@ -185,12 +185,13 @@ public class BillOfMaterials {
* additional BOMs to use, if any.
* @param bootVersion the Spring Boot version
* @return the bill of materials
* @throws InvalidInitializrMetadataException if no suitable mapping is found for that
* version
*/
public BillOfMaterials resolve(Version bootVersion) {
if (this.mappings.isEmpty()) {
return this;
}
for (Mapping mapping : this.mappings) {
if (mapping.range.match(bootVersion)) {
BillOfMaterials resolvedBom = new BillOfMaterials(
@@ -205,7 +206,8 @@ public class BillOfMaterials {
return resolvedBom;
}
}
throw new IllegalStateException("No suitable mapping was found for " + this + " and version " + bootVersion);
throw new InvalidInitializrMetadataException(
"No suitable mapping was found for " + this + " and version " + bootVersion);
}
@Override

View File

@@ -24,7 +24,7 @@ import io.spring.initializr.metadata.BillOfMaterials.Mapping;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* @author Stephane Nicoll
@@ -119,8 +119,8 @@ class BillOfMaterialsTests {
bom.getMappings().add(Mapping.create("[1.2.0.RELEASE,1.3.0.M1)", "1.1.0"));
bom.getMappings().add(Mapping.create("[1.3.0.M1, 1.4.0.M1)", "1.2.0"));
bom.validate();
assertThatIllegalStateException().isThrownBy(() -> bom.resolve(Version.parse("1.4.1.RELEASE")))
.withMessageContaining("1.4.1.RELEASE");
assertThatExceptionOfType(InvalidInitializrMetadataException.class)
.isThrownBy(() -> bom.resolve(Version.parse("1.4.1.RELEASE"))).withMessageContaining("1.4.1.RELEASE");
}
@Test

View File

@@ -16,13 +16,17 @@
package io.spring.initializr.web.controller;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletResponse;
import io.spring.initializr.generator.version.Version;
import io.spring.initializr.metadata.DependencyMetadata;
import io.spring.initializr.metadata.DependencyMetadataProvider;
import io.spring.initializr.metadata.InitializrMetadata;
import io.spring.initializr.metadata.InitializrMetadataProvider;
import io.spring.initializr.metadata.InvalidInitializrMetadataException;
import io.spring.initializr.web.mapper.DependencyMetadataV21JsonMapper;
import io.spring.initializr.web.mapper.InitializrMetadataJsonMapper;
import io.spring.initializr.web.mapper.InitializrMetadataV21JsonMapper;
@@ -30,9 +34,11 @@ import io.spring.initializr.web.mapper.InitializrMetadataV2JsonMapper;
import io.spring.initializr.web.mapper.InitializrMetadataVersion;
import org.springframework.http.CacheControl;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@@ -85,6 +91,12 @@ public class ProjectMetadataController extends AbstractMetadataController {
return dependenciesFor(InitializrMetadataVersion.V2_1, bootVersion);
}
@ExceptionHandler
public void invalidMetadataRequest(HttpServletResponse response, InvalidInitializrMetadataException ex)
throws IOException {
response.sendError(HttpStatus.BAD_REQUEST.value(), ex.getMessage());
}
/**
* Return the {@link CacheControl} response headers to use for the specified
* {@link InitializrMetadata metadata}. If no cache should be applied

View File

@@ -60,6 +60,18 @@ public class ProjectMetadataControllerIntegrationTests extends AbstractInitializ
validateMetadata(response, InitializrMetadataVersion.V2.getMediaType(), "2.0.0", JSONCompareMode.STRICT);
}
@Test
void metadataWithInvalidPlatformVersion() {
try {
execute("/dependencies?bootVersion=1.5.17.RELEASE", String.class, "application/vnd.initializr.v2.1+json",
"application/json");
}
catch (HttpClientErrorException ex) {
assertThat(ex.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST);
assertThat(ex.getResponseBodyAsString().contains("1.5.17.RELEASE"));
}
}
@Test
void metadataWithCurrentAcceptHeader() {
getRequests().setFields("_links.maven-project", "dependencies.values[0]", "type.values[0]",