diff --git a/run_tests.sh b/run_tests.sh new file mode 100755 index 0000000..e747818 --- /dev/null +++ b/run_tests.sh @@ -0,0 +1,147 @@ +#!/bin/bash + +# Tests for the REST Api +# Copright 2016 (c) Marcel Kapfer (mmk2410) +# Licensed under AGPL + +TESTS_AMOUNT=0 +TESTS_PASSED=0 + +ERROR_LOG="./error.log" + +CONTENT_TYPE="Content-Type: application/json" + +BASEURL="http://localhost:8080/titamaApi/v1" + +# Delete old log file if it exists +if [ -f $ERROR_LOG ]; then + rm $ERROR_LOG +fi + +# Test for get requests +function test_get { + if [ -z "$1" ]; then + echo "[ERROR] No URL give." + exit 1 + elif [ -z "$2" ]; then + echo "[ERROR] No expected value given." + exit 1 + fi + + ((TESTS_AMOUNT++)) + + echo "Running test $TESTS_AMOUNT: $1" + + response=$(curl "$1") + + if [ "$2" == "$response" ]; then + echo "Test $TESTS_AMOUNT passed." + ((TESTS_PASSED++)) + else + echo "Test $TESTS_AMOUNT failed." + echo "Test $TESTS_AMOUNT failed." >> $ERROR_LOG + echo "$response" >> $ERROR_LOG + fi +} + +# Test for post requests +function test_post { + if [ -z "$1" ]; then + echo "[ERROR] No URL give." + exit 1 + elif [ -z "$2" ]; then + echo "[ERROR] No post data given." + exit 1 + elif [ -z "$3" ]; then + echo "[ERROR] No expected value given." + exit 1 + fi + + ((TESTS_AMOUNT++)) + + echo "Running test $TESTS_AMOUNT: $1" + + response=$(curl -d "$2" -H "$CONTENT_TYPE" "$1") + + if [ "$3" == "$response" ]; then + echo "Test $TESTS_AMOUNT passed." + ((TESTS_PASSED++)) + else + echo "Test $TESTS_AMOUNT failed." + echo "Test $TESTS_AMOUNT failed." >> $ERROR_LOG + echo "$response" >> $ERROR_LOG + fi +} + +# Test for delete requests +function test_delete { + if [ -z "$1" ]; then + echo "[ERROR] No URL give." + exit 1 + elif [ -z "$2" ]; then + echo "[ERROR] No expected value given." + exit 1 + fi + + ((TESTS_AMOUNT++)) + + echo "Running test $TESTS_AMOUNT: $1" + + response=$(curl -X DELETE "$1") + + if [ "$2" == "$response" ]; then + echo "Test $TESTS_AMOUNT passed." + ((TESTS_PASSED++)) + else + echo "Test $TESTS_AMOUNT failed." + echo "Test $TESTS_AMOUNT failed." >> $ERROR_LOG + echo "$response" >> $ERROR_LOG + fi +} + +# Test for put requests +function test_put { + if [ -z "$1" ]; then + echo "[ERROR] No URL give." + exit 1 + elif [ -z "$2" ]; then + echo "[ERROR] No post data given." + exit 1 + elif [ -z "$3" ]; then + echo "[ERROR] No expected value given." + exit 1 + fi + + ((TESTS_AMOUNT++)) + + echo "Running test $TESTS_AMOUNT: $1" + + response=$(curl -d "$2" -H "$CONTENT_TYPE" -X PUT "$1") + + if [ "$3" == "$response" ]; then + echo "Test $TESTS_AMOUNT passed." + ((TESTS_PASSED++)) + else + echo "Test $TESTS_AMOUNT failed." + echo "Test $TESTS_AMOUNT failed." >> $ERROR_LOG + echo "$response" > $ERROR_LOG + fi +} + +# Tests +post_course_data='{"title": "FIN Sitzung", "day": "Wed", "time": "18:00"}' +put_course_data='{"title": "FIN Sitzung", "day": "Wed", "time": "18:00", "kind": "meeting"}' + +test_get "$BASEURL/courses" "$(cat "./tests/01-get_courses.json")" +test_get "$BASEURL/course/0" "$(cat "./tests/02-get_course-0.json")" +test_get "$BASEURL/course/1" "$(cat "./tests/03-get_course-1.json")" +test_post "$BASEURL/course" "$post_course_data" "$(cat "./tests/04-post_course.json")" +test_get "$BASEURL/courses" "$(cat "./tests/05-get_courses.json")" +test_get "$BASEURL/course/2" "$(cat "./tests/06-get_course-2.json")" +test_delete "$BASEURL/course/1" "$(cat "./tests/07-delete_course-1.json")" +test_get "$BASEURL/courses" "$(cat "./tests/08-get_courses.json")" +test_put "$BASEURL/course/2" "$put_course_data" "$(cat "./tests/09-put_course-2.json")" +test_get "$BASEURL/courses" "$(cat "./tests/10-get_courses.json")" + +# Final result +echo "$TESTS_PASSED/$TESTS_AMOUNT tests passed." diff --git a/test.sh b/test.sh deleted file mode 100755 index 61193b8..0000000 --- a/test.sh +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/bash - -baseurl="http://localhost:8080/titamaApi/v1" -header="Content-Type: application/json" -post_course_data='{"title": "FIN Sitzung", "day": "Wed", "time": "18:00"}' -put_course_data='{"title": "FIN Sitzung", "day": "Wed", "time": "18:00", "kind": "meeting"}' - -printf "Testing GET %s/courses...\n" "$baseurl" -curl "$baseurl/courses" - -printf "\n\nTesting GET %s/course/0...\n" "$baseurl" -curl "$baseurl/course/0" - -printf "\n\nTesting GET %s/course/1...\n" "$baseurl" -curl "$baseurl/course/1" - -printf "\n\nTesting POST %s/course...\n" "$baseurl" -curl -d "$post_course_data" -H "$header" $baseurl/course - -printf "\n\nTesting GET %s/courses...\n" "$baseurl" -curl "$baseurl/courses" - -printf "\n\nTesting GET %s/course/2...\n" "$baseurl" -curl "$baseurl/course/2" - -printf "\n\nTesting DELETE %s/course/1...\n" "$baseurl" -curl -X DELETE "$baseurl/course/1" - -printf "\n\nTesting GET %s/courses...\n" "$baseurl" -curl "$baseurl/courses" - -printf "\n\nTesting PUT %s/course/2...\n" "$baseurl" -curl -d "$put_course_data" -H "$header" -X PUT $baseurl/course/2 - -printf "\n\nTesting GET %s/courses...\n" "$baseurl" -curl "$baseurl/courses" diff --git a/tests/01-get_courses.json b/tests/01-get_courses.json new file mode 100644 index 0000000..6c504af --- /dev/null +++ b/tests/01-get_courses.json @@ -0,0 +1,22 @@ +[ + { + "title": "UlmAPI", + "time": "18:00", + "day": "Monday", + "kind": "Lab", + "place": "O27/343", + "prof": "", + "turnin": "", + "id": 0 + }, + { + "title": "CCC Ulm", + "time": "20:00", + "day": "Monday", + "kind": "Meeting", + "place": "Cafe Einstein", + "prof": "", + "turnin": "", + "id": 1 + } +] diff --git a/tests/02-get_course-0.json b/tests/02-get_course-0.json new file mode 100644 index 0000000..220d3fc --- /dev/null +++ b/tests/02-get_course-0.json @@ -0,0 +1,10 @@ +{ + "title": "UlmAPI", + "time": "18:00", + "day": "Monday", + "kind": "Lab", + "place": "O27/343", + "prof": "", + "turnin": "", + "id": 0 +} \ No newline at end of file diff --git a/tests/03-get_course-1.json b/tests/03-get_course-1.json new file mode 100644 index 0000000..042b9d2 --- /dev/null +++ b/tests/03-get_course-1.json @@ -0,0 +1,10 @@ +{ + "title": "CCC Ulm", + "time": "20:00", + "day": "Monday", + "kind": "Meeting", + "place": "Cafe Einstein", + "prof": "", + "turnin": "", + "id": 1 +} \ No newline at end of file diff --git a/tests/04-post_course.json b/tests/04-post_course.json new file mode 100644 index 0000000..1bf01f3 --- /dev/null +++ b/tests/04-post_course.json @@ -0,0 +1,10 @@ +{ + "title": "FIN Sitzung", + "time": "18:00", + "day": "Wed", + "kind": "", + "place": "", + "prof": "", + "turnin": "", + "id": 2 +} diff --git a/tests/05-get_courses.json b/tests/05-get_courses.json new file mode 100644 index 0000000..b6a02da --- /dev/null +++ b/tests/05-get_courses.json @@ -0,0 +1,32 @@ +[ + { + "title": "UlmAPI", + "time": "18:00", + "day": "Monday", + "kind": "Lab", + "place": "O27/343", + "prof": "", + "turnin": "", + "id": 0 + }, + { + "title": "CCC Ulm", + "time": "20:00", + "day": "Monday", + "kind": "Meeting", + "place": "Cafe Einstein", + "prof": "", + "turnin": "", + "id": 1 + }, + { + "title": "FIN Sitzung", + "time": "18:00", + "day": "Wed", + "kind": "", + "place": "", + "prof": "", + "turnin": "", + "id": 2 + } +] \ No newline at end of file diff --git a/tests/06-get_course-2.json b/tests/06-get_course-2.json new file mode 100644 index 0000000..52f7927 --- /dev/null +++ b/tests/06-get_course-2.json @@ -0,0 +1,10 @@ +{ + "title": "FIN Sitzung", + "time": "18:00", + "day": "Wed", + "kind": "", + "place": "", + "prof": "", + "turnin": "", + "id": 2 +} \ No newline at end of file diff --git a/tests/07-delete_course-1.json b/tests/07-delete_course-1.json new file mode 100644 index 0000000..f97ff84 --- /dev/null +++ b/tests/07-delete_course-1.json @@ -0,0 +1,22 @@ +[ + { + "title": "UlmAPI", + "time": "18:00", + "day": "Monday", + "kind": "Lab", + "place": "O27/343", + "prof": "", + "turnin": "", + "id": 0 + }, + { + "title": "FIN Sitzung", + "time": "18:00", + "day": "Wed", + "kind": "", + "place": "", + "prof": "", + "turnin": "", + "id": 2 + } +] \ No newline at end of file diff --git a/tests/08-get_courses.json b/tests/08-get_courses.json new file mode 100644 index 0000000..f97ff84 --- /dev/null +++ b/tests/08-get_courses.json @@ -0,0 +1,22 @@ +[ + { + "title": "UlmAPI", + "time": "18:00", + "day": "Monday", + "kind": "Lab", + "place": "O27/343", + "prof": "", + "turnin": "", + "id": 0 + }, + { + "title": "FIN Sitzung", + "time": "18:00", + "day": "Wed", + "kind": "", + "place": "", + "prof": "", + "turnin": "", + "id": 2 + } +] \ No newline at end of file diff --git a/tests/09-put_course-2.json b/tests/09-put_course-2.json new file mode 100644 index 0000000..1df2aa5 --- /dev/null +++ b/tests/09-put_course-2.json @@ -0,0 +1,10 @@ +{ + "title": "FIN Sitzung", + "time": "18:00", + "day": "Wed", + "kind": "meeting", + "place": "", + "prof": "", + "turnin": "", + "id": 2 +} diff --git a/tests/10-get_courses.json b/tests/10-get_courses.json new file mode 100644 index 0000000..7fd5a31 --- /dev/null +++ b/tests/10-get_courses.json @@ -0,0 +1,22 @@ +[ + { + "title": "UlmAPI", + "time": "18:00", + "day": "Monday", + "kind": "Lab", + "place": "O27/343", + "prof": "", + "turnin": "", + "id": 0 + }, + { + "title": "FIN Sitzung", + "time": "18:00", + "day": "Wed", + "kind": "meeting", + "place": "", + "prof": "", + "turnin": "", + "id": 2 + } +] \ No newline at end of file