Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements InMemory AASX file server #84

Merged
merged 20 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,7 @@
public class InMemoryAASXFileServer implements AASXFileServer {

private Map<String, Package> packageMap = new LinkedHashMap<>();
private AtomicInteger packageId = new AtomicInteger(0);

/**
* Creates the InMemoryAASXFileServer
*/
public InMemoryAASXFileServer() {}
private AtomicInteger packageId = new AtomicInteger(0);

@Override
public Collection<PackageDescription> getAllAASXPackageIds() {
Expand Down Expand Up @@ -80,8 +75,10 @@ public void updateAASXByPackageId(String packageId, List<String> aasIds, InputSt
public PackageDescription createAASXPackage(List<String> aasIds, InputStream file, String fileName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As identifier is generated automatically at runtime, there is no chance of collision.

Please remove this throws declaration.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

throws CollidingIdentifierException {

String newpackageId = String.valueOf(packageId.incrementAndGet());
PackageDescription packageDescription = createPackageDescription(aasIds, newpackageId);
String newpackageId = String.valueOf(packageId.incrementAndGet());

PackageDescription packageDescription = createPackageDescription(aasIds, newpackageId);

createPackage(aasIds, file, fileName, newpackageId, packageDescription);

return packageDescription;
Expand Down Expand Up @@ -112,17 +109,25 @@ private PackagesBody createPackagesBody(List<String> aasIds, InputStream file, S
}

private void createPackage(List<String> aasIds, InputStream file, String fileName, String newPackageId, PackageDescription packageDescription) {
PackagesBody packagesBody = createPackagesBody(aasIds, file, fileName);
Package aasxPackage = new Package(newPackageId, packageDescription, packagesBody);
PackagesBody packagesBody = createPackagesBody(aasIds, file, fileName);

Package aasxPackage = new Package(newPackageId, packageDescription, packagesBody);

packageMap.put(newPackageId, aasxPackage);
}

private void updateAASXPackage(String packageId, List<String> aasIds, InputStream file, String filename) {
Package aasxPackage = this.packageMap.get(packageId);
aasxPackage.getPackagesBody().setAasIds(aasIds);
aasxPackage.getPackagesBody().setFileName(filename);
aasxPackage.getPackagesBody().setFile(file);
Package aasxPackage = this.packageMap.get(packageId);

updatePackagesBody(aasIds, file, filename, aasxPackage.getPackagesBody());

aasxPackage.getPackageDescription().setAasIds(aasIds);
}

private void updatePackagesBody(List<String> aasIds, InputStream file, String filename, PackagesBody packagesBody) {
packagesBody.setAasIds(aasIds);
packagesBody.setFileName(filename);
packagesBody.setFile(file);
}

private void throwIfAASXPackageIdDoesNotExist(String id) {
Expand Down
12 changes: 10 additions & 2 deletions basyx.aasxfileserver/basyx.aasxfileserver-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

<artifactId>basyx.aasxfileserver-core</artifactId>
<name>AASX File Server Core</name>

<dependencies>
<dependency>
<groupId>org.eclipse.digitaltwin.basyx</groupId>
Expand All @@ -21,5 +21,13 @@
<groupId>org.eclipse.digitaltwin.aas4j</groupId>
<artifactId>model</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<scope>test</scope>
</dependency>

</dependencies>
</project>


</project>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The createAASXPackage test is missing, please add it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added the test case

Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@
import java.util.Collection;
import java.util.List;
import java.util.Iterator;
import org.apache.commons.io.IOUtils;

import org.eclipse.digitaltwin.basyx.aasxfileserver.AASXFileServer;
import org.eclipse.digitaltwin.basyx.aasxfileserver.PackageDescription;
import org.eclipse.digitaltwin.basyx.core.exceptions.ElementDoesNotExistException;
import org.junit.Test;
import org.eclipse.digitaltwin.basyx.aasxfileserver.core.DummyAASXFileServerFactory;

/**
* Testsuite for implementations of the {@link AASXFileServer} interface
Expand All @@ -49,134 +49,122 @@
*
*/
public abstract class AASXFileServerSuite {
protected abstract AASXFileServer getAASXFileServer();

protected abstract AASXFileServer getAASXFileServer();

@Test
public void getAllAASXPackageIds() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test case for getAASXFileServer(String shellId)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test case.

AASXFileServer server = getAASXFileServer();
Collection<PackageDescription> packageDescriptions = DummyAASXFileServerFactory.getAllDummyAASXPackages(server);

AASXFileServer server = getAASXFileServer();
Collection<PackageDescription> packageDescriptions = DummyAASXFileServerFactory.getAllDummyAASXPackages(server);

assertGetAllAASXPackageIds(packageDescriptions);
}

@Test
public void getAllAASXPackageIdsEmpty() {
AASXFileServer server = getAASXFileServer();
Collection<PackageDescription> packageDescriptions = server.getAllAASXPackageIds();
assertIsEmpty(packageDescriptions);

AASXFileServer server = getAASXFileServer();
Collection<PackageDescription> packageDescriptions = server.getAllAASXPackageIds();

assertTrue(packageDescriptions.isEmpty());
}

@Test(expected = ElementDoesNotExistException.class)
public void getSpecificNonExistingPackageId() {

AASXFileServer server = getAASXFileServer();
server.getAASXByPackageId("doesNotExist");
}

@Test
public void updateExistingAASXByPackageId() {
AASXFileServer server = getAASXFileServer();
PackageDescription expectedPackageDescription = DummyAASXFileServerFactory.createFirstDummyAASXPackage(server);
updateAasxPackage(server, expectedPackageDescription.getPackageId(), DummyAASXFileServerFactory.SECOND_AAS_IDS ,
DummyAASXFileServerFactory.SECOND_FILE, DummyAASXFileServerFactory.SECOND_FILENAME);
public void updateExistingAASXByPackageId() {

AASXFileServer server = getAASXFileServer();

PackageDescription expectedPackageDescription = DummyAASXFileServerFactory.createFirstDummyAASXPackage(server);

updateAasxPackage(server, expectedPackageDescription.getPackageId(), DummyAASXFileServerFactory.SECOND_AAS_IDS,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last two parameters are not required, please remove them and update the method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

DummyAASXFileServerFactory.SECOND_FILE, DummyAASXFileServerFactory.SECOND_FILENAME);

Collection<PackageDescription> actualPackageDescription = server.getAllAASXPackageIds();

assertUpdatedAASXPackageId(expectedPackageDescription, actualPackageDescription, server);
}
}

@Test(expected = ElementDoesNotExistException.class)
public void updateNonExistingAASXByPackageId() {

String packageId = "notExisting";

String packageId = "notExisting";
AASXFileServer server = getAASXFileServer();
server.updateAASXByPackageId(packageId, DummyAASXFileServerFactory.FIRST_AAS_IDS,
DummyAASXFileServerFactory.FIRST_FILE, DummyAASXFileServerFactory.FIRST_FILENAME);
}

AASXFileServer server = getAASXFileServer();

server.updateAASXByPackageId(packageId, DummyAASXFileServerFactory.FIRST_AAS_IDS, DummyAASXFileServerFactory.FIRST_FILE, DummyAASXFileServerFactory.FIRST_FILENAME);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please utilize the method for this purpose (updateAasxPackage)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}

@Test
public void getAASXByPackageId() throws ElementDoesNotExistException, IOException {

AASXFileServer server = getAASXFileServer();
PackageDescription packageDescription = DummyAASXFileServerFactory.createFirstDummyAASXPackage(server);
InputStream actualValue = server.getAASXByPackageId(packageDescription.getPackageId());
InputStream expectedValue = DummyAASXFileServerFactory.FIRST_FILE;

assertInputStreamsEqual(expectedValue, actualValue);

PackageDescription packageDescription = DummyAASXFileServerFactory.createFirstDummyAASXPackage(server);

InputStream actualValue = server.getAASXByPackageId(packageDescription.getPackageId());
InputStream expectedValue = DummyAASXFileServerFactory.FIRST_FILE;

assertTrue(IOUtils.contentEquals(expectedValue, actualValue));
}

@Test
public void deleteAASXByPackageId() {

AASXFileServer server = getAASXFileServer();
PackageDescription packageDescription = DummyAASXFileServerFactory.createFirstDummyAASXPackage(server);
server.deleteAASXByPackageId(packageDescription.getPackageId());

PackageDescription packageDescription = DummyAASXFileServerFactory.createFirstDummyAASXPackage(server);

server.deleteAASXByPackageId(packageDescription.getPackageId());

try {
server.getAASXByPackageId(packageDescription.getPackageId());
fail();
} catch (ElementDoesNotExistException expected) {
}
}
}

@Test(expected = ElementDoesNotExistException.class)
public void deleteNonExistingAasxFileServer() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name of this method is not correct. Please correct it (suggestion: deleteNonExistingAASX)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


AASXFileServer server = getAASXFileServer();
server.deleteAASXByPackageId("nonExisting");
}

private void updateAasxPackage(AASXFileServer server, String packageId, List<String> expectedAasIds, InputStream secondFile,
String secondFilename) {

server.updateAASXByPackageId(packageId, expectedAasIds,
DummyAASXFileServerFactory.SECOND_FILE, DummyAASXFileServerFactory.SECOND_FILENAME);

}

private void assertGetAllAASXPackageIds(Collection<PackageDescription> packageDescriptions) {
assertEquals(2, packageDescriptions.size());

Iterator<PackageDescription> iterator = packageDescriptions.iterator();

PackageDescription expectedFirstPackage = iterator.next();
PackageDescription expectedSecondPackage = iterator.next();

assertTrue(packageDescriptions.containsAll(Arrays.asList(expectedFirstPackage, expectedSecondPackage)));
}

private void assertUpdatedAASXPackageId(PackageDescription expectedPackageDescription, Collection<PackageDescription> actualPackageDescriptions, AASXFileServer server) {
assertEquals(1, actualPackageDescriptions.size());
assertTrue(actualPackageDescriptions.contains(expectedPackageDescription));

InputStream actualAASXFile = server.getAASXByPackageId("1");
InputStream expectedAASXFile = DummyAASXFileServerFactory.SECOND_FILE;

assertEquals(expectedAASXFile,actualAASXFile);

private void updateAasxPackage(AASXFileServer server, String packageId, List<String> expectedAasIds,
InputStream secondFile, String secondFilename) {

server.updateAASXByPackageId(packageId, expectedAasIds, DummyAASXFileServerFactory.SECOND_FILE, DummyAASXFileServerFactory.SECOND_FILENAME);
}

private void assertIsEmpty(Collection<PackageDescription> packageDescription) {
assertTrue(packageDescription.isEmpty());
}

private boolean assertInputStreamsEqual(InputStream expectedValue, InputStream actualValue) throws IOException {
int expectedByte;
int actualByte;

while ((expectedByte = expectedValue.read()) != -1) {
actualByte = actualValue.read();

if (expectedByte != actualByte) {
return false;
}
}
return actualValue.read() == -1;

private void assertGetAllAASXPackageIds(Collection<PackageDescription> packageDescriptions) {
assertEquals(2, packageDescriptions.size());

Iterator<PackageDescription> iterator = packageDescriptions.iterator();

PackageDescription expectedFirstPackage = iterator.next();
PackageDescription expectedSecondPackage = iterator.next();

assertTrue(packageDescriptions.containsAll(Arrays.asList(expectedFirstPackage, expectedSecondPackage)));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong, actual is same as the expected.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changes done

}

private void assertUpdatedAASXPackageId(PackageDescription expectedPackageDescription,
Collection<PackageDescription> actualPackageDescriptions, AASXFileServer server) {

assertEquals(1, actualPackageDescriptions.size());
assertTrue(actualPackageDescriptions.contains(expectedPackageDescription));

InputStream actualAASXFile = server.getAASXByPackageId("1");
InputStream expectedAASXFile = DummyAASXFileServerFactory.SECOND_FILE;

assertEquals(expectedAASXFile, actualAASXFile);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use IOUtils to compare the content of the IS.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

}

}