Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/at_client.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ jobs:
distribution: 'adopt'
cache: maven
server-id: ossrh # Value of the distributionManagement/repository/id field of the pom.xml
- name: add entry to hosts file
run: echo "127.0.0.1 vip.ve.atsign.zone" | sudo tee -a /etc/hosts
- name: Build clean and run unit tests
run: mvn --batch-mode clean test -DskipIntegrationTests=true -Dgpg.skip=true
- name: Run integration tests
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea/
76 changes: 76 additions & 0 deletions at_client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
<skipUnitTests>${skipTests}</skipUnitTests>
<skipIntegrationTests>${skipTests}</skipIntegrationTests>
<gpg.skip>true</gpg.skip>

<version.cucumber>7.14.0</version.cucumber>
<version.at_demo_data>1.2.0</version.at_demo_data>
</properties>

<licenses>
Expand Down Expand Up @@ -79,6 +82,29 @@
</execution>
</executions>
</plugin>


<plugin>
<groupId>com.googlecode.maven-download-plugin</groupId>
<artifactId>download-maven-plugin</artifactId>
<version>1.6.8</version>
<executions>
<execution>
<id>download-at_demo_data</id>
<phase>generate-test-resources</phase>
<goals>
<goal>wget</goal>
</goals>
<configuration>
<url>https://pub.dev/packages/at_demo_data/versions/${version.at_demo_data}.tar.gz</url>
<outputDirectory>${project.build.directory}/at_demo_data</outputDirectory>
<outputName>at_demo_data.tar.gz</outputName>
<unpack>true</unpack>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
Expand Down Expand Up @@ -266,6 +292,56 @@
<artifactId>jansi</artifactId>
<version>2.4.0</version>
</dependency>

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>${version.cucumber}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${version.cucumber}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-picocontainer</artifactId>
<version>${version.cucumber}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.21.3</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.7</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>2.0.7</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>

