Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mhalbritter committed Feb 15, 2024
1 parent 4787f4d commit b167522
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,27 @@ class SbomEndpointTests {

private SbomProperties properties;

private SbomEndpoint endpoint;

@BeforeEach
void setUp() {
this.properties = new SbomProperties();
this.endpoint = new SbomEndpoint(this.properties, new GenericApplicationContext());
}

@Test
void shouldUseLocationFromProperties() throws IOException {
this.properties.getApplication().setLocation("classpath:cyclonedx.json");
String content = this.endpoint.sbom("application").getContentAsString(StandardCharsets.UTF_8);
assertThat(content).contains("\"spdxVersion\" : \"SPDX-2.3\"");
String content = createEndpoint().sbom("application").getContentAsString(StandardCharsets.UTF_8);
assertThat(content).contains("\"bomFormat\" : \"CycloneDX\"");
}

@Test
void shouldFailIfNonExistingLocationIsGiven() {
this.properties.getApplication().setLocation("classpath:does-not-exist.json");
assertThatIllegalStateException().isThrownBy(() -> this.endpoint.sbom("application"))
assertThatIllegalStateException().isThrownBy(() -> createEndpoint().sbom("application"))
.withMessageContaining("Resource 'classpath:does-not-exist.json' doesn't exist");
}

private SbomEndpoint createEndpoint() {
return new SbomEndpoint(this.properties, new GenericApplicationContext());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

package org.springframework.boot.actuate.sbom;

import java.io.IOException;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -37,48 +35,49 @@ class SbomEndpointWebExtensionTests {

private SbomProperties properties;

private SbomEndpointWebExtension webExtension;

@BeforeEach
void setUp() {
this.properties = new SbomProperties();
SbomEndpoint endpoint = new SbomEndpoint(this.properties, new GenericApplicationContext());
this.webExtension = new SbomEndpointWebExtension(endpoint, this.properties);
}

@Test
void shouldReturnHttpOk() throws IOException {
void shouldReturnHttpOk() {
this.properties.getApplication().setLocation("classpath:cyclonedx.json");
WebEndpointResponse<Resource> response = this.webExtension.sbom("application");
WebEndpointResponse<Resource> response = createWebExtension().sbom("application");
assertThat(response.getStatus()).isEqualTo(200);
}

@Test
void shouldReturnNotFoundIfResourceDoesntExist() {
WebEndpointResponse<Resource> response = this.webExtension.sbom("application");
WebEndpointResponse<Resource> response = createWebExtension().sbom("application");
assertThat(response.getStatus()).isEqualTo(404);
}

@Test
void shouldAutoDetectContentTypeForCycloneDx() {
this.properties.getApplication().setLocation("classpath:cyclonedx.json");
WebEndpointResponse<Resource> response = this.webExtension.sbom("application");
WebEndpointResponse<Resource> response = createWebExtension().sbom("application");
assertThat(response.getContentType()).isEqualTo(MimeType.valueOf("application/vnd.cyclonedx+json"));
}

@Test
void shouldSupportUnknownFiles() {
this.properties.getApplication().setLocation("classpath:git.properties");
WebEndpointResponse<Resource> response = this.webExtension.sbom("application");
WebEndpointResponse<Resource> response = createWebExtension().sbom("application");
assertThat(response.getContentType()).isNull();
}

@Test
void shouldUseContentTypeIfSet() {
this.properties.getApplication().setLocation("classpath:spdx.json");
this.properties.getApplication().setLocation("classpath:cyclonedx.json");
this.properties.getApplication().setMediaType(MimeType.valueOf("text/plain"));
WebEndpointResponse<Resource> response = this.webExtension.sbom("application");
WebEndpointResponse<Resource> response = createWebExtension().sbom("application");
assertThat(response.getContentType()).isEqualTo(MimeType.valueOf("text/plain"));
}

private SbomEndpointWebExtension createWebExtension() {
SbomEndpoint endpoint = new SbomEndpoint(this.properties, new GenericApplicationContext());
return new SbomEndpointWebExtension(endpoint, this.properties);
}

}

0 comments on commit b167522

Please sign in to comment.