Skip to content
This repository has been archived by the owner on Aug 1, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1486 from SAP/destination-wrapper
Browse files Browse the repository at this point in the history
Add properties for destinations in XSJS API
  • Loading branch information
ThuF authored Apr 8, 2022
2 parents 95eb135 + 16868b9 commit 1849eac
Show file tree
Hide file tree
Showing 8 changed files with 339 additions and 16 deletions.
18 changes: 18 additions & 0 deletions modules/api/api-xsjs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,24 @@
<artifactId>scp-cf</artifactId>
<version>${scp-cf.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
package com.sap.xsk.api.destination;

import com.sap.cloud.sdk.cloudplatform.CloudPlatformAccessor;
import com.sap.cloud.sdk.cloudplatform.connectivity.Destination;
import com.sap.cloud.sdk.cloudplatform.connectivity.DestinationAccessor;
import com.sap.cloud.sdk.cloudplatform.connectivity.HttpClientAccessor;
import com.sap.cloud.sdk.cloudplatform.connectivity.HttpDestination;
import io.vavr.control.Option;
import org.apache.http.HttpResponse;
import org.apache.http.MethodNotSupportedException;
import org.apache.http.client.HttpClient;
Expand All @@ -28,21 +29,37 @@
import java.io.IOException;
import java.net.URI;
import java.util.Arrays;
import java.util.Properties;

public class CloudPlatformDestinationFacade implements IScriptingFacade {
public static Destination getDestination(String name) {

public static Destination getDestination(String destinationName) {
setKymaCloudPlatformFacade();

return DestinationAccessor.getDestination(name);
Properties destinationProperties = new Properties();
com.sap.cloud.sdk.cloudplatform.connectivity.Destination fetchedDestination = DestinationAccessor.getDestination(destinationName);
fetchedDestination.getPropertyNames()
.forEach(propName -> {
Option<Object> property = fetchedDestination.get(propName);
if (!property.isEmpty()) {
destinationProperties.put(propName, property.get());
}
});

URI uri = URI.create((String) fetchedDestination.get("URL").get());

return new Destination(uri.getHost(), uri.getPort(), uri.getPath(), destinationProperties);
}

public static String executeRequest(String requestObject, Destination destination, String options) throws IOException, MethodNotSupportedException {
public static String executeRequest(String requestObject, String destinationName, String options)
throws IOException, MethodNotSupportedException {
setKymaCloudPlatformFacade();

DestinationRequest destinationRequest = GsonHelper.GSON.fromJson(requestObject, DestinationRequest.class);
HttpClientRequestOptions parsedOptions = HttpClientFacade.parseOptions(options);
HttpClient client = HttpClientAccessor.getHttpClient(destination.asHttp());
String uri = URI.create(destination.asHttp().getUri() + destinationRequest.getQueryPath()).toString();
HttpDestination httpDestination = DestinationAccessor.getDestination(destinationName).asHttp();
HttpClient client = HttpClientAccessor.getHttpClient(httpDestination);
String uri = URI.create(httpDestination.getUri() + destinationRequest.getQueryPath()).toString();
HttpRequestBase request = getRequest(uri, destinationRequest, parsedOptions);
setRequestHeaders(destinationRequest, request);

Expand All @@ -59,14 +76,15 @@ public static String executeRequest(String requestObject, Destination destinatio
}

private static void setKymaCloudPlatformFacade() {
if(CloudPlatformAccessor.getCloudPlatformFacade() instanceof CloudPlatformKymaFacade) {
if (CloudPlatformAccessor.getCloudPlatformFacade() instanceof CloudPlatformKymaFacade) {
return;
}

CloudPlatformAccessor.setCloudPlatformFacade(new CloudPlatformKymaFacade());
}

private static HttpRequestBase getRequest(String uri, DestinationRequest destinationRequest, HttpClientRequestOptions options) throws MethodNotSupportedException, IOException {
private static HttpRequestBase getRequest(String uri, DestinationRequest destinationRequest, HttpClientRequestOptions options)
throws MethodNotSupportedException, IOException {
switch (destinationRequest.getMethod()) {
case 0:
return new HttpOptions();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2022 SAP SE or an SAP affiliate company and XSK contributors
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Apache License, v2.0
* which accompanies this distribution, and is available at
* http://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and XSK contributors
* SPDX-License-Identifier: Apache-2.0
*/
package com.sap.xsk.api.destination;

import org.eclipse.dirigible.commons.api.helpers.GsonHelper;
import java.util.Properties;

public class Destination {

private String host;
private int port;
private String pathPrefix;
private Properties properties = new Properties();

public Destination(String host, int port, String pathPrefix, Properties properties) {
this.host = host;
this.port = port;
this.pathPrefix = pathPrefix;
this.properties = properties;
}

public String getHost() {
return host;
}

public int getPort() {
return port;
}

public String getPathPrefix() { return pathPrefix; }

public Properties getProperties() {
return properties;
}

public String getPropertiesAsJSON() {
return GsonHelper.GSON.toJson(getProperties());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,17 @@ var SET_COOKIE_HEADER = "Set-Cookie";
var CONTENT_LENGTH_HEADER = "Content-Length";

exports.readDestination = function (destinationPackage, destinationName) {
var destination = com.sap.xsk.api.destination.CloudPlatformDestinationFacade.getDestination(destinationName);
let readDestination = com.sap.xsk.api.destination.CloudPlatformDestinationFacade.getDestination(destinationName);
let destination = new exports.Destination();

return destination;
destination.host = readDestination.getHost();
destination.port = readDestination.getPort();
destination.pathPrefix = readDestination.getPathPrefix();
destination.name = destinationName;

let properties = JSON.parse(readDestination.getPropertiesAsJSON());

return Object.assign(destination, properties);
};

exports.Client = function () {
Expand Down Expand Up @@ -125,14 +133,14 @@ exports.Client = function () {
let options = {};

if (requestObj.body) {
if (typeof requestObj.body === 'string' || requestObj.bodyy instanceof String) {
if (typeof requestObj.body === 'string' || requestObj.body instanceof String) {
options.text = requestObj.body;
} else {
options.text = JSON.stringify(requestObj.body);
}
}

clientResponse = com.sap.xsk.api.destination.CloudPlatformDestinationFacade.executeRequest(JSON.stringify(requestObj), destination, JSON.stringify(options));
clientResponse = com.sap.xsk.api.destination.CloudPlatformDestinationFacade.executeRequest(JSON.stringify(requestObj), destination.name, JSON.stringify(options));
}

function sendRequestObjToUrl(requestObj, url, proxy) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
* Copyright (c) 2022 SAP SE or an SAP affiliate company and XSK contributors
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Apache License, v2.0
* which accompanies this distribution, and is available at
* http://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-FileCopyrightText: 2022 SAP SE or an SAP affiliate company and XSK contributors
* SPDX-License-Identifier: Apache-2.0
*/
package com.sap.xsk.api.test;

import com.sap.cloud.sdk.cloudplatform.CloudPlatformAccessor;
import com.sap.cloud.sdk.cloudplatform.connectivity.AuthenticationType;
import com.sap.cloud.sdk.cloudplatform.connectivity.DestinationAccessor;
import com.sap.cloud.sdk.cloudplatform.connectivity.HttpClientAccessor;
import com.sap.cloud.sdk.cloudplatform.connectivity.HttpDestination;
import com.sap.xsk.api.destination.CloudPlatformDestinationFacade;
import com.sap.xsk.api.destination.CloudPlatformKymaFacade;
import io.vavr.control.Option;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import static org.mockito.ArgumentMatchers.any;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import com.sap.xsk.api.destination.Destination;
import java.net.URI;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;

import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;

@RunWith(Parameterized.class)
public class CloudPlatformDestinationFacadeTest {

private HttpClient httpClient;
private String destinationName = "test-destination";
private String DESTINATION_URI = "http://test-destination.com:8080/destination";
private MockedStatic<DestinationAccessor> destinationAccessor;
private MockedStatic<HttpClientAccessor> httpClientAccessor;
private MockedStatic<CloudPlatformAccessor> cloudPlatformAccessor;
private boolean isKymaFacadeSet = false;

@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{ true }, { false }
});
}

public CloudPlatformDestinationFacadeTest(boolean isKymaFacadeSet) {
this.isKymaFacadeSet = isKymaFacadeSet;
}

@Before
public void setup() throws IOException {
mockRequest();
mockAccessors(isKymaFacadeSet);
}

@After
public void close() {

if (!isKymaFacadeSet) {
cloudPlatformAccessor.verify(() -> CloudPlatformAccessor.setCloudPlatformFacade(any()));
}
destinationAccessor.close();
httpClientAccessor.close();
cloudPlatformAccessor.close();

}


public void mockAccessors(Boolean isKymaFacadeSet) {
destinationAccessor = Mockito.mockStatic(DestinationAccessor.class);
httpClientAccessor = Mockito.mockStatic(HttpClientAccessor.class);
cloudPlatformAccessor = Mockito.mockStatic(CloudPlatformAccessor.class);

com.sap.cloud.sdk.cloudplatform.connectivity.Destination mockedDestination = Mockito.mock(com.sap.cloud.sdk.cloudplatform.connectivity.Destination.class);
when(mockedDestination.get("URL")).thenReturn(Option.of(DESTINATION_URI));

HttpDestination mockedHttpDestination = Mockito.mock(HttpDestination.class);
when(mockedHttpDestination.getAuthenticationType()).thenReturn(AuthenticationType.NO_AUTHENTICATION);
when(mockedHttpDestination.getUri()).thenReturn(URI.create(DESTINATION_URI));

when(mockedDestination.asHttp()).thenReturn(mockedHttpDestination);

destinationAccessor.when(() -> DestinationAccessor.getDestination(destinationName))
.thenReturn(mockedDestination);

httpClientAccessor.when(() -> HttpClientAccessor.getHttpClient(any()))
.thenReturn(httpClient);

cloudPlatformAccessor.when(() -> CloudPlatformAccessor.getCloudPlatformFacade())
.thenReturn(isKymaFacadeSet ? new CloudPlatformKymaFacade() : null);
}


public void mockRequest() throws IOException {
httpClient = Mockito.mock(HttpClient.class);
HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
StatusLine statusLine = Mockito.mock(StatusLine.class);

when(statusLine.getStatusCode()).thenReturn(200);
when(httpResponse.getStatusLine()).thenReturn(statusLine);
when(httpResponse.getAllHeaders()).thenReturn(new Header[0]);
when(httpClient.execute(any())).thenReturn(httpResponse);

HttpEntity mockedEntity = Mockito.mock(HttpEntity.class);
when(mockedEntity.getContent()).thenReturn(new ByteArrayInputStream("success".getBytes()));

when(httpResponse.getEntity()).thenReturn(mockedEntity);
}

@Test
public void getDestinationTest() throws Exception {
Destination dest = CloudPlatformDestinationFacade.getDestination(destinationName);
assertEquals("test-destination.com", dest.getHost());
assertEquals(8080, dest.getPort());
assertEquals("/destination", dest.getPathPrefix());
}

@Test
public void executeDestinationRequestTest() throws Exception {
String request = "{\"method\": 1, \"queryPath\": \"test-destination.com\", \"headers\": []}";
String options = "{}";

String response = CloudPlatformDestinationFacade.executeRequest(request, destinationName, options);
assertEquals("{\"headers\":[],\"statusCode\":200,\"text\":\"success\"}", response);
}

}
18 changes: 18 additions & 0 deletions modules/engines/engine-mail-destination/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@
<artifactId>scp-cf</artifactId>
<version>${scp-cf.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.sap.cloud.sdk.cloudplatform.connectivity.exception.DestinationAccessException;
import com.sap.xsk.api.destination.CloudPlatformDestinationFacade;
import org.eclipse.dirigible.api.v3.mail.api.IMailConfigurationProvider;
import com.sap.cloud.sdk.cloudplatform.connectivity.Destination;
import org.eclipse.dirigible.commons.config.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -49,10 +48,10 @@ public Properties getProperties() {
Properties properties = new Properties();
try {
String destinationName = Configuration.get(DESTINATION_NAME);
Destination destination = CloudPlatformDestinationFacade.getDestination(destinationName);
Properties destinationProperties = CloudPlatformDestinationFacade.getDestination(destinationName).getProperties();
for (String key : MAIL_PROPERTIES) {
if(!destination.get(key).isEmpty()) {
properties.put(key, destination.get(key).get());
if(destinationProperties.containsKey(key)) {
properties.put(key, destinationProperties.get(key));
}
}
} catch (DestinationAccessException e) {
Expand Down
Loading

0 comments on commit 1849eac

Please sign in to comment.