diff --git a/.dockerignore b/.dockerignore
new file mode 100644
index 0000000..3cf5f0e
--- /dev/null
+++ b/.dockerignore
@@ -0,0 +1,3 @@
+demo
+.git
+tests
diff --git a/Jenkinsfile b/Jenkinsfile
index f39c25c..9d177f9 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -34,9 +34,10 @@ pipeline {
             steps {
                 script {
                     try {
-                        sh './download-midpoint &> debug'
-                        sh 'bin/rebuild.sh &>> debug'
-                        sh 'echo Build output ; cat debug'
+			sh '(docker image ls ; echo Destroying ; bin/destroy.sh ; docker image ls) 2>&1 | tee debug'	// temporary
+                        sh './download-midpoint 2>&1 | tee -a debug'
+                        sh 'bin/rebuild.sh 2>&1 | tee -a debug'
+                        //sh 'echo Build output ; cat debug'
                     } catch (error) {
                         def error_details = readFile('./debug')
                         def message = "BUILD ERROR: There was a problem building ${imagename}:${tag}. \n\n ${error_details}"
@@ -50,9 +51,9 @@ pipeline {
             steps {
                 script {
                     try {
-                        sh 'bin/test.sh &> debug'
-                        sh '(cd demo/simple ; bats tests ) &>> debug'
-                        sh 'echo Test output ; cat debug'
+                        sh 'bin/test.sh 2>&1 | tee debug'
+                        sh '(cd demo/simple ; bats tests ) 2>&1 | tee -a debug'
+                        // sh 'echo Test output ; cat debug'
                     } catch (error) {
                         def error_details = readFile('./debug')
                         def message = "BUILD ERROR: There was a problem testing ${imagename}:${tag}. \n\n ${error_details}"
@@ -62,27 +63,6 @@ pipeline {
                 }
             }
         }
-/*
-        stage ('Test2') {
-            steps {
-                script {
-                    try {
-                      try {
-                         sh 'docker pull tier/mariadb:mariadb10'		// temporary
-                         sh 'env NOCOLOR=true ./test.sh'
-                      } finally {
-                         sh './cleanup.sh'
-                      }
-                    } catch (error) {
-                        def error_details = readFile('./debug')
-                        def message = "BUILD ERROR: There was a problem building ${imagename}:${tag}. \n\n ${error_details}"
-                        sh "rm -f ./debug"
-                        handleError(message)
-                    }
-                }
-            }
-        }
-*/
         stage ('Push') {
             steps {
                 script {
diff --git a/build.sh b/build.sh
index 37397bc..b59e4ed 100755
--- a/build.sh
+++ b/build.sh
@@ -14,8 +14,16 @@ function normalize_path()
 }
 
 cd "$(dirname "$0")"
-./download-midpoint
-docker build --tag tier/midpoint:latest midpoint-server
+SKIP_DOWNLOAD=0
+while getopts "nh?" opt; do
+    case $opt in
+    n) SKIP_DOWNLOAD=1 ;;
+    h | ?) echo "Options: -n skip download" ; exit 0 ;;
+    *) echo "Unknown option: $opt" ; exit 1 ;;
+    esac
+done
+if [ "$SKIP_DOWNLOAD" = "0" ]; then ./download-midpoint; fi
+docker build --tag tier/midpoint:latest .
 echo "---------------------------------------------------------------------------------------"
 echo "The midPoint containers were successfully built. To start them, execute the following:"
 echo ""
diff --git a/demo/simple/.env b/demo/simple/.env
index ce20478..2c8a3e3 100644
--- a/demo/simple/.env
+++ b/demo/simple/.env
@@ -1,3 +1,4 @@
+# These parameters can be overridden by setting environment variables before calling docker-compose up
 AUTHENTICATION=internal
 ENV=demo
 USERTOKEN=
@@ -7,8 +8,4 @@ REPO_HOST=midpoint-data
 REPO_PORT=default
 REPO_DATABASE=registry
 REPO_USER=registry_user
-REPO_PASSWORD_FILE=/run/secrets/m_database_password.txt
-KEYSTORE_PASSWORD_FILE=/run/secrets/m_keystore_password.txt
 MEM=2048m
-LOGOUT_URL=https://localhost:8443/Shibboleth.sso/Logout
-SSO_HEADER=uid
diff --git a/demo/simple/docker-compose.yml b/demo/simple/docker-compose.yml
index 0c850bd..924f2e2 100644
--- a/demo/simple/docker-compose.yml
+++ b/demo/simple/docker-compose.yml
@@ -27,11 +27,7 @@ services:
      - REPO_PORT
      - REPO_DATABASE
      - REPO_USER
-     - REPO_PASSWORD_FILE
-     - KEYSTORE_PASSWORD_FILE
      - MEM
-     - SSO_HEADER
-     - LOGOUT_URL
      - TIER_BEACON_OPT_OUT
     networks:
      - back
diff --git a/demo/simple/tests/main.bats b/demo/simple/tests/main.bats
index eec4945..530c53e 100755
--- a/demo/simple/tests/main.bats
+++ b/demo/simple/tests/main.bats
@@ -9,6 +9,44 @@ load ../../../library
     wait_for_midpoint_start simple_midpoint-server_1
 }
 
+@test "010 Check health" {
+    check_health
+}
+
+@test "100 Get 'administrator'" {
+    check_health
+    get_and_check_object users 00000000-0000-0000-0000-000000000002 administrator
+}
+
+@test "110 And and get 'test110'" {
+    check_health
+    echo "<user><name>test110</name></user>" >/tmp/test110.xml
+    add_object users /tmp/test110.xml
+    rm /tmp/test110.xml
+    search_and_check_object users test110
+}
+
+@test "300 Check repository preserved between restarts" {
+    check_health
+
+    echo "Creating user test300 and checking its existence"
+    echo "<user><name>test300</name></user>" >/tmp/test300.xml
+    add_object users /tmp/test300.xml
+    rm /tmp/test300.xml
+    search_and_check_object users test300
+
+    echo "Bringing the containers down"
+    docker-compose down
+
+    echo "Re-creating the containers"
+    docker-compose up --no-start
+    docker-compose start
+    wait_for_midpoint_start simple_midpoint-server_1
+
+    echo "Searching for the user again"
+    search_and_check_object users test300
+}
+
 @test "999 Clean up" {
     docker-compose down -v
 }
diff --git a/library.bash b/library.bash
index 1cfbdaf..e605ab5 100644
--- a/library.bash
+++ b/library.bash
@@ -1,6 +1,12 @@
 #!/bin/bash
 
+#
+# Contains common functions usable for midPoint system tests
+#
+
+# Waits until midPoint starts
 function wait_for_midpoint_start () {
+    CONTAINER_NAME=$1
     ATTEMPT=0
     MAX_ATTEMPTS=20
     DELAY=10
@@ -10,9 +16,96 @@ function wait_for_midpoint_start () {
         echo "Waiting $DELAY seconds for midPoint to start (attempt $ATTEMPT) ..."
         sleep $DELAY
         docker ps
-        ( docker logs $1 2>&1 | grep "INFO (com.evolveum.midpoint.web.boot.MidPointSpringApplication): Started MidPointSpringApplication in" ) && return 0
+        ( docker logs $CONTAINER_NAME 2>&1 | grep "INFO (com.evolveum.midpoint.web.boot.MidPointSpringApplication): Started MidPointSpringApplication in" ) && return 0
     done
 
-    echo midPoint did not start in $(( $MAX_ATTEMPTS * $DELAY )) seconds
+    echo midPoint did not start in $(( $MAX_ATTEMPTS * $DELAY )) seconds in $CONTAINER_NAME
     return 1
 }
+
+# Checks the health of midPoint server
+function check_health () {
+    echo Checking health...
+    (set -o pipefail ; curl -k -f https://localhost:8443/midpoint/actuator/health | tr -d '[:space:]' | grep -q "\"status\":\"UP\"")
+    status=$?
+    if [ $status -ne 0 ]; then
+        echo Error: $status
+        docker ps
+        return 1
+    else
+        echo OK
+        return 0
+    fi
+}
+
+# Retrieves XML object and checks if the name matches
+function get_and_check_object () {
+    TYPE=$1
+    OID=$2
+    NAME=$3
+    TMPFILE=$(mktemp /tmp/get.XXXXXX)
+    echo tmp file is $TMPFILE
+    curl -k --user administrator:5ecr3t -H "Content-Type: application/xml" -X GET "https://localhost:8443/midpoint/ws/rest/$TYPE/$OID" >$TMPFILE || (rm $TMPFILE ; return 1)
+    if (grep -q "<name>$NAME</name>" <$TMPFILE); then
+        echo "Object $TYPE/$OID '$NAME' is OK"
+        rm $TMPFILE
+        return 0
+    else
+        echo "Object $TYPE/$OID '$NAME' was not found or not retrieved correctly:"
+        cat $TMPFILE
+        rm $TMPFILE
+        return 1
+    fi
+}
+
+# Adds object from a given file
+# TODO Returns the OID in OID variable
+# it can be found in the following HTTP reader returned: Location: "https://localhost:8443/midpoint/ws/rest/users/85e62669-d36b-41ce-b4f1-1ffdd9f66262"
+function add_object () {
+    TYPE=$1
+    FILE=$2
+    echo "Adding to $TYPE from $FILE..."
+    curl -k --user administrator:5ecr3t -H "Content-Type: application/xml" -X POST "https://localhost:8443/midpoint/ws/rest/$TYPE" -d @$FILE || return 1
+    #TODO check the returned XML
+    return 0
+}
+
+# Tries to find an object with a given name
+# Results of the search are in the $SEARCH_RESULT_FILE
+# TODO check if the result is valid (i.e. not an error) - return 1 if invalid, otherwise return 0 ("no objects" is considered OK here)
+function search_objects_by_name () {
+    TYPE=$1
+    NAME=$2
+    TMPFILE=$(mktemp /tmp/search.XXXXXX)
+
+    curl -k --user administrator:5ecr3t -H "Content-Type: application/xml" -X POST "https://localhost:8443/midpoint/ws/rest/$TYPE/search" -d @- << EOF >$TMPFILE || (rm $TMPFILE ; return 1)
+<q:query xmlns:q="http://prism.evolveum.com/xml/ns/public/query-3">
+    <q:filter>
+        <q:equal>
+            <q:path>name</q:path>
+            <q:value>$NAME</q:value>
+        </q:equal>
+    </q:filter>
+</q:query>
+EOF
+    SEARCH_RESULT_FILE=$TMPFILE
+    # TODO check validity of the file
+    return 0
+}
+
+# Searches for object with a given name and verifies it was found
+function search_and_check_object () {
+    TYPE=$1
+    NAME=$2
+    search_objects_by_name $TYPE $NAME || return 1
+    if (grep -q "<name>$NAME</name>" <$SEARCH_RESULT_FILE); then
+        echo "Object $TYPE/'$NAME' is OK"
+        rm $SEARCH_RESULT_FILE
+        return 0
+    else
+        echo "Object $TYPE/'$NAME' was not found or not retrieved correctly:"
+        cat $SEARCH_RESULT_FILE
+        rm $SEARCH_RESULT_FILE
+        return 1
+    fi
+}