From 64508b209994877cec36448dc43580719e526fdc Mon Sep 17 00:00:00 2001 From: user1 Date: Fri, 1 Jul 2016 11:35:07 +0200 Subject: [PATCH] fix path issues + add daemon build --- build_release_deb.sh | 10 ++- build_release_rpm.sh | 10 ++- config/application.yml | 20 +++++ debian/DEBIAN/postinst | 30 +++++++ debian/etc/systemd/system/cdmi-server.service | 11 +++ .../system/identity-harmonization.service | 11 +++ debian/usr/bin/cdmi-server | 2 - pom.xml | 5 +- rpm/SOURCES/cdmi-server | 2 - rpm/SOURCES/cdmi-server.service | 11 +++ rpm/SPECS/cdmi-server.spec | 38 +++++++-- .../java/edu/kit/scc/CdmiRestController.java | 1 + .../kit/scc/FilesystemBeanConfiguration.java | 2 +- .../edu/kit/scc/FilesystemConfiguration.java | 44 +++++----- .../dao/filesystem/CapabilityDaoImpl.java | 43 +++++----- .../dao/filesystem/CdmiObjectDaoImpl.java | 67 ++++++++------- .../dao/filesystem/ContainerDaoImpl.java | 64 +++++++------- .../dao/filesystem/DataObjectDaoImpl.java | 83 ++++++++----------- .../dao/filesystem/DomainDaoImpl.java | 6 +- .../resources/application-test.properties | 9 ++ src/test/java/edu/kit/scc/TestSuite.java | 19 ----- .../filesystem/CapabilityFilesystemTest.java | 3 +- .../filesystem/CdmiObjectFilesystemTest.java | 6 +- .../filesystem/ContainerFilesystemTest.java | 18 ++-- .../filesystem/DataObjectFilesystemTest.java | 15 ++-- .../cdmi/filesystem/FileSystemTestSuite.java | 2 +- .../kit/scc/cdmi/rest/AuthorizationTest.java | 2 + .../kit/scc/cdmi/rest/CapabilitiesTest.java | 2 + .../edu/kit/scc/cdmi/rest/CdmiObjectTest.java | 2 + .../edu/kit/scc/cdmi/rest/ContainerTest.java | 4 +- .../edu/kit/scc/cdmi/rest/DataObjectTest.java | 2 + .../edu/kit/scc/cdmi/rest/DomainTest.java | 2 + .../edu/kit/scc/cdmi/rest/FilterJsonTest.java | 2 + .../edu/kit/scc/cdmi/rest/RestTestSuite.java | 2 +- .../kit/scc/http/client/HttpClientTest.java | 3 + .../kit/scc/http/client/HttpTestSuite.java | 53 ++++++++++++ src/test/java/edu/kit/scc/test/TestSuite.java | 70 ++++++++++++++++ .../edu/kit/scc/{ => utils}/UtilsTest.java | 7 +- .../edu/kit/scc/utils/UtilsTestSuite.java | 62 ++++++++++++++ 39 files changed, 513 insertions(+), 232 deletions(-) create mode 100644 config/application.yml create mode 100755 debian/DEBIAN/postinst create mode 100644 debian/etc/systemd/system/cdmi-server.service create mode 100644 debian/etc/systemd/system/identity-harmonization.service delete mode 100755 debian/usr/bin/cdmi-server delete mode 100755 rpm/SOURCES/cdmi-server create mode 100644 rpm/SOURCES/cdmi-server.service create mode 100644 src/main/resources/application-test.properties delete mode 100644 src/test/java/edu/kit/scc/TestSuite.java create mode 100644 src/test/java/edu/kit/scc/test/TestSuite.java rename src/test/java/edu/kit/scc/{ => utils}/UtilsTest.java (85%) create mode 100644 src/test/java/edu/kit/scc/utils/UtilsTestSuite.java diff --git a/build_release_deb.sh b/build_release_deb.sh index 5053102..8d6d284 100755 --- a/build_release_deb.sh +++ b/build_release_deb.sh @@ -1,10 +1,14 @@ #!/bin/bash +VERSION=0.1 +NAME=cdmi-server + mvn clean package -mkdir -p debian/usr/lib/cdmi-server -cp target/cdmi-server-0.1-SNAPSHOT.jar debian/usr/lib/cdmi-server +mkdir -p debian/var/lib/$NAME/config/ +cp config/application.yml debian/var/lib/$NAME/config/ +cp target/$NAME-$VERSION-SNAPSHOT.jar debian/var/lib/$NAME/ dpkg --build debian -mv debian.deb cdmi-server-1.0.deb +mv debian.deb $NAME-$VERSION.deb diff --git a/build_release_rpm.sh b/build_release_rpm.sh index 636108f..360cab8 100755 --- a/build_release_rpm.sh +++ b/build_release_rpm.sh @@ -1,11 +1,15 @@ #!/bin/bash +VERSION=0.1 +NAME=cdmi-server TOPDIR=`pwd`/rpm mvn clean package -cp target/cdmi-server-0.1-SNAPSHOT.jar $TOPDIR/SOURCES +cp target/$NAME-$VERSION-SNAPSHOT.jar $TOPDIR/SOURCES +cp config/application.yml $TOPDIR/SOURCES -rpmbuild --define "_topdir ${TOPDIR}" -ba $TOPDIR/SPECS/cdmi-server.spec +rpmbuild --define "_topdir ${TOPDIR}" -ba $TOPDIR/SPECS/$NAME.spec -cp ${TOPDIR}/RPMS/x86_64/cdmi-server-1.0-1.el7.centos.x86_64.rpm . +cp ${TOPDIR}/RPMS/x86_64/cdmi-server-0.1-1.x86_64.rpm . +#cp ${TOPDIR}/RPMS/x86_64/cdmi-server-1.0-1.el7.centos.x86_64.rpm . diff --git a/config/application.yml b/config/application.yml new file mode 100644 index 0000000..d527a69 --- /dev/null +++ b/config/application.yml @@ -0,0 +1,20 @@ +spring: + profiles: + active: filesystem + +server: + port: 8888 + +rest: + user: restadmin + pass: restadmin +--- +spring: + profiles: filesystem +cdmi: + data: + baseDirectory: /tmp/cdmi + objectIdPrefix: .cdmi_ + qos: + backend: + type: dummy_filesystem \ No newline at end of file diff --git a/debian/DEBIAN/postinst b/debian/DEBIAN/postinst new file mode 100755 index 0000000..7990676 --- /dev/null +++ b/debian/DEBIAN/postinst @@ -0,0 +1,30 @@ +#!/bin/sh + +NAME=cdmi-server +VERSION=0.1 +USER=cdmi + +count=`egrep -c "^${USER}:" /etc/group` +if [ $count -eq 0 ]; then + echo "No ${USER} group found ... create group" + addgroup --system $USER +fi + +/usr/bin/id -u $USER > /dev/null 2>&1 +if [ $? -eq 1 ]; then + echo "No ${USER} user found .. create user" + adduser --system $USER --ingroup $USER +fi + +if [ -f /var/lib/$NAME/$NAME-$VERSION-SNAPSHOT.jar ]; then + chmod +x /var/lib/$NAME/$NAME-$VERSION-SNAPSHOT.jar +fi + +chown -R $USER:$USER /var/lib/$NAME/ + +if [ ! -L /etc/init.d/$NAME ]; then + ln -s /var/lib/$NAME/$NAME-$VERSION-SNAPSHOT.jar /etc/init.d/$NAME +fi + +service $NAME start +update-rc.d $NAME defaults 99 diff --git a/debian/etc/systemd/system/cdmi-server.service b/debian/etc/systemd/system/cdmi-server.service new file mode 100644 index 0000000..6aef54a --- /dev/null +++ b/debian/etc/systemd/system/cdmi-server.service @@ -0,0 +1,11 @@ +[Unit] +Description=cdmi-server +After=syslog.target + +[Service] +User=cdmi +ExecStart=/var/lib/cdmi-server/cdmi-server-0.1-SNAPSHOT.jar +SuccessExitStatus=143 + +[Install] +WantedBy=multi-user.target diff --git a/debian/etc/systemd/system/identity-harmonization.service b/debian/etc/systemd/system/identity-harmonization.service new file mode 100644 index 0000000..6aef54a --- /dev/null +++ b/debian/etc/systemd/system/identity-harmonization.service @@ -0,0 +1,11 @@ +[Unit] +Description=cdmi-server +After=syslog.target + +[Service] +User=cdmi +ExecStart=/var/lib/cdmi-server/cdmi-server-0.1-SNAPSHOT.jar +SuccessExitStatus=143 + +[Install] +WantedBy=multi-user.target diff --git a/debian/usr/bin/cdmi-server b/debian/usr/bin/cdmi-server deleted file mode 100755 index 31b87ea..0000000 --- a/debian/usr/bin/cdmi-server +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -java -jar /usr/lib/cdmi-server/cdmi-server-0.1-SNAPSHOT.jar "$@" diff --git a/pom.xml b/pom.xml index 2ff083b..cd4bb9b 100644 --- a/pom.xml +++ b/pom.xml @@ -89,10 +89,7 @@ false - edu.kit.scc.cdmi.filesystem.FileSystemTestSuite.java - edu.kit.scc.cdmi.rest.RestTestSuite.java - edu.kit.scc.TestSuite.java - edu.kit.scc.http.client.HttpTestSuite.java + edu.kit.scc.test.TestSuite.java diff --git a/rpm/SOURCES/cdmi-server b/rpm/SOURCES/cdmi-server deleted file mode 100755 index 31b87ea..0000000 --- a/rpm/SOURCES/cdmi-server +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -java -jar /usr/lib/cdmi-server/cdmi-server-0.1-SNAPSHOT.jar "$@" diff --git a/rpm/SOURCES/cdmi-server.service b/rpm/SOURCES/cdmi-server.service new file mode 100644 index 0000000..6aef54a --- /dev/null +++ b/rpm/SOURCES/cdmi-server.service @@ -0,0 +1,11 @@ +[Unit] +Description=cdmi-server +After=syslog.target + +[Service] +User=cdmi +ExecStart=/var/lib/cdmi-server/cdmi-server-0.1-SNAPSHOT.jar +SuccessExitStatus=143 + +[Install] +WantedBy=multi-user.target diff --git a/rpm/SPECS/cdmi-server.spec b/rpm/SPECS/cdmi-server.spec index 8281956..abf9982 100644 --- a/rpm/SPECS/cdmi-server.spec +++ b/rpm/SPECS/cdmi-server.spec @@ -1,9 +1,13 @@ %define __jar_repack %{nil} %define _tmppath %{_topdir}/tmp -%define buildroot %{_topdir}/hello-rpm-root +%define buildroot %{_topdir}/build-rpm-root -Name: cdmi-server -Version: 1.0 +%define name cdmi-server +%define jarversion 0.1 +%define user cdmi + +Name: %{name} +Version: %{jarversion} Release: 1%{?dist} Summary: SNIA CDMI server reference implementation. @@ -22,14 +26,30 @@ Standalone Spring Boot application version. %build %install -mkdir -p %{buildroot}/usr/local/bin -mkdir -p %{buildroot}/usr/lib/cdmi-server -cp %{_topdir}/SOURCES/cdmi-server %{buildroot}/usr/local/bin/cdmi-server -cp %{_topdir}/SOURCES/cdmi-server-0.1-SNAPSHOT.jar %{buildroot}/usr/lib/cdmi-server +mkdir -p %{buildroot}/var/lib/%{name}/config +mkdir -p %{buildroot}/etc/systemd/system +cp %{_topdir}/SOURCES/application.yml %{buildroot}/var/lib/%{name}/config +cp %{_topdir}/SOURCES/%{name}-%{jarversion}-SNAPSHOT.jar %{buildroot}/var/lib/%{name} +cp %{_topdir}/SOURCES/%{name}.service %{buildroot}/etc/systemd/system %files -/usr/local/bin/cdmi-server -/usr/lib/cdmi-server/cdmi-server-0.1-SNAPSHOT.jar +/var/lib/%{name}/config/application.yml +/var/lib/%{name}/%{name}-%{jarversion}-SNAPSHOT.jar +/etc/systemd/system/%{name}.service %changelog +%post +/usr/bin/id -u %{user} > /dev/null 2>&1 +if [ $? -eq 1 ]; then + adduser --system --user-group %{user} +fi + +if [ -f /var/lib/%{name}/%{name}-%{jarversion}-SNAPSHOT.jar ]; then + chmod +x /var/lib/%{name}/%{name}-%{jarversion}-SNAPSHOT.jar +fi + +chown -R %{user}:%{user} /var/lib/%{name} + +systemctl start %{name}.service +systemctl enable %{name}.service diff --git a/src/main/java/edu/kit/scc/CdmiRestController.java b/src/main/java/edu/kit/scc/CdmiRestController.java index 6c64527..0130e8b 100644 --- a/src/main/java/edu/kit/scc/CdmiRestController.java +++ b/src/main/java/edu/kit/scc/CdmiRestController.java @@ -564,6 +564,7 @@ public boolean verifyAuthorization(String authorizationHeader) { String body = "token=" + encodedCredentials; response = httpClient.makeHttpsPostRequest(clientId, clientSecret, body, tokenInfo); if (response.statusCode == HttpStatus.OK.value()) { + log.debug("Token info {}", response.getResponseString()); // TODO set client ACLs return true; } diff --git a/src/main/java/edu/kit/scc/FilesystemBeanConfiguration.java b/src/main/java/edu/kit/scc/FilesystemBeanConfiguration.java index d8b7745..474e71e 100644 --- a/src/main/java/edu/kit/scc/FilesystemBeanConfiguration.java +++ b/src/main/java/edu/kit/scc/FilesystemBeanConfiguration.java @@ -25,7 +25,7 @@ import org.springframework.context.annotation.Profile; @Configuration -@Profile("filesystem") +@Profile({"filesystem", "test"}) public class FilesystemBeanConfiguration { @Value("${cdmi.data.baseDirectory}") diff --git a/src/main/java/edu/kit/scc/FilesystemConfiguration.java b/src/main/java/edu/kit/scc/FilesystemConfiguration.java index 5ccfbea..4de3fa9 100644 --- a/src/main/java/edu/kit/scc/FilesystemConfiguration.java +++ b/src/main/java/edu/kit/scc/FilesystemConfiguration.java @@ -21,7 +21,6 @@ import org.snia.cdmiserver.model.Capability; import org.snia.cdmiserver.model.CdmiObject; import org.snia.cdmiserver.model.Container; -import org.snia.cdmiserver.model.Domain; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Profile; @@ -37,7 +36,7 @@ import javax.annotation.PreDestroy; @Component -@Profile("filesystem") +@Profile({"filesystem", "test"}) public class FilesystemConfiguration { private static final Logger log = LoggerFactory.getLogger(FilesystemConfiguration.class); @@ -63,29 +62,37 @@ public class FilesystemConfiguration { public void init() throws IOException { log.debug("Set-up root container..."); - CdmiObject rootObject = new CdmiObject(); - Path path = Paths.get(baseDirectory); if (!Files.exists(path)) { - Files.createDirectories(Paths.get(baseDirectory, "cdmi_objectid")); + Files.createDirectory(Paths.get(baseDirectory)); log.debug("root directory {} created", path.toString()); } - Container rootContainer = new Container("/", "/", rootObject.getObjectId()); - rootContainer.setObjectId(rootObject.getObjectId()); - cdmiObjectDao.createCdmiObject(rootContainer, baseDirectory); + path = Paths.get(baseDirectory, "cdmi_objectid"); + if (!Files.exists(path)) { + Files.createDirectory(path); + log.debug("cdmi objectid directory {} created", path.toString()); + } path = Paths.get(baseDirectory, "cdmi_capabilities"); if (!Files.exists(path)) { - Files.createDirectory(Paths.get(baseDirectory, "cdmi_capabilities")); - log.debug("capabilities directory {} created", path.toString()); + Files.createDirectory(path); + log.debug("cdmi objectid directory {} created", path.toString()); } - rootObject = cdmiObjectDao.getCdmiObjectByPath(baseDirectory); + CdmiObject rootObject = new CdmiObject(); + Container rootContainer = new Container("/", "/", rootObject.getObjectId()); + rootContainer.setObjectId(rootObject.getObjectId()); + + rootContainer = (Container) cdmiObjectDao.createCdmiObject(rootContainer, "/"); + log.debug("root container created {}", rootContainer.toString()); + + rootObject = cdmiObjectDao.getCdmiObjectByPath("/"); Capability rootCapability = new Capability("cdmi_capabilities", "/", rootObject.getObjectId()); - cdmiObjectDao.createCdmiObject(rootCapability, - Paths.get(baseDirectory, "cdmi_capabilities").toString()); + rootCapability = + (Capability) cdmiObjectDao.createCdmiObject(rootCapability, "/cdmi_capabilities"); + log.debug("root capability created {}", rootCapability.toString()); Capability containerCapability = new Capability("container", "/cdmi_capabilities", rootCapability.getObjectId()); @@ -97,17 +104,6 @@ public void init() throws IOException { capabilityDao.createByPath(Paths.get("cdmi_capabilities", "dataobject").toString(), dataObjectCapability); - path = Paths.get(baseDirectory, "cdmi_domains"); - if (!Files.exists(path)) { - Files.createDirectory(path); - log.debug("domain directory {} created", path.toString()); - } - rootObject = cdmiObjectDao.getCdmiObjectByPath(baseDirectory); - - Domain rootDomain = new Domain("cdmi_domains", "/", rootObject.getObjectId()); - - cdmiObjectDao.createCdmiObject(rootDomain, Paths.get(baseDirectory, "cdmi_domains").toString()); - Capability defaultContainerCapability = capabilityDao.findByPath(Paths.get("cdmi_capabilities", "container").toString()); log.debug(defaultContainerCapability.toString()); diff --git a/src/main/java/org/snia/cdmiserver/dao/filesystem/CapabilityDaoImpl.java b/src/main/java/org/snia/cdmiserver/dao/filesystem/CapabilityDaoImpl.java index c8c102e..9a4adad 100644 --- a/src/main/java/org/snia/cdmiserver/dao/filesystem/CapabilityDaoImpl.java +++ b/src/main/java/org/snia/cdmiserver/dao/filesystem/CapabilityDaoImpl.java @@ -51,7 +51,7 @@ */ public class CapabilityDaoImpl implements CapabilityDao { - private static final Logger LOG = LoggerFactory.getLogger(CapabilityDaoImpl.class); + private static final Logger log = LoggerFactory.getLogger(CapabilityDaoImpl.class); private String baseDirectory; @@ -80,41 +80,41 @@ public Capability findByObjectId(String objectId) { @Override public Capability findByPath(String path) { - return (Capability) cdmiObjectDao - .getCdmiObjectByPath(Paths.get(baseDirectory.trim(), path.trim()).toString()); + return (Capability) cdmiObjectDao.getCdmiObjectByPath(path.trim()); } @Override public Capability createByPath(String path, Capability capabilityRequest) { - if (path == null) { - return null; - } - - // TODO: only allow paths containing "cdmi_capabilities" - - final Path capabilityPath = Paths.get(baseDirectory.trim(), path.trim()); - try { + final Path capabilityPath = Paths.get(baseDirectory.trim(), path.trim()); + // create the capability directory Files.createDirectory(capabilityPath); - LOG.debug("create capability object {}", path.trim()); + log.debug("create capability object {} {}", path.trim(), capabilityRequest.toString()); + log.debug("create directory {}", capabilityPath.toString()); } catch (FileAlreadyExistsException ex) { - LOG.error(ex.getMessage()); - return null; + log.error(ex.getMessage()); } catch (Exception ex) { - LOG.error(ex.getMessage()); + log.error(ex.getMessage()); return null; } - String objectName = capabilityPath.getFileName().toString(); - String parentUri = Paths.get(path.trim()).getParent().toString(); - String parentPath = capabilityPath.getParent().toString(); + Path urlPath = Paths.get(path.trim()); + Path parentPath = urlPath.getParent(); + if (parentPath == null) { + return null; + } + + // create the capability meta-data files Capability parentCapability = (Capability) cdmiObjectDao.getCdmiObjectByPath(parentPath.toString()); - Capability capability = new Capability(objectName, parentUri, parentCapability.getObjectId()); + Capability capability = new Capability(urlPath.getFileName().toString(), parentPath.toString(), + parentCapability.getObjectId()); + if (parentCapability.getChildren() == null) { parentCapability.setChildren(new JSONArray()); } + parentCapability.getChildren().put(capability.getObjectName()); String childrenRange = CdmiObject.getChildrenRange(parentCapability.getChildren()); parentCapability.setChildrenrange(childrenRange); @@ -122,10 +122,11 @@ public Capability createByPath(String path, Capability capabilityRequest) { capability.setMetadata(capabilityRequest.getMetadata()); capability.setCapabilities(capabilityRequest.getCapabilities()); - cdmiObjectDao.createCdmiObject(capability, capabilityPath.toString()); + if (cdmiObjectDao.createCdmiObject(capability, urlPath.toString()) == null) { + return (Capability) cdmiObjectDao.deleteCdmiObjectByPath(urlPath.toString()); + } cdmiObjectDao.updateCdmiObject(parentCapability); return capability; } - } diff --git a/src/main/java/org/snia/cdmiserver/dao/filesystem/CdmiObjectDaoImpl.java b/src/main/java/org/snia/cdmiserver/dao/filesystem/CdmiObjectDaoImpl.java index e5397dd..622dd39 100644 --- a/src/main/java/org/snia/cdmiserver/dao/filesystem/CdmiObjectDaoImpl.java +++ b/src/main/java/org/snia/cdmiserver/dao/filesystem/CdmiObjectDaoImpl.java @@ -20,6 +20,7 @@ import org.snia.cdmiserver.model.Domain; import org.snia.cdmiserver.util.MediaTypes; +import java.nio.file.FileAlreadyExistsException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -64,25 +65,24 @@ public void setObjectIdDirectory(String objectIdDirectory) { this.objectIdDirectory = objectIdDirectory; } - private Path getObjectPathForPath(String path) { - Path objectPath = Paths.get(path.trim()); + private Path getCdmiObjectFilePathByUrl(String path) { + Path fileSystemRoot = Paths.get(baseDirectory.trim()); + Path fileSystemPath = Paths.get(baseDirectory.trim(), path.trim()); - if (objectPath.getFileName() == null) { - log.debug("system path {}", - Paths.get(baseDirectory.trim(), objectIdPrefix + baseDirectory.trim()).toString()); - return Paths.get(baseDirectory.trim(), objectIdPrefix + baseDirectory.trim()); - } - String fileName = objectIdPrefix + objectPath.getFileName(); - Path parentPath = objectPath.getParent(); - if (parentPath == null || parentPath.toString().equals("/")) { - log.debug("system path {}", Paths.get(baseDirectory.trim(), fileName).toString()); - return Paths.get(baseDirectory.trim(), fileName); + Path returnPath = null; + + if (fileSystemPath.compareTo(fileSystemRoot) == 0) { + returnPath = fileSystemRoot.resolve(objectIdPrefix); + } else { + String name = objectIdPrefix + fileSystemPath.getFileName(); + returnPath = fileSystemPath.getParent().resolve(name); } - log.debug("system path {}", Paths.get(objectPath.getParent().toString(), fileName).toString()); - return Paths.get(objectPath.getParent().toString(), fileName); + + log.debug("return cdmi object file path {} for uri {}", returnPath.toString(), path); + return returnPath; } - private Path getObjectPathForId(String objectId) { + private Path getObjectIdFilePath(String objectId) { return Paths.get(baseDirectory.trim(), objectIdDirectory, objectId); } @@ -95,7 +95,7 @@ private Path getObjectPathForId(String objectId) { *

