From 9b3913cbdddf133c02c408003fc7ba77da77df12 Mon Sep 17 00:00:00 2001 From: apeiris Date: Wed, 10 Jul 2024 11:42:27 -0400 Subject: [PATCH] Add API endpoint to retrieve configuration properties test case ApiResourceTest --- .../cmd/processor/restapi/ApiResource.java | 37 +++++++++--- .../java/org/openas2/ApiResourceTest.java | 58 +++++++++++++++++++ 2 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 Server/src/test/java/org/openas2/ApiResourceTest.java diff --git a/Server/src/main/java/org/openas2/cmd/processor/restapi/ApiResource.java b/Server/src/main/java/org/openas2/cmd/processor/restapi/ApiResource.java index b41f3ae5..a76ba000 100644 --- a/Server/src/main/java/org/openas2/cmd/processor/restapi/ApiResource.java +++ b/Server/src/main/java/org/openas2/cmd/processor/restapi/ApiResource.java @@ -7,9 +7,13 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; +import com.fasterxml.jackson.core.JsonProcessingException; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.openas2.cert.AliasedCertificateFactory; import org.openas2.cmd.CommandResult; import org.openas2.cmd.processor.RestCommandProcessor; +import org.openas2.util.Properties; import javax.annotation.security.RolesAllowed; import jakarta.ws.rs.Consumes; @@ -38,12 +42,7 @@ import java.io.ByteArrayInputStream; import java.security.cert.Certificate; import java.security.cert.X509Certificate; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Base64; -import java.util.HashMap; -import java.util.Iterator; -import java.util.List; +import java.util.*; import java.util.logging.Level; import java.util.logging.Logger; @@ -53,6 +52,7 @@ @Path("api") public class ApiResource { + private final Log logger = LogFactory.getLog(ApiResource.class.getSimpleName()); /** * @return the processor */ @@ -73,9 +73,9 @@ public static void setProcessor(RestCommandProcessor aProcessor) { @Context Request request; private final ObjectMapper mapper; - + public ApiResource() { - + mapper = new ObjectMapper(); // enable pretty printing mapper.enable(SerializationFeature.INDENT_OUTPUT); @@ -249,4 +249,25 @@ private CommandResult importCertificateByStream(String itemId, MultivaluedMap result = new HashMap<>(); + result = (Map) Properties.getProperties(); + + ObjectMapper om = new ObjectMapper(); + try { + String js = om.writeValueAsString(result); + return Response.ok(js, MediaType.APPLICATION_JSON).build(); + } catch (JsonProcessingException e) { + logger.error(e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR) + .entity("error") + .type(MediaType.APPLICATION_JSON) + .build(); + } + } + } diff --git a/Server/src/test/java/org/openas2/ApiResourceTest.java b/Server/src/test/java/org/openas2/ApiResourceTest.java new file mode 100644 index 00000000..b9747960 --- /dev/null +++ b/Server/src/test/java/org/openas2/ApiResourceTest.java @@ -0,0 +1,58 @@ +package org.openas2; + +import com.fasterxml.jackson.databind.ObjectMapper; +import jakarta.ws.rs.core.Response; +import jakarta.ws.rs.client.Client; +import jakarta.ws.rs.client.ClientBuilder; +import jakarta.ws.rs.client.WebTarget; +import jakarta.ws.rs.core.HttpHeaders; +import jakarta.ws.rs.core.MediaType; +import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.MockedStatic; +import org.openas2.cmd.processor.restapi.ApiResource; +import org.openas2.logging.Logger; +import org.openas2.util.Properties; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.*; + +public class ApiResourceTest { + + @Mock + private Logger logger; + + @Mock + private ObjectMapper objectMapper; + + @InjectMocks + private ApiResource apiResource; + + private Client client; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + + // Set up the client with basic authentication + HttpAuthenticationFeature authFeature = HttpAuthenticationFeature.basicBuilder() + .credentials("userID", "pWd") + .build(); + + client = ClientBuilder.newClient().register(authFeature); + } + + @Test + public void testGetProperties() throws Exception { + Response response = apiResource.getPropertyList(); + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + + } +}