diff --git a/oscm-rest-api-service/src/main/java/org/oscm/rest/service/TechnicalServiceBackend.java b/oscm-rest-api-service/src/main/java/org/oscm/rest/service/TechnicalServiceBackend.java index adb3a226..38f801b7 100644 --- a/oscm-rest-api-service/src/main/java/org/oscm/rest/service/TechnicalServiceBackend.java +++ b/oscm-rest-api-service/src/main/java/org/oscm/rest/service/TechnicalServiceBackend.java @@ -12,12 +12,19 @@ import java.nio.charset.StandardCharsets; import java.util.Collection; import java.util.List; + import javax.ejb.EJB; import javax.ejb.Stateless; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + import org.oscm.internal.intf.ServiceProvisioningService; import org.oscm.internal.types.enumtypes.OrganizationRoleType; import org.oscm.internal.types.exception.DomainObjectException; import org.oscm.internal.types.exception.ObjectNotFoundException; +import org.oscm.internal.types.exception.OperationNotPermittedException; +import org.oscm.internal.types.exception.OrganizationAuthoritiesException; import org.oscm.internal.vo.VOTechnicalService; import org.oscm.rest.common.PostResponseBody; import org.oscm.rest.common.RestBackend; @@ -76,6 +83,34 @@ public RestBackend.Post post( }; } + @Produces(MediaType.APPLICATION_XML) + public Response getXML(Long id) + throws ObjectNotFoundException, OrganizationAuthoritiesException, + OperationNotPermittedException { + List technicalServices = + sps.getTechnicalServices(OrganizationRoleType.TECHNOLOGY_PROVIDER); + + for (VOTechnicalService technicalService : technicalServices) { + if (id.equals(technicalService.getKey())) { + byte[] b = sps.exportTechnicalServices(technicalServices); + return Response.ok(b, MediaType.APPLICATION_XML).build(); + } + } + throw new ObjectNotFoundException( + DomainObjectException.ClassEnum.TECHNICAL_SERVICE, String.valueOf(id)); + } + + @Produces(MediaType.APPLICATION_XML) + public Response getXMLCollection() + throws OrganizationAuthoritiesException, ObjectNotFoundException, + OperationNotPermittedException { + List technicalServices = + sps.getTechnicalServices(OrganizationRoleType.TECHNOLOGY_PROVIDER); + + byte[] b = sps.exportTechnicalServices(technicalServices); + return Response.ok(b, MediaType.APPLICATION_XML).build(); + } + public RestBackend.Put importFromXml() { return (content, params) -> { byte[] bytes = content.getTechnicalServiceXml().getBytes(StandardCharsets.UTF_8); diff --git a/oscm-rest-api-service/src/main/java/org/oscm/rest/service/TechnicalServiceResource.java b/oscm-rest-api-service/src/main/java/org/oscm/rest/service/TechnicalServiceResource.java index 37c0ade0..ef457bb4 100644 --- a/oscm-rest-api-service/src/main/java/org/oscm/rest/service/TechnicalServiceResource.java +++ b/oscm-rest-api-service/src/main/java/org/oscm/rest/service/TechnicalServiceResource.java @@ -9,6 +9,30 @@ */ package org.oscm.rest.service; +import static javax.ws.rs.core.MediaType.APPLICATION_XML; + +import javax.ejb.EJB; +import javax.ejb.Stateless; +import javax.ws.rs.DELETE; +import javax.ws.rs.DefaultValue; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; + +import org.oscm.rest.common.CommonParams; +import org.oscm.rest.common.RestResource; +import org.oscm.rest.common.Since; +import org.oscm.rest.common.representation.TechnicalServiceImportRepresentation; +import org.oscm.rest.common.representation.TechnicalServiceRepresentation; +import org.oscm.rest.common.requestparameters.ServiceParameters; + import constants.CommonConstants; import constants.DocDescription; import constants.ServiceConstants; @@ -19,20 +43,8 @@ import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.parameters.RequestBody; import io.swagger.v3.oas.annotations.responses.ApiResponse; -import javax.ejb.EJB; -import javax.ejb.Stateless; -import javax.ws.rs.*; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.Response; -import javax.ws.rs.core.UriInfo; import lombok.AccessLevel; import lombok.Setter; -import org.oscm.rest.common.CommonParams; -import org.oscm.rest.common.RestResource; -import org.oscm.rest.common.Since; -import org.oscm.rest.common.representation.TechnicalServiceImportRepresentation; -import org.oscm.rest.common.representation.TechnicalServiceRepresentation; -import org.oscm.rest.common.requestparameters.ServiceParameters; @Path(CommonParams.PATH_VERSION + "/technicalservices") @Stateless @@ -45,7 +57,7 @@ public class TechnicalServiceResource extends RestResource { @GET @Since(CommonParams.VERSION_1) @Path(CommonParams.PATH_ID) - @Produces(CommonParams.JSON) + @Produces({CommonParams.JSON, APPLICATION_XML}) @Operation( summary = "Retrieves technical service", tags = {"technicalservices"}, @@ -64,6 +76,7 @@ public class TechnicalServiceResource extends RestResource { }) public Response getTechnicalService( @Context UriInfo uriInfo, + @Context HttpHeaders headers, @Parameter(description = DocDescription.ENDPOINT_VERSION) @DefaultValue("v1") @PathParam(value = "version") @@ -71,6 +84,10 @@ public Response getTechnicalService( @Parameter(description = DocDescription.TECHNICAL_SERVICE_ID) @PathParam(value = "id") long id) throws Exception { + if (headers.getMediaType() != null + && APPLICATION_XML.equals(headers.getMediaType().toString())) { + return tsb.getXML(id); + } ServiceParameters params = new ServiceParameters(); params.setEndpointVersion(version); params.setId(id); @@ -79,7 +96,7 @@ public Response getTechnicalService( @GET @Since(CommonParams.VERSION_1) - @Produces(CommonParams.JSON) + @Produces({CommonParams.JSON, APPLICATION_XML}) @Operation( summary = "Retrieves all available technical services", tags = {"technicalservices"}, @@ -98,11 +115,16 @@ public Response getTechnicalService( }) public Response getTechnicalServices( @Context UriInfo uriInfo, + @Context HttpHeaders headers, @Parameter(description = DocDescription.ENDPOINT_VERSION) @DefaultValue("v1") @PathParam(value = "version") String version) throws Exception { + if (headers.getMediaType() != null + && APPLICATION_XML.equals(headers.getMediaType().toString())) { + return tsb.getXMLCollection(); + } ServiceParameters params = new ServiceParameters(); params.setEndpointVersion(version); return getCollection(uriInfo, tsb.getCollection(), params); diff --git a/oscm-rest-api-service/src/test/java/org/oscm/rest/service/TechnicalServiceBackendTest.java b/oscm-rest-api-service/src/test/java/org/oscm/rest/service/TechnicalServiceBackendTest.java index 6484ac96..00e9f112 100644 --- a/oscm-rest-api-service/src/test/java/org/oscm/rest/service/TechnicalServiceBackendTest.java +++ b/oscm-rest-api-service/src/test/java/org/oscm/rest/service/TechnicalServiceBackendTest.java @@ -14,15 +14,17 @@ import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.when; -import com.google.common.collect.Lists; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; -import lombok.SneakyThrows; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; import org.oscm.internal.intf.ServiceProvisioningService; import org.oscm.internal.vo.VOTechnicalService; @@ -32,10 +34,15 @@ import org.oscm.rest.common.representation.TechnicalServiceRepresentation; import org.oscm.rest.common.requestparameters.ServiceParameters; +import com.google.common.collect.Lists; + +import lombok.SneakyThrows; + @ExtendWith(MockitoExtension.class) public class TechnicalServiceBackendTest { @Mock private ServiceProvisioningService service; + @Mock HttpHeaders headers; @InjectMocks private TechnicalServiceBackend backend; private TechnicalServiceResource resource; private UriInfo uriInfo; @@ -43,6 +50,27 @@ public class TechnicalServiceBackendTest { private TechnicalServiceRepresentation representation; private TechnicalServiceImportRepresentation importRepresentation; + private String TECHNICAL_SERVICE_XML_EXAMPLE_RESPONSE = + "\n" + + " \n" + + " \n" + + " Description of how to access the application.\n" + + " Sample APP implementation.\n" + + " Sample APP Implementierung.\n" + + " \n" + + " \n" + + " IAAS password\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " Inline CSS for Email Notification\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " "; + @BeforeEach public void setUp() { resource = new TechnicalServiceResource(); @@ -59,7 +87,8 @@ public void shouldGetTechnicalServices() { when(service.getTechnicalServices(any())) .thenReturn(Lists.newArrayList(new VOTechnicalService())); - Response response = resource.getTechnicalServices(uriInfo, parameters.getEndpointVersion()); + Response response = + resource.getTechnicalServices(uriInfo, headers, parameters.getEndpointVersion()); assertThat(response).isNotNull(); assertThat(response) @@ -78,8 +107,55 @@ public void shouldGetTechnicalService() { when(service.getTechnicalServices(any())).thenReturn(Lists.newArrayList(technicalService)); Response response = - resource.getTechnicalService(uriInfo, parameters.getEndpointVersion(), parameters.getId()); + resource.getTechnicalService( + uriInfo, headers, parameters.getEndpointVersion(), parameters.getId()); + + assertThat(response).isNotNull(); + assertThat(response) + .extracting(Response::getStatus) + .isEqualTo(Response.Status.OK.getStatusCode()); + } + + @Test + public void shouldGetTechnicalServiceXML() throws Exception { + // given + VOTechnicalService technicalService = new VOTechnicalService(); + technicalService.setKey(parameters.getId()); + when(service.getTechnicalServices(any())).thenReturn(Lists.newArrayList(technicalService)); + when(service.exportTechnicalServices(any())) + .thenReturn(TECHNICAL_SERVICE_XML_EXAMPLE_RESPONSE.getBytes()); + MediaType type = Mockito.mock(MediaType.class); + when(headers.getMediaType()).thenReturn(type); + when(type.toString()).thenReturn(MediaType.APPLICATION_XML); + + // when + Response response = + resource.getTechnicalService( + uriInfo, headers, parameters.getEndpointVersion(), parameters.getId()); + + // then + assertThat(response).isNotNull(); + assertThat(response) + .extracting(Response::getStatus) + .isEqualTo(Response.Status.OK.getStatusCode()); + } + + @Test + public void shouldGetTechnicalServicesXML() throws Exception { + // given + VOTechnicalService technicalService = new VOTechnicalService(); + when(service.getTechnicalServices(any())).thenReturn(Lists.newArrayList(technicalService)); + when(service.exportTechnicalServices(any())) + .thenReturn(TECHNICAL_SERVICE_XML_EXAMPLE_RESPONSE.getBytes()); + MediaType type = Mockito.mock(MediaType.class); + when(headers.getMediaType()).thenReturn(type); + when(type.toString()).thenReturn(MediaType.APPLICATION_XML); + + // when + Response response = + resource.getTechnicalServices(uriInfo, headers, parameters.getEndpointVersion()); + // then assertThat(response).isNotNull(); assertThat(response) .extracting(Response::getStatus) diff --git a/oscm-rest-api-service/src/test/java/org/oscm/rest/service/TechnicalServiceResourceTest.java b/oscm-rest-api-service/src/test/java/org/oscm/rest/service/TechnicalServiceResourceTest.java index bef341f9..97a9da1c 100644 --- a/oscm-rest-api-service/src/test/java/org/oscm/rest/service/TechnicalServiceResourceTest.java +++ b/oscm-rest-api-service/src/test/java/org/oscm/rest/service/TechnicalServiceResourceTest.java @@ -11,17 +11,21 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.fail; +import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.Mockito.when; -import com.google.common.collect.Lists; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.UriInfo; + import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.Mockito; import org.mockito.Spy; import org.mockito.junit.jupiter.MockitoExtension; import org.oscm.internal.intf.ServiceProvisioningService; @@ -32,15 +36,39 @@ import org.oscm.rest.common.representation.TechnicalServiceRepresentation; import org.oscm.rest.common.requestparameters.ServiceParameters; +import com.google.common.collect.Lists; + @ExtendWith(MockitoExtension.class) public class TechnicalServiceResourceTest { @Mock private TechnicalServiceBackend technicalServiceBackend; + @Mock HttpHeaders headers; @Mock private ServiceProvisioningService serviceProvisioningService; @InjectMocks @Spy TechnicalServiceResource technicalServiceResource; + private String TECHNICAL_SERVICE_XML_EXAMPLE_RESPONSE = + "\n" + + " \n" + + " \n" + + " Description of how to access the application.\n" + + " Sample APP implementation.\n" + + " Sample APP Implementierung.\n" + + " \n" + + " \n" + + " IAAS password\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " Inline CSS for Email Notification\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " "; + private Response response; private TechnicalServiceRepresentation technicalServiceRepresentation; private TechnicalServiceImportRepresentation technicalServiceImportRepresentation; @@ -70,7 +98,7 @@ public void shouldGetTechnicalServices() { try { response = technicalServiceResource.getTechnicalServices( - uriInfo, serviceParameters.getEndpointVersion()); + uriInfo, headers, serviceParameters.getEndpointVersion()); } catch (Exception e) { fail(e); } @@ -90,6 +118,58 @@ public void shouldGetTechnicalServices() { .isEqualTo(technicalServiceRepresentation); } + @Test + public void shouldGetTechnicalServicesXML() throws Exception { + + // given + when(technicalServiceBackend.getXMLCollection()) + .thenReturn( + Response.ok( + TECHNICAL_SERVICE_XML_EXAMPLE_RESPONSE.getBytes(), MediaType.APPLICATION_XML) + .build()); + MediaType type = Mockito.mock(MediaType.class); + when(headers.getMediaType()).thenReturn(type); + when(type.toString()).thenReturn(MediaType.APPLICATION_XML); + + // when + response = + technicalServiceResource.getTechnicalServices( + uriInfo, headers, serviceParameters.getEndpointVersion()); + + // then + assertThat(response).isNotNull(); + assertThat(response) + .extracting(Response::getStatus) + .isEqualTo(Response.Status.OK.getStatusCode()); + assertThat(response).extracting(Response::hasEntity).isEqualTo(true); + } + + @Test + public void shouldGetTechnicalServiceXML() throws Exception { + // given + when(technicalServiceBackend.getXML(anyLong())) + .thenReturn( + Response.ok( + TECHNICAL_SERVICE_XML_EXAMPLE_RESPONSE.getBytes(), MediaType.APPLICATION_XML) + .build()); + + MediaType type = Mockito.mock(MediaType.class); + when(headers.getMediaType()).thenReturn(type); + when(type.toString()).thenReturn(MediaType.APPLICATION_XML); + + // when + response = + technicalServiceResource.getTechnicalService( + uriInfo, headers, serviceParameters.getEndpointVersion(), serviceParameters.getId()); + + // then + assertThat(response).isNotNull(); + assertThat(response) + .extracting(Response::getStatus) + .isEqualTo(Response.Status.OK.getStatusCode()); + assertThat(response).extracting(Response::hasEntity).isEqualTo(true); + } + @Test public void shouldGetTechnicalService() { when(technicalServiceBackend.get()) @@ -98,7 +178,7 @@ public void shouldGetTechnicalService() { try { response = technicalServiceResource.getTechnicalService( - uriInfo, serviceParameters.getEndpointVersion(), serviceParameters.getId()); + uriInfo, headers, serviceParameters.getEndpointVersion(), serviceParameters.getId()); } catch (Exception e) { fail(e); }