* * @param object the object's id - * @param path the file system path + * @param path the CDMI URL path * @return the created {@link CdmiObject} */ @Override @@ -105,14 +105,19 @@ public CdmiObject createCdmiObject(CdmiObject object, String path) { if (objectById != null) { try { - Files.createLink(getObjectPathForPath(path), getObjectPathForId(objectById.getObjectId())); + Files.createLink(getCdmiObjectFilePathByUrl(path), + getObjectIdFilePath(objectById.getObjectId())); // Files.write(getObjectPathForPath(path), object.toJson().toString().getBytes(), // StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW); - log.debug("create new objectId link {} to {}", getObjectPathForPath(path).toString(), - getObjectPathForId(objectById.getObjectId()).toString()); - + log.debug("create new objectId link {} to {}", getCdmiObjectFilePathByUrl(path).toString(), + getObjectIdFilePath(objectById.getObjectId()).toString()); + } catch (FileAlreadyExistsException ex) { + log.error("{} {}", ex.getClass().getName(), ex.getMessage()); + deleteCdmiObject(objectById.getObjectId()); + log.debug("return existing object {}", getCdmiObjectByPath(path).toString()); + return getCdmiObjectByPath(path); } catch (Exception ex) { // ex.printStackTrace(); log.error("{} {}", ex.getClass().getName(), ex.getMessage()); @@ -127,7 +132,7 @@ public CdmiObject createCdmiObject(CdmiObject object, String path) { public CdmiObject createCdmiObject(CdmiObject object) { try { - Files.write(getObjectPathForId(object.getObjectId()), object.toJson().toString().getBytes(), + Files.write(getObjectIdFilePath(object.getObjectId()), object.toJson().toString().getBytes(), StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW); log.debug("create new objectId file {} {}", object.toString(), object.toJson()); @@ -144,14 +149,14 @@ public CdmiObject createCdmiObject(CdmiObject object) { * Updates the given CDMI object. * * @param updateObject the updated object - * @param path the object's file system path + * @param path the CDMI URL path * @return the updated {@link CdmiObject} */ @Override public CdmiObject updateCdmiObject(CdmiObject updateObject, String path) { try { - Files.write(getObjectPathForPath(path), updateObject.toJson().toString().getBytes(), + Files.write(getCdmiObjectFilePathByUrl(path), updateObject.toJson().toString().getBytes(), StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING); log.debug("update objectId file {} {}", updateObject.toString(), updateObject.toJson()); @@ -168,7 +173,7 @@ public CdmiObject updateCdmiObject(CdmiObject updateObject, String path) { public CdmiObject updateCdmiObject(CdmiObject object) { try { - Files.write(getObjectPathForId(object.getObjectId()), object.toJson().toString().getBytes(), + Files.write(getObjectIdFilePath(object.getObjectId()), object.toJson().toString().getBytes(), StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING); log.debug("update objectId file {} {}", object.toString(), object.toJson()); @@ -184,7 +189,7 @@ public CdmiObject updateCdmiObject(CdmiObject object) { /** * Deletes a CdmiObject by path. * - * @param path the file system path to the CDMI object + * @param path the CDMI URL path * @return the deleted {@link CdmiObject} */ @Override @@ -193,7 +198,7 @@ public CdmiObject deleteCdmiObjectByPath(String path) { if (object != null) { try { - boolean deleted = Files.deleteIfExists(getObjectPathForPath(path)); + boolean deleted = Files.deleteIfExists(getCdmiObjectFilePathByUrl(path)); log.debug("delete objectId file {} success {}", path, deleted); @@ -214,7 +219,7 @@ public CdmiObject deleteCdmiObject(String objectId) { if (object != null) { try { - boolean deleted = Files.deleteIfExists(getObjectPathForId(objectId)); + boolean deleted = Files.deleteIfExists(getObjectIdFilePath(objectId)); log.debug("delete objectId file {} success {}", object.toString(), deleted); @@ -230,14 +235,14 @@ public CdmiObject deleteCdmiObject(String objectId) { /** * Gets a CDMI object by path. * - * @param path the object's file system path + * @param path the CDMI URL path * @return the {@link CdmiObject} */ @Override public CdmiObject getCdmiObjectByPath(String path) { try { - byte[] content = Files.readAllBytes(getObjectPathForPath(path)); + byte[] content = Files.readAllBytes(getCdmiObjectFilePathByUrl(path)); JSONObject json = new JSONObject(new String(content)); String objectType = json.optString("objectType"); @@ -255,7 +260,7 @@ public CdmiObject getCdmiObjectByPath(String path) { } } } catch (Exception ex) { - // ex.printStackTrace(); + //ex.printStackTrace(); log.error("{} {}", ex.getClass().getName(), ex.getMessage()); } return null; @@ -265,7 +270,7 @@ public CdmiObject getCdmiObjectByPath(String path) { public CdmiObject getCdmiObject(String objectId) { try { - byte[] content = Files.readAllBytes(getObjectPathForId(objectId)); + byte[] content = Files.readAllBytes(getObjectIdFilePath(objectId)); JSONObject json = new JSONObject(new String(content)); String objectType = json.optString("objectType"); diff --git a/src/main/java/org/snia/cdmiserver/dao/filesystem/ContainerDaoImpl.java b/src/main/java/org/snia/cdmiserver/dao/filesystem/ContainerDaoImpl.java index 5bdf1d4..3cc3150 100644 --- a/src/main/java/org/snia/cdmiserver/dao/filesystem/ContainerDaoImpl.java +++ b/src/main/java/org/snia/cdmiserver/dao/filesystem/ContainerDaoImpl.java @@ -58,7 +58,7 @@ */ public class ContainerDaoImpl implements ContainerDao { - private static final Logger LOG = LoggerFactory.getLogger(ContainerDaoImpl.class); + private static final Logger log = LoggerFactory.getLogger(ContainerDaoImpl.class); private String baseDirectoryName; @@ -82,47 +82,46 @@ public void setCdmiObjectDao(CdmiObjectDao cdmiObjectDao) { @Override public Container createByPath(String path, Container containerRequest) { - if (path == null) { - return null; - } - - final Path containerPath = Paths.get(baseDirectoryName.trim(), path.trim()); - try { + final Path containerPath = Paths.get(baseDirectoryName.trim(), path.trim()); // create the container directory Files.createDirectory(containerPath); - LOG.debug("create container {} {}", path.trim(), containerRequest.toString()); + log.debug("create container {} {}", path.trim(), containerRequest.toString()); + log.debug("create directory {}", containerPath.toString()); } catch (FileAlreadyExistsException ex) { - LOG.error("File already exists {}", ex.getMessage()); - return null; + log.error("File already exists {}", ex.getMessage()); } catch (Exception ex) { - LOG.error(ex.getMessage()); + log.error(ex.getMessage()); return null; } - // generate the container meta-data - String containerName = containerPath.getFileName().toString(); - String parentUri = Paths.get(path.trim()).getParent() == null ? "/" - : Paths.get(path.trim()).getParent().toString(); - String parentPath = containerPath.getParent() == null ? baseDirectoryName - : containerPath.getParent().toString(); + Path urlPath = Paths.get(path.trim()); + Path parentPath = urlPath.getParent(); + if (parentPath == null) { + // return root container + return (Container) cdmiObjectDao.getCdmiObjectByPath("/"); + } + // create the container meta-data files Container parentContainer = (Container) cdmiObjectDao.getCdmiObjectByPath(parentPath.toString()); - Container container = new Container(containerName, parentUri, parentContainer.getObjectId()); + Container container = new Container(urlPath.getFileName().toString(), parentPath.toString(), + parentContainer.getObjectId()); if (parentContainer.getChildren() == null) { parentContainer.setChildren(new JSONArray()); } + parentContainer.getChildren().put(container.getObjectName()); String childrenRange = CdmiObject.getChildrenRange(parentContainer.getChildren()); parentContainer.setChildrenrange(childrenRange); container.setCompletionStatus("Complete"); container.setMetadata(containerRequest.getMetadata()); + // container.setCapabilitiesUri(containerRequest.getCapabilitiesUri()); - cdmiObjectDao.createCdmiObject(container, containerPath.toString()); + container = (Container) cdmiObjectDao.createCdmiObject(container, urlPath.toString()); cdmiObjectDao.updateCdmiObject(parentContainer); return container; @@ -130,28 +129,26 @@ public Container createByPath(String path, Container containerRequest) { @Override public Container deleteByPath(String path) { - if (path == null) { - return null; - } - LOG.debug("delete container {}", path.trim()); + Container container = null; + try { + log.debug("delete container {}", path.trim()); - final Path containerPath = Paths.get(baseDirectoryName.trim(), path.trim()); - Container container = (Container) cdmiObjectDao.getCdmiObjectByPath(containerPath.toString()); + container = (Container) cdmiObjectDao.getCdmiObjectByPath(path.trim()); - if (container != null) { - try { - LOG.debug("delete directory {}", containerPath.toString()); + if (container != null) { + final Path containerPath = Paths.get(baseDirectoryName.trim(), path.trim()); + + log.debug("delete directory {}", containerPath.toString()); Files.delete(containerPath); cdmiObjectDao.deleteCdmiObject(container.getObjectId()); - cdmiObjectDao.deleteCdmiObjectByPath(containerPath.toString()); + cdmiObjectDao.deleteCdmiObjectByPath(path.trim()); // removeChild(containerPath.getFileName().toString(), // containerPath.getParent().toString()); - - } catch (Exception ex) { - LOG.error("ERROR: {}", ex.getMessage()); } + } catch (Exception ex) { + log.error("ERROR: {}", ex.getMessage()); } return container; } @@ -163,8 +160,7 @@ public Container findByObjectId(String objectId) { @Override public Container findByPath(String path) { - return (Container) cdmiObjectDao - .getCdmiObjectByPath(Paths.get(baseDirectoryName.trim(), path.trim()).toString()); + return (Container) cdmiObjectDao.getCdmiObjectByPath(path.trim()); } @Override diff --git a/src/main/java/org/snia/cdmiserver/dao/filesystem/DataObjectDaoImpl.java b/src/main/java/org/snia/cdmiserver/dao/filesystem/DataObjectDaoImpl.java index f4b416d..e30f62f 100644 --- a/src/main/java/org/snia/cdmiserver/dao/filesystem/DataObjectDaoImpl.java +++ b/src/main/java/org/snia/cdmiserver/dao/filesystem/DataObjectDaoImpl.java @@ -54,7 +54,7 @@ */ public class DataObjectDaoImpl implements DataObjectDao { - private static final Logger LOG = LoggerFactory.getLogger(DataObjectDaoImpl.class); + private static final Logger log = LoggerFactory.getLogger(DataObjectDaoImpl.class); private String baseDirectoryName; @@ -78,50 +78,50 @@ public void setCdmiObjectDao(CdmiObjectDao cdmiObjectDao) { @Override public DataObject createByPath(String path, DataObject dataObjectRequest) { - if (path == null) { - return null; - } - - final Path dataObjectPath = Paths.get(baseDirectoryName.trim(), path.trim()); - try { + final Path dataObjectPath = Paths.get(baseDirectoryName.trim(), path.trim()); + // create the data object file Files.createFile(dataObjectPath); - LOG.debug("create data object {} {}", path.trim(), dataObjectRequest.toString()); - + log.debug("create data object {} {}", path.trim(), dataObjectRequest.toString()); + log.debug("create file {}", dataObjectPath.toString()); if (dataObjectRequest.getValue() != null) { Files.write(dataObjectPath, dataObjectRequest.getValue().getBytes()); - LOG.debug("writing value to data object"); + log.debug("writing value to data object"); } } catch (FileAlreadyExistsException ex) { - LOG.error(ex.getMessage()); - return null; + log.error(ex.getMessage()); } catch (Exception ex) { - LOG.error(ex.getMessage()); + log.error(ex.getMessage()); return null; } - String objectName = dataObjectPath.getFileName().toString(); - String parentUri = Paths.get(path.trim()).getParent() == null ? "/" - : Paths.get(path.trim()).getParent().toString(); - String parentPath = dataObjectPath.getParent() == null ? baseDirectoryName - : dataObjectPath.getParent().toString(); + Path urlPath = Paths.get(path.trim()); + Path parentPath = urlPath.getParent(); + if (parentPath == null) { + // root container + return null; + } + // create the data object meta-data files Container parentContainer = (Container) cdmiObjectDao.getCdmiObjectByPath(parentPath.toString()); - DataObject dataObject = new DataObject(objectName, parentUri, parentContainer.getObjectId()); + DataObject dataObject = new DataObject(urlPath.getFileName().toString(), parentPath.toString(), + parentContainer.getObjectId()); if (parentContainer.getChildren() == null) { parentContainer.setChildren(new JSONArray()); } + parentContainer.getChildren().put(dataObject.getObjectName()); String childrenRange = CdmiObject.getChildrenRange(parentContainer.getChildren()); parentContainer.setChildrenrange(childrenRange); dataObject.setCompletionStatus("Complete"); dataObject.setMetadata(dataObjectRequest.getMetadata()); + // dataObject.setCapabilitiesUri(dataObjectRequest.getCapabilitiesUri()); - cdmiObjectDao.createCdmiObject(dataObject, dataObjectPath.toString()); + dataObject = (DataObject) cdmiObjectDao.createCdmiObject(dataObject, urlPath.toString()); cdmiObjectDao.updateCdmiObject(parentContainer); return dataObject; @@ -139,39 +139,32 @@ public DataObject createById(String objectId, DataObject dataObj) { @Override public DataObject deleteByPath(String path) { - if (path == null) { - return null; - } - LOG.debug("delete data object {}", path.trim()); + DataObject dataObject = null; + try { + log.debug("delete data object {}", path.trim()); - final Path objectPath = Paths.get(baseDirectoryName.trim(), path.trim()); - DataObject dataObject = (DataObject) cdmiObjectDao.getCdmiObjectByPath(objectPath.toString()); + dataObject = (DataObject) cdmiObjectDao.getCdmiObjectByPath(path.trim()); - if (dataObject != null) { - try { - LOG.debug("delete file {}", objectPath.toString()); + if (dataObject != null) { + final Path objectPath = Paths.get(baseDirectoryName.trim(), path.trim()); + + log.debug("delete file {}", objectPath.toString()); Files.delete(objectPath); cdmiObjectDao.deleteCdmiObject(dataObject.getObjectId()); - cdmiObjectDao.deleteCdmiObjectByPath(objectPath.toString()); + cdmiObjectDao.deleteCdmiObjectByPath(path.trim()); // removeChild(objectPath.getFileName().toString(), objectPath.getParent().toString()); - - } catch (Exception e) { - LOG.error("ERROR: {}", e.getMessage()); } + } catch (Exception e) { + log.error("ERROR: {}", e.getMessage()); } return dataObject; } @Override public DataObject findByPath(String path) { - if (path == null) { - return null; - } - - Path objectPath = Paths.get(baseDirectoryName.trim(), path.trim()); - DataObject dataObject = (DataObject) cdmiObjectDao.getCdmiObjectByPath(objectPath.toString()); + DataObject dataObject = (DataObject) cdmiObjectDao.getCdmiObjectByPath(path.trim()); // if (dataObject != null) { // dataObject.setValue(new String(getDataObjectContent(path))); // } @@ -202,21 +195,17 @@ private byte[] getDataObjectContent(String path) { @Override public DataObject updateContent(String path, byte[] content) { - if (path == null) { - return null; - } - - Path objectPath = Paths.get(baseDirectoryName.trim(), path.trim()); - DataObject dataObject = (DataObject) cdmiObjectDao.getCdmiObjectByPath(objectPath.toString()); + DataObject dataObject = (DataObject) cdmiObjectDao.getCdmiObjectByPath(path.trim()); if (dataObject != null) { try { + Path objectPath = Paths.get(baseDirectoryName.trim(), path.trim()); Files.write(objectPath, content, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING); - LOG.debug("writing value to data object"); + log.debug("writing value to data object"); } catch (IOException ex) { // ex.printStackTrace(); - LOG.error("ERROR {}", ex.getMessage()); + log.error("ERROR {}", ex.getMessage()); } } return dataObject; diff --git a/src/main/java/org/snia/cdmiserver/dao/filesystem/DomainDaoImpl.java b/src/main/java/org/snia/cdmiserver/dao/filesystem/DomainDaoImpl.java index b0da517..018f744 100644 --- a/src/main/java/org/snia/cdmiserver/dao/filesystem/DomainDaoImpl.java +++ b/src/main/java/org/snia/cdmiserver/dao/filesystem/DomainDaoImpl.java @@ -16,9 +16,6 @@ import org.snia.cdmiserver.model.CdmiObject; import org.snia.cdmiserver.model.Domain; -import java.nio.file.Path; -import java.nio.file.Paths; - public class DomainDaoImpl implements DomainDao { private static final Logger LOG = LoggerFactory.getLogger(DomainDaoImpl.class); @@ -63,9 +60,8 @@ public CdmiObject findByObjectId(String objectId) { @Override public CdmiObject findByPath(String path) { - Path domainPath = Paths.get(baseDirectoryName.trim(), path.trim()); LOG.debug("path is {}", path); - return (Domain) cdmiObjectDao.getCdmiObjectByPath(domainPath.toString()); + return (Domain) cdmiObjectDao.getCdmiObjectByPath(path.trim()); } @Override diff --git a/src/main/resources/application-test.properties b/src/main/resources/application-test.properties new file mode 100644 index 0000000..af5a2b4 --- /dev/null +++ b/src/main/resources/application-test.properties @@ -0,0 +1,9 @@ +# CDMI Data +# the root container/directory +cdmi.data.baseDirectory: test +# the prefix for all objectID files by path +cdmi.data.objectIdPrefix: .cdmi_ + +# CDMI QoS +# the type name of the storage back-end to load +cdmi.qos.backend.type: dummy_filesystem \ No newline at end of file diff --git a/src/test/java/edu/kit/scc/TestSuite.java b/src/test/java/edu/kit/scc/TestSuite.java deleted file mode 100644 index e39c049..0000000 --- a/src/test/java/edu/kit/scc/TestSuite.java +++ /dev/null @@ -1,19 +0,0 @@ -/* - * Copyright 2016 Karlsruhe Institute of Technology (KIT) - * - * 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 - */ - -package edu.kit.scc; - -import org.junit.runner.RunWith; -import org.junit.runners.Suite; - -@RunWith(Suite.class) -@Suite.SuiteClasses({UtilsTest.class}) -public class TestSuite { - -} diff --git a/src/test/java/edu/kit/scc/cdmi/filesystem/CapabilityFilesystemTest.java b/src/test/java/edu/kit/scc/cdmi/filesystem/CapabilityFilesystemTest.java index 6cd126c..08d446e 100644 --- a/src/test/java/edu/kit/scc/cdmi/filesystem/CapabilityFilesystemTest.java +++ b/src/test/java/edu/kit/scc/cdmi/filesystem/CapabilityFilesystemTest.java @@ -14,7 +14,6 @@ import edu.kit.scc.CdmiServerApplication; import org.json.JSONObject; -import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; @@ -36,7 +35,7 @@ @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = CdmiServerApplication.class) -@ActiveProfiles("filesystem") +@ActiveProfiles("test") public class CapabilityFilesystemTest { @Value("${cdmi.data.objectIdPrefix}") diff --git a/src/test/java/edu/kit/scc/cdmi/filesystem/CdmiObjectFilesystemTest.java b/src/test/java/edu/kit/scc/cdmi/filesystem/CdmiObjectFilesystemTest.java index cfe2c21..c01b992 100644 --- a/src/test/java/edu/kit/scc/cdmi/filesystem/CdmiObjectFilesystemTest.java +++ b/src/test/java/edu/kit/scc/cdmi/filesystem/CdmiObjectFilesystemTest.java @@ -15,7 +15,6 @@ import edu.kit.scc.CdmiServerApplication; -import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; @@ -39,7 +38,7 @@ @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = CdmiServerApplication.class) -@ActiveProfiles("filesystem") +@ActiveProfiles("test") public class CdmiObjectFilesystemTest { @Value("${cdmi.data.objectIdPrefix}") @@ -115,8 +114,7 @@ public void testUpdateCdmiObjectByPath() { CdmiObject updatedObject = new CdmiObject("myId"); - updatedObject = cdmiObjectDao.updateCdmiObject(updatedObject, - Paths.get(baseDirectoryName, objectName).toString()); + updatedObject = cdmiObjectDao.updateCdmiObject(updatedObject, objectName); assertNotNull(updatedObject); assertTrue(updatedObject.getObjectId().equals("myId")); diff --git a/src/test/java/edu/kit/scc/cdmi/filesystem/ContainerFilesystemTest.java b/src/test/java/edu/kit/scc/cdmi/filesystem/ContainerFilesystemTest.java index e6c8425..934bb8c 100644 --- a/src/test/java/edu/kit/scc/cdmi/filesystem/ContainerFilesystemTest.java +++ b/src/test/java/edu/kit/scc/cdmi/filesystem/ContainerFilesystemTest.java @@ -9,6 +9,7 @@ package edu.kit.scc.cdmi.filesystem; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; @@ -17,7 +18,6 @@ import edu.kit.scc.CdmiServerApplication; import org.json.JSONObject; -import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; @@ -41,7 +41,7 @@ @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = CdmiServerApplication.class) -@ActiveProfiles("filesystem") +@ActiveProfiles("test") public class ContainerFilesystemTest { @Autowired @@ -83,23 +83,23 @@ public void testCreateContainer() { } @Test - public void testCreateContainerFailIfExists() { + public void testCreateContainerIfExists() { String containerName = "existingContainer"; Container containerRequest = Container.fromJson(new JSONObject("{}")); - Container container = + Container container1 = containerDao.createByPath(Paths.get("/", containerName).toString(), containerRequest); - assertNotNull(container); + assertNotNull(container1); - container = + Container container2 = containerDao.createByPath(Paths.get("/", containerName).toString(), containerRequest); - assertTrue(container == null); + assertEquals(container1.toString(), container2.toString()); } @Test - public void testCreateContainerFails() { + public void testCreateRootContainer() { Container containerRequest = Container.fromJson(new JSONObject("{}")); Container container = containerDao.createByPath(null, containerRequest); @@ -107,7 +107,7 @@ public void testCreateContainerFails() { container = containerDao.createByPath("/", null); - assertTrue(container == null); + assertNotNull(container); } @Test diff --git a/src/test/java/edu/kit/scc/cdmi/filesystem/DataObjectFilesystemTest.java b/src/test/java/edu/kit/scc/cdmi/filesystem/DataObjectFilesystemTest.java index 3c6dc1d..fd42f5b 100644 --- a/src/test/java/edu/kit/scc/cdmi/filesystem/DataObjectFilesystemTest.java +++ b/src/test/java/edu/kit/scc/cdmi/filesystem/DataObjectFilesystemTest.java @@ -9,14 +9,13 @@ package edu.kit.scc.cdmi.filesystem; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import edu.kit.scc.CdmiServerApplication; import org.json.JSONObject; -import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; @@ -40,7 +39,7 @@ @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = CdmiServerApplication.class) -@ActiveProfiles("filesystem") +@ActiveProfiles("test") public class DataObjectFilesystemTest { static String baseDirectoryName; @@ -169,20 +168,20 @@ public void testFindById() { } @Test - public void testCreateDataObjectFailsIfAlreadyExists() { + public void testCreateDataObjectIfAlreadyExists() { String dataObjectName = "existing"; DataObject dataObjectRequest = DataObject.fromJson(new JSONObject("{}")); - DataObject dataObject = + DataObject dataObject1 = dataObjectDao.createByPath(Paths.get("/", dataObjectName).toString(), dataObjectRequest); - assertNotNull(dataObject); + assertNotNull(dataObject1); - dataObject = + DataObject dataObject2 = dataObjectDao.createByPath(Paths.get("/", dataObjectName).toString(), dataObjectRequest); - assertNull(dataObject); + assertEquals(dataObject1.toString(), dataObject2.toString()); } // @AfterClass diff --git a/src/test/java/edu/kit/scc/cdmi/filesystem/FileSystemTestSuite.java b/src/test/java/edu/kit/scc/cdmi/filesystem/FileSystemTestSuite.java index ec2638c..f6d2d69 100644 --- a/src/test/java/edu/kit/scc/cdmi/filesystem/FileSystemTestSuite.java +++ b/src/test/java/edu/kit/scc/cdmi/filesystem/FileSystemTestSuite.java @@ -31,7 +31,7 @@ public class FileSystemTestSuite { @AfterClass public static void destroy() throws IOException { Properties props = new Properties(); - InputStream is = ClassLoader.getSystemResourceAsStream("application.properties"); + InputStream is = ClassLoader.getSystemResourceAsStream("application-test.properties"); try { props.load(is); } catch (IOException e) { diff --git a/src/test/java/edu/kit/scc/cdmi/rest/AuthorizationTest.java b/src/test/java/edu/kit/scc/cdmi/rest/AuthorizationTest.java index aa80b35..b6cb2e7 100644 --- a/src/test/java/edu/kit/scc/cdmi/rest/AuthorizationTest.java +++ b/src/test/java/edu/kit/scc/cdmi/rest/AuthorizationTest.java @@ -21,12 +21,14 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.nio.charset.StandardCharsets; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = CdmiServerApplication.class) +@ActiveProfiles("test") public class AuthorizationTest { @Autowired diff --git a/src/test/java/edu/kit/scc/cdmi/rest/CapabilitiesTest.java b/src/test/java/edu/kit/scc/cdmi/rest/CapabilitiesTest.java index 3a75ae9..852c7b1 100644 --- a/src/test/java/edu/kit/scc/cdmi/rest/CapabilitiesTest.java +++ b/src/test/java/edu/kit/scc/cdmi/rest/CapabilitiesTest.java @@ -22,6 +22,7 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.WebIntegrationTest; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.jayway.restassured.RestAssured; @@ -30,6 +31,7 @@ @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = CdmiServerApplication.class) @WebIntegrationTest +@ActiveProfiles("test") public class CapabilitiesTest { private static final Logger log = LoggerFactory.getLogger(CapabilitiesTest.class); diff --git a/src/test/java/edu/kit/scc/cdmi/rest/CdmiObjectTest.java b/src/test/java/edu/kit/scc/cdmi/rest/CdmiObjectTest.java index 686a66f..835226c 100644 --- a/src/test/java/edu/kit/scc/cdmi/rest/CdmiObjectTest.java +++ b/src/test/java/edu/kit/scc/cdmi/rest/CdmiObjectTest.java @@ -22,6 +22,7 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.WebIntegrationTest; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.jayway.restassured.RestAssured; @@ -30,6 +31,7 @@ @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = CdmiServerApplication.class) @WebIntegrationTest +@ActiveProfiles("test") public class CdmiObjectTest { private static final Logger log = LoggerFactory.getLogger(CdmiObjectTest.class); diff --git a/src/test/java/edu/kit/scc/cdmi/rest/ContainerTest.java b/src/test/java/edu/kit/scc/cdmi/rest/ContainerTest.java index e404f40..d4125de 100644 --- a/src/test/java/edu/kit/scc/cdmi/rest/ContainerTest.java +++ b/src/test/java/edu/kit/scc/cdmi/rest/ContainerTest.java @@ -22,6 +22,7 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.WebIntegrationTest; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.jayway.restassured.RestAssured; @@ -30,6 +31,7 @@ @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = CdmiServerApplication.class) @WebIntegrationTest +@ActiveProfiles("test") public class ContainerTest { private static final Logger log = LoggerFactory.getLogger(ContainerTest.class); @@ -84,7 +86,7 @@ public void testPutNewContainer() { @Test public void testGetRootContainer() { String authString = Base64.encodeBase64String((restUser + ":" + restPassword).getBytes()); - + log.debug(authString); Response response = given().header("Authorization", "Basic " + authString).and() .header("Content-Type", "application/cdmi-object").when().get("/").then() .statusCode(org.apache.http.HttpStatus.SC_OK).extract().response(); diff --git a/src/test/java/edu/kit/scc/cdmi/rest/DataObjectTest.java b/src/test/java/edu/kit/scc/cdmi/rest/DataObjectTest.java index 3f87b2b..7d7cc55 100644 --- a/src/test/java/edu/kit/scc/cdmi/rest/DataObjectTest.java +++ b/src/test/java/edu/kit/scc/cdmi/rest/DataObjectTest.java @@ -22,6 +22,7 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.WebIntegrationTest; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.jayway.restassured.RestAssured; @@ -30,6 +31,7 @@ @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = CdmiServerApplication.class) @WebIntegrationTest +@ActiveProfiles("test") public class DataObjectTest { private static final Logger log = LoggerFactory.getLogger(DataObjectTest.class); diff --git a/src/test/java/edu/kit/scc/cdmi/rest/DomainTest.java b/src/test/java/edu/kit/scc/cdmi/rest/DomainTest.java index 6112eb3..7b47f57 100644 --- a/src/test/java/edu/kit/scc/cdmi/rest/DomainTest.java +++ b/src/test/java/edu/kit/scc/cdmi/rest/DomainTest.java @@ -13,6 +13,7 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.WebIntegrationTest; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.jayway.restassured.RestAssured; @@ -21,6 +22,7 @@ @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = CdmiServerApplication.class) @WebIntegrationTest +@ActiveProfiles("test") public class DomainTest { private static final Logger log = LoggerFactory.getLogger(DomainTest.class); diff --git a/src/test/java/edu/kit/scc/cdmi/rest/FilterJsonTest.java b/src/test/java/edu/kit/scc/cdmi/rest/FilterJsonTest.java index 24c5adf..d149f64 100644 --- a/src/test/java/edu/kit/scc/cdmi/rest/FilterJsonTest.java +++ b/src/test/java/edu/kit/scc/cdmi/rest/FilterJsonTest.java @@ -23,10 +23,12 @@ import org.snia.cdmiserver.model.Capability; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = CdmiServerApplication.class) +@ActiveProfiles("test") public class FilterJsonTest { private static final Logger log = LoggerFactory.getLogger(FilterJsonTest.class); diff --git a/src/test/java/edu/kit/scc/cdmi/rest/RestTestSuite.java b/src/test/java/edu/kit/scc/cdmi/rest/RestTestSuite.java index 34c9279..291b6ff 100644 --- a/src/test/java/edu/kit/scc/cdmi/rest/RestTestSuite.java +++ b/src/test/java/edu/kit/scc/cdmi/rest/RestTestSuite.java @@ -31,7 +31,7 @@ public class RestTestSuite { @AfterClass public static void destroy() throws IOException { Properties props = new Properties(); - InputStream is = ClassLoader.getSystemResourceAsStream("application.properties"); + InputStream is = ClassLoader.getSystemResourceAsStream("application-test.properties"); try { props.load(is); } catch (IOException e) { diff --git a/src/test/java/edu/kit/scc/http/client/HttpClientTest.java b/src/test/java/edu/kit/scc/http/client/HttpClientTest.java index 6ee9a20..d5a4cbc 100644 --- a/src/test/java/edu/kit/scc/http/client/HttpClientTest.java +++ b/src/test/java/edu/kit/scc/http/client/HttpClientTest.java @@ -22,12 +22,15 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.nio.charset.StandardCharsets; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = CdmiServerApplication.class) +@ActiveProfiles("test") +@SuppressWarnings("unused") public class HttpClientTest { @Autowired diff --git a/src/test/java/edu/kit/scc/http/client/HttpTestSuite.java b/src/test/java/edu/kit/scc/http/client/HttpTestSuite.java index 0909326..b1bc6e5 100644 --- a/src/test/java/edu/kit/scc/http/client/HttpTestSuite.java +++ b/src/test/java/edu/kit/scc/http/client/HttpTestSuite.java @@ -9,11 +9,64 @@ package edu.kit.scc.http.client; +import org.junit.AfterClass; import org.junit.runner.RunWith; import org.junit.runners.Suite; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.Properties; @RunWith(Suite.class) @Suite.SuiteClasses({HttpClientTest.class}) public class HttpTestSuite { + private static final Logger log = LoggerFactory.getLogger(HttpTestSuite.class); + + @AfterClass + public static void destroy() throws IOException { + Properties props = new Properties(); + InputStream is = ClassLoader.getSystemResourceAsStream("application-test.properties"); + try { + props.load(is); + } catch (IOException e) { + e.printStackTrace(); + } + + String baseDirectoryName = props.getProperty("cdmi.data.baseDirectory"); + + Path start = Paths.get(baseDirectoryName); + + try { + Files.walkFileTree(start, new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + Files.delete(file); + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException ex) throws IOException { + if (ex == null) { + Files.delete(dir); + return FileVisitResult.CONTINUE; + } else { + // directory iteration failed + throw ex; + } + } + }); + } catch (Exception ex) { + log.warn("failed to delete test directory {} with error {}", baseDirectoryName, + ex.toString()); + } + } } diff --git a/src/test/java/edu/kit/scc/test/TestSuite.java b/src/test/java/edu/kit/scc/test/TestSuite.java new file mode 100644 index 0000000..684cc94 --- /dev/null +++ b/src/test/java/edu/kit/scc/test/TestSuite.java @@ -0,0 +1,70 @@ +package edu.kit.scc.test; + +import edu.kit.scc.cdmi.filesystem.CapabilityFilesystemTest; +import edu.kit.scc.cdmi.filesystem.CdmiObjectFilesystemTest; +import edu.kit.scc.cdmi.filesystem.ContainerFilesystemTest; +import edu.kit.scc.cdmi.filesystem.DataObjectFilesystemTest; +import edu.kit.scc.cdmi.rest.AuthorizationTest; +import edu.kit.scc.cdmi.rest.CapabilitiesTest; +import edu.kit.scc.cdmi.rest.CdmiObjectTest; +import edu.kit.scc.cdmi.rest.ContainerTest; +import edu.kit.scc.cdmi.rest.DataObjectTest; +import edu.kit.scc.cdmi.rest.DomainTest; +import edu.kit.scc.cdmi.rest.FilterJsonTest; +import edu.kit.scc.http.client.HttpClientTest; +import edu.kit.scc.utils.UtilsTest; + +import org.junit.AfterClass; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.Properties; + +@RunWith(Suite.class) +@Suite.SuiteClasses({CapabilityFilesystemTest.class, CdmiObjectFilesystemTest.class, + ContainerFilesystemTest.class, DataObjectFilesystemTest.class, AuthorizationTest.class, + CapabilitiesTest.class, CdmiObjectTest.class, ContainerTest.class, DataObjectTest.class, + DomainTest.class, FilterJsonTest.class, HttpClientTest.class, UtilsTest.class}) +public class TestSuite { + + @AfterClass + public static void destroy() throws IOException { + Properties props = new Properties(); + InputStream is = ClassLoader.getSystemResourceAsStream("application-test.properties"); + try { + props.load(is); + } catch (IOException e) { + e.printStackTrace(); + } + + String baseDirectoryName = props.getProperty("cdmi.data.baseDirectory"); + + Path start = Paths.get(baseDirectoryName); + Files.walkFileTree(start, new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + Files.delete(file); + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException ex) throws IOException { + if (ex == null) { + Files.delete(dir); + return FileVisitResult.CONTINUE; + } else { + // directory iteration failed + throw ex; + } + } + }); + } +} diff --git a/src/test/java/edu/kit/scc/UtilsTest.java b/src/test/java/edu/kit/scc/utils/UtilsTest.java similarity index 85% rename from src/test/java/edu/kit/scc/UtilsTest.java rename to src/test/java/edu/kit/scc/utils/UtilsTest.java index c1b9aba..175cb5d 100644 --- a/src/test/java/edu/kit/scc/UtilsTest.java +++ b/src/test/java/edu/kit/scc/utils/UtilsTest.java @@ -7,17 +7,22 @@ * http://www.apache.org/licenses/LICENSE-2.0 */ -package edu.kit.scc; +package edu.kit.scc.utils; import static org.junit.Assert.assertTrue; +import edu.kit.scc.CdmiServerApplication; +import edu.kit.scc.Utils; + import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.test.context.ActiveProfiles; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = CdmiServerApplication.class) +@ActiveProfiles("test") public class UtilsTest { @Test diff --git a/src/test/java/edu/kit/scc/utils/UtilsTestSuite.java b/src/test/java/edu/kit/scc/utils/UtilsTestSuite.java new file mode 100644 index 0000000..bba8805 --- /dev/null +++ b/src/test/java/edu/kit/scc/utils/UtilsTestSuite.java @@ -0,0 +1,62 @@ +/* + * Copyright 2016 Karlsruhe Institute of Technology (KIT) + * + * 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 + */ + +package edu.kit.scc.utils; + +import org.junit.AfterClass; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.Properties; + +@RunWith(Suite.class) +@Suite.SuiteClasses({UtilsTest.class}) +public class UtilsTestSuite { + + @AfterClass + public static void destroy() throws IOException { + Properties props = new Properties(); + InputStream is = ClassLoader.getSystemResourceAsStream("application-test.properties"); + try { + props.load(is); + } catch (IOException e) { + e.printStackTrace(); + } + + String baseDirectoryName = props.getProperty("cdmi.data.baseDirectory"); + + Path start = Paths.get(baseDirectoryName); + Files.walkFileTree(start, new SimpleFileVisitor() { + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + Files.delete(file); + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult postVisitDirectory(Path dir, IOException ex) throws IOException { + if (ex == null) { + Files.delete(dir); + return FileVisitResult.CONTINUE; + } else { + // directory iteration failed + throw ex; + } + } + }); + } +}