Skip to content
This repository has been archived by the owner on Jan 12, 2023. It is now read-only.

Commit

Permalink
add get xml endpoints for technical service (#249)
Browse files Browse the repository at this point in the history
* add get xml endpoints for technical service

* refactor

* implement feedback
  • Loading branch information
cworf91 authored Nov 3, 2020
1 parent 618cd99 commit 309ec82
Show file tree
Hide file tree
Showing 4 changed files with 234 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -76,6 +83,34 @@ public RestBackend.Post<TechnicalServiceRepresentation, ServiceParameters> post(
};
}

@Produces(MediaType.APPLICATION_XML)
public Response getXML(Long id)
throws ObjectNotFoundException, OrganizationAuthoritiesException,
OperationNotPermittedException {
List<VOTechnicalService> 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<VOTechnicalService> technicalServices =
sps.getTechnicalServices(OrganizationRoleType.TECHNOLOGY_PROVIDER);

byte[] b = sps.exportTechnicalServices(technicalServices);
return Response.ok(b, MediaType.APPLICATION_XML).build();
}

public RestBackend.Put<TechnicalServiceImportRepresentation, ServiceParameters> importFromXml() {
return (content, params) -> {
byte[] bytes = content.getTechnicalServiceXml().getBytes(StandardCharsets.UTF_8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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"},
Expand All @@ -64,13 +76,18 @@ public class TechnicalServiceResource extends RestResource {
})
public Response getTechnicalService(
@Context UriInfo uriInfo,
@Context HttpHeaders headers,
@Parameter(description = DocDescription.ENDPOINT_VERSION)
@DefaultValue("v1")
@PathParam(value = "version")
String version,
@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);
Expand All @@ -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"},
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,17 +34,43 @@
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;
private ServiceParameters parameters;
private TechnicalServiceRepresentation representation;
private TechnicalServiceImportRepresentation importRepresentation;

private String TECHNICAL_SERVICE_XML_EXAMPLE_RESPONSE =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
+ " <tns:TechnicalServices xmlns:tns=\"oscm.serviceprovisioning/1.9/TechnicalService.xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"oscm.serviceprovisioning/1.9/TechnicalService.xsd ../../oscm-serviceprovisioning/javares/TechnicalServices.xsd\">\n"
+ " <tns:TechnicalService accessType=\"DIRECT\" allowingOnBehalfActing=\"false\" baseUrl=\"\" billingIdentifier=\"NATIVE_BILLING\" build=\"2019.12.13\" id=\"AppSampleService\" loginPath=\"/Dynamically provided.\" onlyOneSubscriptionPerUser=\"false\" provisioningPassword=\"\" provisioningType=\"ASYNCHRONOUS\" provisioningUrl=\"http://oscm-app:8880/oscm-app/webservices/oscm-app/oscm-app/org.oscm.app.v2_0.service.AsynchronousProvisioningProxy?wsdl\" provisioningUsername=\"\" provisioningVersion=\"1.0\">\n"
+ " <AccessInfo locale=\"en\">Description of how to access the application.</AccessInfo>\n"
+ " <LocalizedDescription locale=\"en\">Sample APP implementation.</LocalizedDescription>\n"
+ " <LocalizedDescription locale=\"de\">Sample APP Implementierung.</LocalizedDescription>\n"
+ " <LocalizedLicense locale=\"en\"/>\n"
+ " <ParameterDefinition configurable=\"true\" id=\"PARAM_PWD\" mandatory=\"true\" valueType=\"PWD\">\n"
+ " <LocalizedDescription locale=\"en\">IAAS password</LocalizedDescription>\n"
+ " <LocalizedDescription locale=\"de\"/>\n"
+ " <LocalizedDescription locale=\"ja\"/>\n"
+ " </ParameterDefinition>\n"
+ " <ParameterDefinition configurable=\"true\" id=\"CSSSTYLE\" mandatory=\"false\" valueType=\"STRING\">\n"
+ " <LocalizedDescription locale=\"en\">Inline CSS for Email Notification</LocalizedDescription>\n"
+ " <LocalizedDescription locale=\"de\"/>\n"
+ " <LocalizedDescription locale=\"ja\"/>\n"
+ " </ParameterDefinition>\n"
+ " </tns:TechnicalService>\n"
+ " </tns:TechnicalServices>";

@BeforeEach
public void setUp() {
resource = new TechnicalServiceResource();
Expand All @@ -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)
Expand All @@ -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)
Expand Down
Loading

0 comments on commit 309ec82

Please sign in to comment.