</dependencies>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public Response executeCommand(String command, boolean throwExceptionOnErrorResp

@Override public void startMonitor() {ensureMonitorRunning();}
@Override public void stopMonitor() {ensureMonitorNotRunning();}
@Override public boolean isMonitorRunning() {return monitorConnection.isRunning();}
@Override public boolean isMonitorRunning() {return monitorConnection != null && monitorConnection.isRunning();}

@Override
public synchronized void handleEvent(AtEventType eventType, Map<String, Object> eventData) {
Expand Down
32 changes: 28 additions & 4 deletions at_client/src/main/java/org/atsign/client/util/KeysUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,25 @@
import java.util.TreeMap;

public class KeysUtil {

private static final ObjectMapper mapper = new ObjectMapper();

public static final String expectedKeysFilesLocation = System.getProperty("user.home") + "/.atsign/keys/";
public static final String ATSIGN_KEYS_DIR = "ATSIGN_KEYS_DIR";
public static final String ATSIGN_KEYS_SUFFIX = "ATSIGN_KEYS_SUFFIX";

public static String expectedKeysFilesLocation = getFirstNonEmpty(
System.getProperty(ATSIGN_KEYS_DIR),
System.getenv(ATSIGN_KEYS_DIR),
System.getProperty("user.home") + "/.atsign/keys/"
);

public static final String legacyKeysFilesLocation = System.getProperty("user.dir") + "/keys/";
public static final String keysFileSuffix = "_key.atKeys";

public static String keysFileSuffix = getFirstNonEmpty(
System.getProperty(ATSIGN_KEYS_SUFFIX),
System.getenv(ATSIGN_KEYS_SUFFIX),
"_key.atKeys"
);

public static final String pkamPublicKeyName = "aesPkamPublicKey";
public static final String pkamPrivateKeyName = "aesPkamPrivateKey";
Expand Down Expand Up @@ -61,7 +75,8 @@ public static Map<String, String> loadKeys(AtSign atSign) throws Exception {
file = getKeysFile(atSign, legacyKeysFilesLocation);
// if file does not exist under current working directory, we're done - can't find the keys file
if (!file.exists()) {
throw new AtClientConfigException("loadKeys: No file called " + atSign + keysFileSuffix + " at ~/.atsign/keys or ./keys" +
throw new AtClientConfigException("loadKeys: No file called " + atSign + keysFileSuffix
+ " at " + expectedKeysFilesLocation + " or " + legacyKeysFilesLocation +
"\t Keys files are expected to be in ~/.atsign/keys/ (canonical location) or ./keys/ (legacy location)");
}
}
Expand All @@ -86,6 +101,15 @@ public static Map<String, String> loadKeys(AtSign atSign) throws Exception {
}

public static File getKeysFile(AtSign atSign, String folderToLookIn) {
return new File(folderToLookIn + atSign + keysFileSuffix);
return new File(folderToLookIn, atSign + keysFileSuffix);
}

private static String getFirstNonEmpty(String... candidates) {
for (String candidate : candidates) {
if (candidate != null && candidate.trim().length() > 0) {
return candidate;
}
}
throw new IllegalArgumentException("all candidates are null");
}
}
3 changes: 3 additions & 0 deletions at_client/src/main/java/org/atsign/common/Metadata.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.atsign.common;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
Expand All @@ -14,6 +15,8 @@
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;


@JsonIgnoreProperties(ignoreUnknown = true)
public class Metadata {
static final ObjectMapper mapper = new ObjectMapper();

Expand Down
23 changes: 23 additions & 0 deletions at_client/src/test/java/org/atsign/cucumber/CucumberTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.atsign.cucumber;

import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
import io.cucumber.plugin.EventListener;
import io.cucumber.plugin.event.EventPublisher;
import io.cucumber.plugin.event.TestRunStarted;
import org.atsign.virtualenv.VirtualEnv;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(
features = "src/test/resources/features",
glue = {"org.atsign.cucumber.steps"},
plugin = {"pretty", "org.atsign.cucumber.CucumberTests"}
)
public class CucumberTests implements EventListener {

@Override
public void setEventPublisher(EventPublisher publisher) {
publisher.registerHandlerFor(TestRunStarted.class, e -> VirtualEnv.setUp());
}
}
120 changes: 120 additions & 0 deletions at_client/src/test/java/org/atsign/cucumber/helpers/Helpers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package org.atsign.cucumber.helpers;

import org.junit.jupiter.api.Assertions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

public class Helpers {

private static final Logger LOGGER = LoggerFactory.getLogger(Helpers.class);

public static boolean testContains(List<Map<String, String>> actualMaps, List<Map<String, String>> expectedMaps, boolean exactMatch) {
List<Map<String, String>> actuals = toCanonicalMaps(actualMaps);
List<Map<String, String>> expecteds = toCanonicalMaps(expectedMaps);
for (Map<String, String> expected : expecteds) {
int match = findMatch(actuals, expected);
if (match >= 0) {
actuals.remove(match);
} else {
return false;
}
}
return !exactMatch || actuals.isEmpty();
}

public static void assertContains(List<Map<String, String>> actualMaps, List<Map<String, String>> expectedMaps, boolean exactMatch) {
List<Map<String, String>> actuals = toCanonicalMaps(actualMaps);
List<Map<String, String>> expecteds = toCanonicalMaps(expectedMaps);
for (Map<String, String> expected : expecteds) {
int match = findMatch(actuals, expected);
if (match >= 0) {
actuals.remove(match);
} else {
Assertions.fail("no match for " + expected);
}
}
if (exactMatch) {
assertThat(actuals, is(empty()));
}
}

public static int findMatch(List<Map<String, String>> actuals, Map<String, String> expected) {
for (int i = 0; i < actuals.size(); i++) {
if (isMatch(actuals.get(i), expected)) {
return i;
}
}
return -1;
}

public static List<Map<String, String>> toCanonicalMaps(List<Map<String, String>> maps) {
return maps.stream()
.map(x -> toCanonicalMap(x))
.collect(Collectors.toCollection(ArrayList::new));
}

public static Map<String, String> toCanonicalMap(Map<String, String> map) {
return map.entrySet().stream()
.collect(Collectors.toMap(e -> toCanonicalKey(e.getKey()), e -> e.getValue() != null ? e.getValue() : ""));
}

public static String toCanonicalKey(String key) {
return key.toLowerCase().replaceAll("\\s+", "");
}

public static boolean isMatch(Map<String, String> actual, Map<String, String> expected) {
for (Map.Entry<String, String> entry : expected.entrySet()) {
if (!isMatch(actual.get(entry.getKey()), entry.getValue())) {
return false;
}
}
return expected.size() > 0;
}

public static boolean isMatch(String actual, String expected) {
if (expected == null || expected.isEmpty()) {
return true;
}
if (actual == null) {
return false;
}
return actual.matches(expected);
}

public static void addKeyValues(List<Map<String, String>> expected, String... keyValues) {
Map<String, String> map = new HashMap<>();
assertThat(keyValues.length % 2, equalTo(0));
for (int i = 0; i < keyValues.length; i++) {
map.put(keyValues[i], keyValues[++i]);
}
expected.add(map);
}

public static boolean isHostPortReachable(String hostAndPort, long timeoutMillis) {
try (Socket socket = new Socket()) {
Matcher matcher = Pattern.compile("([^:]+):(\\d+)").matcher(hostAndPort);
if (matcher.matches()) {
String host = matcher.group(1);
int port = Integer.parseInt(matcher.group(2));
socket.connect(new InetSocketAddress(host, port), (int) timeoutMillis);
return true;
}
} catch (IOException e) {
}
return false;
}
}
Loading