Skip to content

Commit

Permalink
Add e2e tests
Browse files Browse the repository at this point in the history
Signed-off-by: Osman Hadzic <[email protected]>
  • Loading branch information
osmanhadzic committed Dec 4, 2023
1 parent 76471b9 commit bdbb2d0
Show file tree
Hide file tree
Showing 21 changed files with 1,529 additions and 1 deletion.
76 changes: 76 additions & 0 deletions .github/workflows/e2e-test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#
# This file is part of Astarte.
#
# Copyright 2023 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

name: End to End test
permissions:
contents: read
on:
push:
# Run on branch/tag creation
create:
# Run on pull requests
pull_request:


concurrency:
group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
cancel-in-progress: true
env:
CARGO_TERM_COLOR: always
jobs:
e2e-test:
runs-on: ubuntu-latest
steps:
- name: Create Astarte Cluster
id: astarte
uses: astarte-platform/astarte-cluster-action@v1
with:
astarte_version: "1.1.1"
- name: Checkout sources
uses: actions/checkout@v3
- name: Install interface
run: |
astartectl realm-management interfaces sync $GITHUB_WORKSPACE/tests/e2etest/GenericTest/src/main/resources/standard-interfaces/*.json --non-interactive
astartectl realm-management interfaces ls
- name: Register device
run: |
echo "E2E_REALM=test" >> $GITHUB_ENV
DEVICE_ID=$(astartectl utils device-id generate-random)
echo "E2E_DEVICE_ID=$DEVICE_ID" >> $GITHUB_ENV
CREDENTIALS_SECRET=$(astartectl pairing agent register --compact-output -- "$DEVICE_ID")
echo "E2E_CREDENTIALS_SECRET=$CREDENTIALS_SECRET" >> $GITHUB_ENV
echo "Java_LOG=debug" >> $GITHUB_ENV
TOKEN=$(astartectl utils gen-jwt appengine)
echo "E2E_TOKEN=$TOKEN" >> $GITHUB_ENV
echo "E2E_API_URL=https://api.autotest.astarte-platform.org" >> $GITHUB_ENV
- name: Set up JDK
uses: actions/setup-java@v1
with:
java-version: 11
- name: Generate SSL certificates
run: |
sudo openssl s_client -connect api.autotest.astarte-platform.org:443 -showcerts </dev/null 2>/dev/null|openssl x509 -outform PEM > ${GITHUB_WORKSPACE}/astarte-certificates.pem
openssl x509 -outform der -in ${GITHUB_WORKSPACE}/astarte-certificates.pem -out ${GITHUB_WORKSPACE}/astarte-certificates.der
echo $JAVA_HOME
KEYSTORE_PATH=$JAVA_HOME/lib/security/cacerts
sudo keytool -import -noprompt -trustcacerts -alias "astarteroot-cert" -file ${GITHUB_WORKSPACE}/astarte-certificates.der -keystore $KEYSTORE_PATH -storepass changeit
sudo keytool -keystore $KEYSTORE_PATH -storepass changeit -list | grep astarteroot-cert
- name: Build with Gradle
run: ./gradlew :tests:e2etest:GenericTest:run
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ protected List<AstarteTransport> reloadTransports(
"Null Pointer exception - probably got a wrong payload?", e);
} catch (Exception e) {
throw new AstartePairingException(
"Failure in calling Pairing API to " + requestUrl.toString(), e);
"Failure in calling Pairing API to " + requestUrl.toString() + " " + e.getMessage());
}

// Iterate Transports and make them available
Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ include ':DeviceSDKAndroid'
include ':DeviceSDKGeneric'
include ':examples:Android'
include ':examples:Generic'
include 'tests:e2etest:GenericTest'
21 changes: 21 additions & 0 deletions tests/e2etest/GenericTest/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
plugins {
id 'application'
}

application {
mainClassName = 'org.astarteplatform.devicesdk.tests.e2etest.GenericDevice'
}

repositories {
mavenCentral()
}

dependencies {
implementation project(path: ':DeviceSDKGeneric')
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1'
implementation 'commons-cli:commons-cli:1.4'
implementation 'com.h2database:h2:1.4.200'
}

tasks.withType(Javadoc).all { enabled = false }
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package org.astarteplatform.devicesdk.tests.e2etest;

import java.io.IOException;
import java.util.*;
import org.astarteplatform.devicesdk.AstarteDevice;
import org.astarteplatform.devicesdk.protocol.*;
import org.astarteplatform.devicesdk.tests.e2etest.utilities.AstarteHttpRequest;
import org.astarteplatform.devicesdk.tests.e2etest.utilities.GenericDeviceMockData;
import org.astarteplatform.devicesdk.tests.e2etest.utilities.GenericDeviceMockDataFactory;
import org.astarteplatform.devicesdk.tests.e2etest.utilities.GenericMockDevice;
import org.astarteplatform.devicesdk.transport.AstarteTransportException;
import org.joda.time.DateTime;

public class GenericAggregateDevice implements AstarteAggregateDatastreamEventListener {
private final GenericDeviceMockData m_deviceMockData;
private final GenericMockDevice m_mockDevice;
private final AstarteDevice m_astarteGenericDevice;

private final AstarteHttpRequest astarteHttpRequest;

public GenericAggregateDevice() throws Exception {
m_deviceMockData = new GenericDeviceMockDataFactory();
m_astarteGenericDevice = m_deviceMockData.getAstarteDeviceSingleton();
m_mockDevice = m_deviceMockData.getMockData();
astarteHttpRequest = new AstarteHttpRequest();
}

public void aggregateFromDeviceToServer()
throws AstarteInvalidValueException, AstarteInterfaceMappingNotFoundException,
AstarteTransportException, IOException, InterruptedException {

System.out.println(
"Test for aggregated object datastreams" + " in the direction from device to server.");
String interfaceName = m_mockDevice.getInterfaceDeviceAggr();

AstarteDeviceAggregateDatastreamInterface astarteDeviceAggregateDatastream =
(AstarteDeviceAggregateDatastreamInterface)
m_astarteGenericDevice.getInterface(interfaceName);

astarteDeviceAggregateDatastream.streamData(
"/seco", m_mockDevice.getMockDataDictionary(), DateTime.now());

Thread.sleep(1000);

List<Object> response =
astarteHttpRequest
.getServerInterface(interfaceName)
.getJSONObject("data")
.getJSONArray("seco")
.toList();

if (response.isEmpty()) {
System.exit(1);
}

Map<String, Object> object = (Map<String, Object>) response.get(0);

object.remove("timestamp");

if (!m_mockDevice.compereHasMas(object)) {
System.exit(1);
}
}

public void aggregateFromServerToDevice() throws IOException {
System.out.println(
"Test for aggregated object datastreams" + " in the direction from server to device.");
String interfaceName = m_mockDevice.getInterfaceServerAggr();

AstarteServerAggregateDatastreamInterface astarteServerAggregateDatastream =
(AstarteServerAggregateDatastreamInterface)
m_astarteGenericDevice.getInterface(interfaceName);

astarteServerAggregateDatastream.addListener(this);

Map<String, Object> data = m_mockDevice.getMockDataDictionary();

astarteHttpRequest.postServerInterface(interfaceName, "/seco", data);
}

@Override
public void valueReceived(AstarteAggregateDatastreamEvent e) {

System.out.println(
"Received aggregate datastream value on interface "
+ e.getInterfaceName()
+ ", values: "
+ e.getValues());

if (!m_mockDevice.compereHasMas(e.getValues())) {
System.exit(1);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
package org.astarteplatform.devicesdk.tests.e2etest;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.astarteplatform.devicesdk.AstarteDevice;
import org.astarteplatform.devicesdk.protocol.*;
import org.astarteplatform.devicesdk.tests.e2etest.utilities.AstarteHttpRequest;
import org.astarteplatform.devicesdk.tests.e2etest.utilities.GenericDeviceMockData;
import org.astarteplatform.devicesdk.tests.e2etest.utilities.GenericDeviceMockDataFactory;
import org.astarteplatform.devicesdk.tests.e2etest.utilities.GenericMockDevice;
import org.astarteplatform.devicesdk.transport.AstarteTransportException;
import org.joda.time.DateTime;
import org.json.JSONObject;

public class GenericDatastreamDevice implements AstarteDatastreamEventListener {

private GenericDeviceMockData m_deviceMockData;
private GenericMockDevice m_mockDevice;
private AstarteDevice m_astarteGenericDevice;
private AstarteHttpRequest m_astarteHttpRequest;

public GenericDatastreamDevice() throws Exception {
m_deviceMockData = new GenericDeviceMockDataFactory();
m_astarteGenericDevice = m_deviceMockData.getAstarteDeviceSingleton();
m_mockDevice = m_deviceMockData.getMockData();
m_astarteHttpRequest = new AstarteHttpRequest();
}

public void datastreamFromDeviceToServer()
throws AstarteInvalidValueException, AstarteInterfaceMappingNotFoundException,
AstarteTransportException, IOException, InterruptedException {

System.out.println("Test for individual datastreams in the direction from device to server.");

String interfaceName = m_mockDevice.getInterfaceDeviceData();

AstarteDeviceDatastreamInterface astarteDatastreamInterface =
(AstarteDeviceDatastreamInterface) m_astarteGenericDevice.getInterface(interfaceName);

for (Map.Entry<String, Object> entry : m_mockDevice.getMockDataDictionary().entrySet()) {
astarteDatastreamInterface.streamData("/" + entry.getKey(), entry.getValue(), DateTime.now());
Thread.sleep(500);
}

JSONObject response = m_astarteHttpRequest.getServerInterface(interfaceName);

if (response.isEmpty()) {
System.exit(1);
}

JSONObject dataMap = response.getJSONObject("data");

Map<String, Object> object = new HashMap<>();
Map<String, Object> map = dataMap.toMap();

for (Map.Entry<String, Object> entry : map.entrySet()) {
HashMap<String, Object> temp = (HashMap<String, Object>) entry.getValue();
String key = entry.getKey();
for (Map.Entry<String, Object> e : temp.entrySet()) {
if (e.getKey().equals("value")) {
object.put(key, e.getValue());
}
}
}
if (!m_mockDevice.compereHasMas(object)) {
System.exit(1);
}
}

public void datastreamFromServerToDevice() throws IOException {

System.out.println("Test for individual datastreams in the direction from server to device.");

String interfaceName = m_mockDevice.getInterfaceServerData();

AstarteServerDatastreamInterface astarteServerDatastreamInterface =
(AstarteServerDatastreamInterface) m_astarteGenericDevice.getInterface(interfaceName);

astarteServerDatastreamInterface.addListener(this);

for (Map.Entry<String, Object> entry : m_mockDevice.getMockDataDictionary().entrySet()) {
m_astarteHttpRequest.postServerInterface(
interfaceName, "/" + entry.getKey(), entry.getValue());
}
}

@Override
public void valueReceived(AstarteDatastreamEvent e) {
for (Map.Entry<String, Object> entry : m_mockDevice.getMockDataDictionary().entrySet()) {
if (entry.getKey().equals(e.getPath().replace("/sensorUuid/", ""))) {
if (e.getValue().getClass().isArray()) {
if (e.getValue() instanceof Integer[]) {
Integer[] dest = new Integer[((Integer[]) e.getValue()).length];
System.arraycopy(e.getValue(), 0, dest, 0, ((Integer[]) e.getValue()).length);
if (!Arrays.equals(dest, (Integer[]) e.getValue())) {
System.exit(1);
}
} else if (e.getValue() instanceof Double[]) {
Double[] dest = new Double[((Double[]) e.getValue()).length];
System.arraycopy(e.getValue(), 0, dest, 0, (((Double[]) e.getValue()).length));
if (!Arrays.equals(dest, (Double[]) e.getValue())) {
System.exit(1);
}
} else if (e.getValue() instanceof Long[]) {
Long[] dest = new Long[((Long[]) e.getValue()).length];
System.arraycopy(e.getValue(), 0, dest, 0, ((Long[]) e.getValue()).length);
if (!Arrays.equals(dest, (Long[]) e.getValue())) {
System.exit(1);
}
} else if (e.getValue() instanceof String[]) {
String[] dest = new String[((String[]) e.getValue()).length];
System.arraycopy(e.getValue(), 0, dest, 0, ((String[]) e.getValue()).length);
if (!Arrays.equals(dest, (String[]) e.getValue())) {
System.exit(1);
}
} else if (e.getValue() instanceof byte[]) {
byte[] dest = new byte[((byte[]) e.getValue()).length];
System.arraycopy(e.getValue(), 0, dest, 0, ((byte[]) e.getValue()).length);
if (!Arrays.equals(dest, (byte[]) e.getValue())) {
System.exit(1);
}
} else if (e.getValue() instanceof DateTime[]) {
DateTime[] dest = new DateTime[((DateTime[]) e.getValue()).length];
System.arraycopy(e.getValue(), 0, dest, 0, ((DateTime[]) e.getValue()).length);
if (!Arrays.equals(dest, (DateTime[]) e.getValue())) {
System.exit(1);
}
} else if (e.getValue() instanceof Boolean[]) {
Boolean[] dest = new Boolean[((Boolean[]) e.getValue()).length];
System.arraycopy(e.getValue(), 0, dest, 0, ((Boolean[]) e.getValue()).length);
if (!Arrays.equals(dest, (Boolean[]) e.getValue())) {
System.exit(1);
}
}
} else if (e.getValue() instanceof DateTime) {
if (!m_mockDevice.compareDates((DateTime) entry.getValue())) {
System.exit(1);
}
} else {
if (!e.getValue().equals(entry.getValue())) {
System.exit(1);
}
}
}
}
}
}
Loading

0 comments on commit bdbb2d0

Please sign in to comment.