Skip to content

Commit

Permalink
:FC-1343: ⚡ Integration Tests (#100)
Browse files Browse the repository at this point in the history
* :FC-1343: ⚡ Integration Tests

* ⚡ removed workflow due to write permissions issues

* ⚡ Added the integration test workflow

* ⚡ added log

* ⚡ update workflow to check env

* ⚡ updated the workflow

* ⚡ testing Environment variables

* ⚡ testing env

* ⚡ adding integration test env

* ⚡ update

* ⚡ added env variables

* ⚡ removed the env variables and ignore the integration test

* ⚡ ignore integration tests in Java CI

* ⚡ update Java CI

* ⚡ custom test ignore module

* ⚡ applied ignore test on Java CI

* ⚡ removed the test log

* ⚡ moving test related files to test folder

Co-authored-by: LivePerson FaaS Core Team Account <[email protected]>
Co-authored-by: chrbrt <[email protected]>
  • Loading branch information
3 people authored Jan 9, 2023
1 parent 57cab44 commit 5856e51
Show file tree
Hide file tree
Showing 8 changed files with 533 additions and 1 deletion.
37 changes: 37 additions & 0 deletions .github/workflows/integration-test.maven.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# This workflow will run Integration Tests for the project with Maven, and cache/restore any dependencies to improve the workflow execution time
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven

name: Integration Tests

on:
pull_request:
branches: [ develop, master ]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up JDK 1.8
uses: actions/setup-java@v3
with:
java-version: 11
distribution: 'adopt'
server-id: ossrh
server-username: OSSRH_USERNAME
server-password: OSSRH_PASSWORD
gpg-private-key: ${{ secrets.GPG_PRIVATE_KEY_1 }}
gpg-passphrase: MAVEN_GPG_PASSPHRASE
- name: Integration testing with Maven
run: |
mvn test -Dtest=FaaSIntegrationClientTest
env:
ACCOUNT_ID: ${{ secrets.ACCOUNT_ID }}
LAMBDAUUID: ${{ secrets.LAMBDAUUID }}
CLIENT_ID: ${{ secrets.CLIENT_ID }}
CLIENT_SECRET: ${{ secrets.CLIENT_SECRET }}
USER: ${{ secrets.USER }}
PASS: ${{ secrets.PASS }}
OSSRH_USERNAME: ${{ secrets.NEXUS_USERNAME }}
OSSRH_PASSWORD: ${{ secrets.NEXUS_PASSWORD }}
MAVEN_GPG_PASSPHRASE: ${{ secrets.GPG_PRIVATE_KEY_PASSPHRASE }}
6 changes: 6 additions & 0 deletions .github/workflows/maven-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ jobs:
OSSRH_USERNAME: ${{ secrets.NEXUS_USERNAME }}
OSSRH_PASSWORD: ${{ secrets.NEXUS_PASSWORD }}
MAVEN_GPG_PASSPHRASE: ${{ secrets.GPG_PRIVATE_KEY_PASSPHRASE }}
ACCOUNT_ID: ${{ secrets.ACCOUNT_ID }}
LAMBDAUUID: ${{ secrets.LAMBDAUUID }}
CLIENT_ID: ${{ secrets.CLIENT_ID }}
CLIENT_SECRET: ${{ secrets.CLIENT_SECRET }}
USER: ${{ secrets.USER }}
PASS: ${{ secrets.PASS }}
2 changes: 1 addition & 1 deletion .github/workflows/test.maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ jobs:
distribution: 'temurin'
cache: maven
- name: Build with Maven
run: mvn --batch-mode --update-snapshots verify
run: mvn --batch-mode --update-snapshots verify -DIntegrationModule.test.excludes="**/*FaaSIntegrationClientTest.java"
16 changes: 16 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,24 @@
<check/>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.9</version>
<configuration>
<excludes>
<exclude>${IntegrationModule.test.excludes}</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<IntegrationModule.skip.tests>false</IntegrationModule.skip.tests>
<skipTests>${IntegrationModule.skip.tests}</skipTests>
<IntegrationModule.test.includes>**/*Test.java</IntegrationModule.test.includes>
<IntegrationModule.test.excludes>**/*FaaSIntegrationClientTest.java</IntegrationModule.test.excludes>
</properties>
<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
package com.liveperson.faas.client;

import com.liveperson.faas.client.types.OptionalParams;
import com.liveperson.faas.csds.CsdsMapClient;
import com.liveperson.faas.dto.FaaSInvocation;
import com.liveperson.faas.exception.*;
import com.liveperson.faas.http.DefaultRestClient;
import com.liveperson.faas.metriccollector.MetricCollector;
import com.liveperson.faas.response.lambda.ErrorLogResponseObject;
import com.liveperson.faas.response.lambda.LambdaResponse;
import com.liveperson.faas.security.AuthSignatureBuilder;
import com.liveperson.faas.util.AuthBearerGenerator;
import com.liveperson.faas.util.BearerConfigResponseObject;
import com.liveperson.faas.util.EventResponse;
import com.liveperson.faas.util.UUIDResponse;

import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public class FaaSIntegrationClientTest {
private static AuthSignature authSignatureBuilder;
private FaaSWebClient client;
private FaaSWebClient clientWithBearer;
private DefaultRestClient restClient = new DefaultRestClient();
private MetricCollector metricCollector;
private DefaultIsImplementedCache defaultIsImplementedCache;
private String accountId = System.getenv("ACCOUNT_ID");
private String clientId = System.getenv("CLIENT_ID");
private String clientSecret = System.getenv("CLIENT_SECRET");
private String userId;
private String externalSystem = "test_system";
private FaaSEvent event = FaaSEvent.MessagingNewConversation;
private String lambdaUUID = System.getenv("LAMBDAUUID");
private String requestId = "requestId";

private int defaultTimeOut = 15000;

private OptionalParams optionalParams;

@Before
public void before() throws Exception, TokenGenerationException {
client = getFaaSClient();
authSignatureBuilder = new AuthSignature();
clientWithBearer = getFaaSClientBearer();
optionalParams = new OptionalParams();
optionalParams.setTimeOutInMs(defaultTimeOut);
optionalParams.setRequestId(requestId);
userId = authSignatureBuilder.getUserId();
}

@Test
public void getLambdas() throws Exception {
List<LambdaResponse> lambdaResponse = clientWithBearer.getLambdas(userId, new HashMap<String, String>(),
optionalParams);
assertTrue(lambdaResponse.size() > 0);
assertTrue(lambdaResponse.toString().contains("name"));
assertTrue(lambdaResponse.toString().contains("uuid"));
assertTrue(lambdaResponse.toString().contains("createdAt"));
assertTrue(lambdaResponse.toString().contains("updatedAt"));
assertTrue(lambdaResponse.toString().contains("updatedBy"));
}

@Test
public void getLambdasWithOptionalQueryParameters() throws Exception {
HashMap<String, String> filterMap = new HashMap<String, String>();
filterMap.put("eventId", "not-existing");
List<LambdaResponse> lambdaResponse = clientWithBearer.getLambdas(userId, filterMap, optionalParams);
assertTrue(lambdaResponse.size() == 0);
}

@Test(expected = FaaSException.class)
public void getLambdasWithInvalidStateValueQueryParameter() throws IOException, FaaSException {
try {
HashMap<String, String> filterMap = new HashMap<String, String>();
filterMap.put("state", "active");
clientWithBearer.getLambdas(userId, filterMap, optionalParams);

} catch (FaaSException e) {
assertTrue(e.getCause().toString().contains("Received response code 400"));
throw e;
}
}

@Test
public void isImplementedEventRetrievedFromCache() throws Exception {
boolean isImplemented = client.isImplemented(externalSystem, event, optionalParams);
assertTrue("Lambda should be implemented", isImplemented);
}

@Test
public void isImplementedEventRetrievedFromCacheOnNotExistingEvent() throws Exception {
boolean isImplemented = client.isImplemented(externalSystem, FaaSEvent.ChatPostSurveyEmailTranscript,
optionalParams);
assertFalse("Lambda should not be implemented", isImplemented);
}

@Test
public void invokeViaEventType() throws Exception {
UUIDResponse eventPayload = new UUIDResponse();
long timestamp = System.currentTimeMillis();
Map<String, String> headers = getTestHeaders();
FaaSInvocation<UUIDResponse> invocationData = getUUIDResponseFaaSInvocation(eventPayload, timestamp, headers);
EventResponse[] response = client.invokeByEvent(externalSystem, event, invocationData, EventResponse[].class,
optionalParams);
assertEquals("Success", response[0].result.value);
assertNotNull(response[0].uuid, "The uuid should not be null");
}

@Test
public void invokeViaEventTypeWithValidPayload() throws Exception {
UUIDResponse eventPayload = new UUIDResponse();
eventPayload.value = "validLogs";
long timestamp = System.currentTimeMillis();
Map<String, String> headers = getTestHeaders();
FaaSInvocation<UUIDResponse> invocationData = getUUIDResponseFaaSInvocation(eventPayload, timestamp, headers);
EventResponse[] response = client.invokeByEvent(externalSystem, event, invocationData, EventResponse[].class,
optionalParams);
assertEquals("With Payload", response[0].result.value);
}

@Test
public void invokeViaEventTypeWithNonExistingEvent() throws Exception {
UUIDResponse eventPayload = new UUIDResponse();
long timestamp = System.currentTimeMillis();
Map<String, String> headers = getTestHeaders();
FaaSInvocation<UUIDResponse> invocationData = getUUIDResponseFaaSInvocation(eventPayload, timestamp, headers);
EventResponse[] response = client.invokeByEvent(externalSystem, FaaSEvent.ChatPostSurveyEmailTranscript,
invocationData, EventResponse[].class, optionalParams);
assertTrue(response.length == 0);
}

@Test
public void invokeViaUUIDWithRequestId() throws Exception {
String payload = "request_data";
long timestamp = System.currentTimeMillis();
FaaSInvocation<String> invocationData = new FaaSInvocation<String>(null, payload);
invocationData.setTimestamp(timestamp);
optionalParams.setRequestId(requestId);

String response = client.invokeByUUID(externalSystem, lambdaUUID, invocationData, String.class, optionalParams);
assertEquals("Success", response);
}

@Test
public void invokeViaUUIDWithoutPayload() throws Exception {
long timestamp = System.currentTimeMillis();
FaaSInvocation<Object> invocationData = new FaaSInvocation<Object>(null, null);
invocationData.setTimestamp(timestamp);
String response = client.invokeByUUID(externalSystem, lambdaUUID, invocationData, String.class, optionalParams);
assertEquals("Success", response);
}

@Test
public void invokeViaUUIDWithValidPayload() throws Exception {
String payload = "validLogs";
long timestamp = System.currentTimeMillis();
FaaSInvocation<Object> invocationData = new FaaSInvocation<Object>(null, payload);
invocationData.setTimestamp(timestamp);
String response = client.invokeByUUID(externalSystem, lambdaUUID, invocationData, String.class, optionalParams);
assertEquals("validLogs", response);
}

@Test(expected = FaaSException.class)
public void invokeViaUUIDWithTimeoutPayload() throws IOException, FaaSException {

String payload = "timeout";
long timestamp = System.currentTimeMillis();
FaaSInvocation<String> invocationData = new FaaSInvocation<String>(null, payload);
invocationData.setTimestamp(timestamp);
optionalParams.setRequestId(requestId);
try {
client.invokeByUUID(externalSystem, lambdaUUID, invocationData, ErrorLogResponseObject.class,
optionalParams);
} catch (FaaSException e) {
assertEquals("Error occured during lambda invocation", e.getMessage());
assertEquals("Read timed out", e.getCause().getMessage());
throw e;
}
}

@Test(expected = FaaSDetailedException.class)
public void invokeViaUUIDThrowsFaasDetailedException() throws FaaSException, FaaSDetailedException {

String payload = "error";
long timestamp = System.currentTimeMillis();
FaaSInvocation<Object> invocationData = new FaaSInvocation<Object>(null, payload);
invocationData.setTimestamp(timestamp);
try {
client.invokeByUUID(externalSystem, lambdaUUID, invocationData, optionalParams);
} catch (FaaSDetailedException e) {
assertEquals(FaaSLambdaErrorCodes.RUNTIME_EXCEPTION.getCode(), e.getFaaSError().getErrorCode());
assertEquals(901, e.getCause().getStatusCode());
throw e;
}
}

private FaaSWebClient getFaaSClient() {
return new FaaSWebClient.Builder(accountId).withClientId(clientId)
.withClientSecret(clientSecret)
.withRestClient(restClient)
.withMetricCollector(metricCollector)
.withIsImplementedCache(defaultIsImplementedCache)
.build();
}

private FaaSWebClient getFaaSClientBearer() {
return new FaaSWebClient.Builder(accountId).withAuthSignatureBuilder(authSignatureBuilder)
.withRestClient(restClient)
.withMetricCollector(metricCollector)
.withIsImplementedCache(defaultIsImplementedCache)
.build();
}

private Map<String, String> getTestHeaders() {
Map<String, String> headers = new HashMap();
headers.put("testHeader", "testHeaderValue");
return headers;
}

private FaaSInvocation<UUIDResponse> getUUIDResponseFaaSInvocation(UUIDResponse payload, long timestamp,
Map<String, String> headers) {
FaaSInvocation<UUIDResponse> invocationData = new FaaSInvocation();
invocationData.setHeaders(headers);
invocationData.setPayload(payload);
invocationData.setTimestamp(timestamp);
return invocationData;
}

}

class AuthSignature implements AuthSignatureBuilder {

private DefaultRestClient restClient = new DefaultRestClient();
private CsdsMapClient csdsClient = new CsdsMapClient(getAlphaDomains());
private String accountId = System.getenv("ACCOUNT_ID");
private String username = System.getenv("USER");
private String password = System.getenv("PASS");
private AuthBearerGenerator bearerGenerator;
private BearerConfigResponseObject configData;
private String authHeader;

public AuthSignature() throws TokenGenerationException {
this.bearerGenerator = new AuthBearerGenerator(restClient, csdsClient, accountId, username, password);
this.authHeader = bearerGenerator.retrieveBearerToken();
this.configData = bearerGenerator.retrieveBearerConfig();
}

@Override
public String getAuthHeader() {
return authHeader;
}

public String getUserId() {
return configData.getUserId();
}

private Map<String, String> getAlphaDomains() {
Map<String, String> domains = new HashMap();
domains.put("agentVep", "va-a.agentvep.liveperson.net");
return domains;
}
}
Loading

0 comments on commit 5856e51

Please sign in to comment.