Skip to content

Commit

Permalink
update with description/reasoning of customFields and reduce new test…
Browse files Browse the repository at this point in the history
… ot only test the JSON
  • Loading branch information
Joar Varpe authored and Joar Varpe committed Nov 6, 2024
1 parent 981f726 commit c1bfb1c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,15 @@ public void validate() {
this.env.validate();
}

// Add customFields
/**
* The customFields feature in Spring Initializr enables additional, adaptable metadata beyond the standard fields.
* This allows organizations to set environment-specific values like default configurations, dependencies,
* version control, or deployment options—aligning generated projects directly with their standards.
* By using customFields, organizations can add new metadata flexibly without altering core configurations,
* making Initializr more adaptable and valuable for various use cases.
*/
private Map<String, Object> customFields = Collections.emptyMap();

// Getter and Setter
public Map<String, Object> getCustomFields() {
return this.customFields;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,63 +16,25 @@

package io.spring.initializr.web.controller;

import io.spring.initializr.metadata.InitializrMetadata;
import io.spring.initializr.metadata.InitializrMetadataBuilder;
import io.spring.initializr.metadata.InitializrMetadataProvider;
import io.spring.initializr.web.AbstractFullStackInitializrIntegrationTests;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.web.client.HttpClientErrorException;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Integration tests for {@link ProjectMetadataController} on a real http server.
*
* @author Stephane Nicoll
* @author Joar Varpe
*/
@ActiveProfiles("test-custom-fields")
class ProjectMetadataControllerCustomFieldsIntegrationTests extends AbstractFullStackInitializrIntegrationTests {

@Autowired
private InitializrMetadataProvider metadataProvider;

@Test
void initializeRemoteConfig() throws Exception {
InitializrMetadata localMetadata = this.metadataProvider.get();
InitializrMetadata metadata = InitializrMetadataBuilder.create()
.withInitializrMetadata(new UrlResource(createUrl("/metadata/config")))
.build();
// Basic assertions
assertThat(metadata.getDependencies().getContent()).hasSameSizeAs(localMetadata.getDependencies().getContent());
assertThat(metadata.getTypes().getContent()).hasSameSizeAs(localMetadata.getTypes().getContent());
assertThat(metadata.getBootVersions().getContent()).hasSameSizeAs(localMetadata.getBootVersions().getContent());
assertThat(metadata.getPackagings().getContent()).hasSameSizeAs(localMetadata.getPackagings().getContent());
assertThat(metadata.getJavaVersions().getContent()).hasSameSizeAs(localMetadata.getJavaVersions().getContent());
assertThat(metadata.getLanguages().getContent()).hasSameSizeAs(localMetadata.getLanguages().getContent());
}

@Test
void textPlainNotAccepted() {
try {
execute("/metadata/config", String.class, null, "text/plain");
}
catch (HttpClientErrorException ex) {
assertThat(ex.getStatusCode()).isEqualTo(HttpStatus.NOT_ACCEPTABLE);
}
}

@Test
void validateJson() throws JSONException {
ResponseEntity<String> response = execute("/metadata/config", String.class, null, "application/json");
Expand All @@ -81,47 +43,4 @@ void validateJson() throws JSONException {
JSONObject expected = readJsonFrom("metadata/config/test-custom-fields.json");
JSONAssert.assertEquals(expected, json, JSONCompareMode.STRICT);
}

@Test
void metadataClientEndpoint() {
ResponseEntity<String> response = execute("/metadata/client", String.class, null, "application/json");
validateDefaultMetadata(response);
}

@Test
void dependenciesNoAcceptHeaderWithNoBootVersion() throws JSONException {
validateDependenciesMetadata("*/*", DEFAULT_METADATA_MEDIA_TYPE);
}

@Test
void dependenciesV21WithNoBootVersion() throws JSONException {
validateDependenciesMetadata("application/vnd.initializr.v2.1+json", DEFAULT_METADATA_MEDIA_TYPE);
}

@Test
void dependenciesV22WithNoBootVersion() throws JSONException {
validateDependenciesMetadata("application/vnd.initializr.v2.2+json", CURRENT_METADATA_MEDIA_TYPE);
}

private void validateDependenciesMetadata(String acceptHeader, MediaType expectedMediaType) throws JSONException {
ResponseEntity<String> response = execute("/dependencies", String.class, null, acceptHeader);
assertThat(response.getHeaders().getFirst(HttpHeaders.ETAG)).isNotNull();
validateContentType(response, expectedMediaType);
validateDependenciesOutput("2.4.4", response.getBody());
}

@Test
void filteredDependencies() throws JSONException {
ResponseEntity<String> response = execute("/dependencies?bootVersion=2.6.1", String.class, null,
"application/json");
assertThat(response.getHeaders().getFirst(HttpHeaders.ETAG)).isNotNull();
validateContentType(response, DEFAULT_METADATA_MEDIA_TYPE);
validateDependenciesOutput("2.6.1", response.getBody());
}

protected void validateDependenciesOutput(String version, String actual) throws JSONException {
JSONObject expected = readJsonFrom("metadata/dependencies/test-dependencies-" + version + ".json");
JSONAssert.assertEquals(expected, new JSONObject(actual), JSONCompareMode.STRICT);
}

}

0 comments on commit c1bfb1c

Please sign in to comment.