-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(config): add test docs for /config actuator
Signed-off-by: Gaurav Mishra <[email protected]>
- Loading branch information
Showing
9 changed files
with
236 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// | ||
// Copyright Siemens AG, 2024. Part of the SW360 Portal Project. | ||
// | ||
// This program and the accompanying materials are made | ||
// available under the terms of the Eclipse Public License 2.0 | ||
// which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
// | ||
// SPDX-License-Identifier: EPL-2.0 | ||
// | ||
[[resources-config]] | ||
=== Config | ||
|
||
The config resource is used to give information from the `sw360.properties` file. | ||
The configurations exposed are useful for the frontend UI. The backend only configs are not exposed. | ||
|
||
[[resources-config-get]] | ||
==== Getting config | ||
|
||
A `GET` request will get the service's configurations. | ||
|
||
===== Example request | ||
include::{snippets}/should_document_get_config/curl-request.adoc[] | ||
|
||
The response structure is the config object of the config actuator. | ||
|
||
===== Response structure | ||
include::{snippets}/should_document_get_config/response-fields.adoc[] | ||
|
||
===== Example response | ||
include::{snippets}/should_document_get_config/http-response.adoc[] | ||
|
||
The response structure is not complete as it will grow overtime. But it is a key-value pair of strings. | ||
|
||
[[resources-config-get-single]] | ||
==== Getting single config value | ||
|
||
A `GET` request will get the service's single configuration. | ||
|
||
===== Request Parameters | ||
include::{snippets}/should_document_get_single_config/path-parameters.adoc[] | ||
|
||
===== Example request | ||
include::{snippets}/should_document_get_single_config/curl-request.adoc[] | ||
|
||
===== Example response | ||
include::{snippets}/should_document_get_single_config/http-response.adoc[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...src/test/java/org/eclipse/sw360/rest/resourceserver/actuator/SW360ConfigActuatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright Siemens AG, 2024. Part of the SW360 Portal Project. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.sw360.rest.resourceserver.actuator; | ||
|
||
import org.eclipse.sw360.rest.resourceserver.Sw360ResourceServer; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.SpyBean; | ||
import org.springframework.boot.test.web.client.TestRestTemplate; | ||
import org.springframework.boot.test.web.server.LocalServerPort; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.BDDAssertions.then; | ||
|
||
/* @DirtiesContext is necessary because the context needs to be reloaded inbetween the tests | ||
otherwise the responses of previous tests are taken. NoOpCacheManager through @AutoConfigureCache | ||
was not enough to avoid this bug. | ||
*/ | ||
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) | ||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(classes = Sw360ResourceServer.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
public class SW360ConfigActuatorTest { | ||
|
||
@LocalServerPort | ||
private int port; | ||
|
||
@SpyBean | ||
private SW360ConfigActuator restConfigActuatorMock; | ||
|
||
@Autowired | ||
private TestRestTemplate testRestTemplate; | ||
|
||
/** | ||
* Makes a request to localhost with the default server port and returns | ||
* the response as a response entity with type Map | ||
* @param endpoint endpoint that will be called | ||
* @return response of request | ||
*/ | ||
private ResponseEntity<Map> getMapResponseEntityForHealthEndpointRequest(String endpoint) { | ||
return this.testRestTemplate.getForEntity( | ||
"http://localhost:" + this.port + Sw360ResourceServer.REST_BASE_PATH + endpoint, Map.class); | ||
} | ||
|
||
@Test | ||
public void config_should_return_200() { | ||
ResponseEntity<Map> entity = getMapResponseEntityForHealthEndpointRequest("/config"); | ||
|
||
then(entity.getStatusCode()).isEqualTo(HttpStatus.OK); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...e-server/src/test/java/org/eclipse/sw360/rest/resourceserver/restdocs/ConfigSpecTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright Siemens AG, 2024. Part of the SW360 Portal Project. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.sw360.rest.resourceserver.restdocs; | ||
|
||
import org.eclipse.sw360.rest.resourceserver.actuator.SW360ConfigActuator; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.mockito.BDDMockito.given; | ||
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath; | ||
import static org.springframework.restdocs.payload.PayloadDocumentation.responseFields; | ||
import static org.springframework.restdocs.request.RequestDocumentation.parameterWithName; | ||
import static org.springframework.restdocs.request.RequestDocumentation.pathParameters; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
@RunWith(SpringJUnit4ClassRunner.class) | ||
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) | ||
public class ConfigSpecTest extends TestRestDocsSpecBase{ | ||
|
||
@MockBean | ||
private SW360ConfigActuator restConfigActuatorMock; | ||
|
||
Map<String, String> properties; | ||
|
||
{ | ||
properties = new HashMap<>(); | ||
properties.put("admin.private.project.access.enabled", "true"); | ||
properties.put("clearing.teams", "org1,org2,org3"); | ||
properties.put("rest.apitoken.read.validity.days", "90"); | ||
properties.put("rest.write.access.usergroup", "ADMIN"); | ||
} | ||
|
||
@Test | ||
public void should_document_get_config() throws Exception { | ||
given(this.restConfigActuatorMock.config()).willReturn(properties); | ||
|
||
mockMvc.perform(get("/api/config") | ||
.accept(MediaType.APPLICATION_JSON)) | ||
.andExpect(status().isOk()) | ||
.andDo(this.documentationHandler.document( | ||
responseFields( | ||
fieldWithPath("['admin.private.project.access.enabled']").description("Sample boolean property."), | ||
fieldWithPath("['clearing.teams']").description("Sample set property (separated by comma)."), | ||
fieldWithPath("['rest.apitoken.read.validity.days']").description("Sample integer property."), | ||
fieldWithPath("['rest.write.access.usergroup']").description("Sample string property.") | ||
) | ||
)); | ||
} | ||
|
||
@Test | ||
public void should_document_get_single_config() throws Exception { | ||
given(this.restConfigActuatorMock.config("rest.apitoken.read.validity.days")).willReturn(properties.get("rest.apitoken.read.validity.days")); | ||
|
||
mockMvc.perform(RestDocumentationRequestBuilders.get("/api/config/{key}", "rest.apitoken.read.validity.days") | ||
.accept(MediaType.TEXT_PLAIN)) | ||
.andExpect(status().isOk()) | ||
.andExpect(header().string("Content-Type", "text/plain;charset=UTF-8")) | ||
.andDo(this.documentationHandler.document( | ||
pathParameters(parameterWithName("key").description("Property key")) | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters