|
- #!/bin/bash
- # Full API smoke test: list, update, and delete for gateways, zones,
- # tracker zones, and trackers. Followed by a pagination showcase for
- # every list endpoint.
- #
- # The script reads existing records from the database and exercises
- # update + delete on them, so it is safe to run against a populated
- # dev environment. Nothing is created — only existing rows are touched.
- #
- # Requires: jq (https://stedolan.github.io/jq/)
- #
- # Usage:
- # ./api/smoke_test.sh
- # BASE_URL=http://host:port ./api/smoke_test.sh
- set -e
- SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
- . "${SCRIPT_DIR}/../_common.sh"
-
- # ---------------------------------------------------------------------------
- # GATEWAYS
- # ---------------------------------------------------------------------------
- echo "=========================================="
- echo "GATEWAY API TESTS"
- echo "=========================================="
-
- # List all gateways. jq -c '.[]' prints one compact JSON object per line
- # so we can iterate and extract the id field.
- echo "1. Listing all Gateways"
- LIST=$(curl -s -X GET "$BASE_URL/reslevis/getGateways" | jq -c '.[]')
- GATEWAY_IDS=()
- IFS=$'\n'
- for r in $LIST; do
- echo "$r"
- GATEWAY_IDS+=($(echo "$r" | jq -r '.id'))
- done
- sleep 1
-
- # Update and delete only when there are at least 2 gateways so we don't
- # accidentally destroy the only one in the system.
- if [ ${#GATEWAY_IDS[@]} -gt 1 ]; then
- echo -e "\n\n2. Updating Gateway ${GATEWAY_IDS[1]}"
- # PUT replaces the full record — all fields must be supplied.
- curl -s -X PUT "$BASE_URL/reslevis/updateGateway/${GATEWAY_IDS[1]}" \
- -H "Content-Type: application/json" \
- -d "{\"id\": \"${GATEWAY_IDS[1]}\", \"name\": \"GU-100-Updated\", \"mac\": \"AA:BB:CC:DD:EE:FF\", \"status\": \"online\", \"model\": \"MG3\", \"ip\": \"127.0.0.1\", \"position\": \"unknown\", \"x\": 1, \"y\": 1, \"notes\": \"some description\", \"floor\": \"second\", \"building\": \"hospital\"}"
- sleep 1
-
- echo -e "\n\n3. Listing Gateways after update (verify name changed)"
- curl -s -X GET "$BASE_URL/reslevis/getGateways" | jq -c '.[]'
- sleep 1
-
- echo -e "\n\n4. Deleting Gateway ${GATEWAY_IDS[1]}"
- curl -s -X DELETE "$BASE_URL/reslevis/removeGateway/${GATEWAY_IDS[1]}"
- sleep 1
-
- echo -e "\n\n5. Verifying delete — Gateway ${GATEWAY_IDS[1]} should be gone"
- curl -s -X GET "$BASE_URL/reslevis/getGateways" | jq -c '.[]'
- else
- echo "Not enough gateways to test update/delete (need at least 2)"
- fi
-
- # ---------------------------------------------------------------------------
- # ZONES
- # ---------------------------------------------------------------------------
- echo -e "\n\n=========================================="
- echo "ZONE API TESTS"
- echo "=========================================="
-
- echo "6. Listing all Zones"
- LIST=$(curl -s -X GET "$BASE_URL/reslevis/getZones" | jq -c '.[]')
- ZONE_IDS=()
- for r in $LIST; do
- echo "$r"
- ZONE_IDS+=($(echo "$r" | jq -r '.id'))
- done
- sleep 1
-
- if [ ${#ZONE_IDS[@]} -gt 0 ]; then
- echo -e "\n\n7. Updating Zone ${ZONE_IDS[0]}"
- # updateZone takes the id inside the body (no path parameter).
- curl -s -X PUT "$BASE_URL/reslevis/updateZone" -H "Content-Type: application/json" \
- -d "{\"id\": \"${ZONE_IDS[0]}\", \"name\": \"Zone-Updated\", \"groups\": [\"security\", \"logistics\"]}"
- sleep 1
-
- echo -e "\n\n8. Listing Zones after update (verify name and groups changed)"
- curl -s -X GET "$BASE_URL/reslevis/getZones" | jq -c '.[]'
- sleep 1
-
- echo -e "\n\n9. Deleting Zone ${ZONE_IDS[0]}"
- curl -s -X DELETE "$BASE_URL/reslevis/removeZone/${ZONE_IDS[0]}"
- sleep 1
-
- echo -e "\n\n10. Verifying delete — Zone ${ZONE_IDS[0]} should be gone"
- curl -s -X GET "$BASE_URL/reslevis/getZones" | jq -c '.[]'
- else
- echo "No zones found — skipping update/delete tests"
- fi
-
- # ---------------------------------------------------------------------------
- # TRACKER ZONES
- # ---------------------------------------------------------------------------
- echo -e "\n\n=========================================="
- echo "TRACKERZONE API TESTS"
- echo "=========================================="
-
- echo "11. Listing all TrackerZones"
- LIST=$(curl -s -X GET "$BASE_URL/reslevis/getTrackerZones" | jq -c '.[]')
- TRACKERZONE_IDS=()
- for r in $LIST; do
- echo "$r"
- TRACKERZONE_IDS+=($(echo "$r" | jq -r '.id'))
- done
- sleep 1
-
- if [ ${#TRACKERZONE_IDS[@]} -gt 0 ]; then
- echo -e "\n\n12. Updating TrackerZone ${TRACKERZONE_IDS[0]}"
- # updateTrackerZone also takes the id inside the body.
- curl -s -X PUT "$BASE_URL/reslevis/updateTrackerZone" -H "Content-Type: application/json" \
- -d "{\"id\": \"${TRACKERZONE_IDS[0]}\", \"name\": \"TrackerZone-Updated\"}"
- sleep 1
-
- echo -e "\n\n13. Listing TrackerZones after update"
- curl -s -X GET "$BASE_URL/reslevis/getTrackerZones" | jq -c '.[]'
- sleep 1
-
- echo -e "\n\n14. Deleting TrackerZone ${TRACKERZONE_IDS[0]}"
- curl -s -X DELETE "$BASE_URL/reslevis/removeTrackerZone/${TRACKERZONE_IDS[0]}"
- sleep 1
-
- echo -e "\n\n15. Verifying delete — TrackerZone ${TRACKERZONE_IDS[0]} should be gone"
- curl -s -X GET "$BASE_URL/reslevis/getTrackerZones" | jq -c '.[]'
- else
- echo "No tracker zones found — skipping update/delete tests"
- fi
-
- # ---------------------------------------------------------------------------
- # TRACKERS
- # ---------------------------------------------------------------------------
- echo -e "\n\n=========================================="
- echo "TRACKER API TESTS"
- echo "=========================================="
-
- echo "16. Listing all Trackers"
- LIST=$(curl -s -X GET "$BASE_URL/reslevis/getTrackers" | jq -c '.[]')
- TRACKER_IDS=()
- for r in $LIST; do
- echo "$r"
- TRACKER_IDS+=($(echo "$r" | jq -r '.id'))
- done
- sleep 1
-
- if [ ${#TRACKER_IDS[@]} -gt 0 ]; then
- echo -e "\n\n17. Updating Tracker ${TRACKER_IDS[0]}"
- # Only the fields provided are updated; omitted fields retain their values
- # because the server does a full Save (not a partial patch).
- curl -s -X PUT "$BASE_URL/reslevis/updateTracker" -H "Content-Type: application/json" \
- -d "{\"id\": \"${TRACKER_IDS[0]}\", \"name\": \"Tracker-Updated\", \"battery\": 85, \"status\": \"inactive\"}"
- sleep 1
-
- echo -e "\n\n18. Listing Trackers after update (verify name/status changed)"
- curl -s -X GET "$BASE_URL/reslevis/getTrackers" | jq -c '.[]'
- sleep 1
-
- echo -e "\n\n19. Deleting Tracker ${TRACKER_IDS[0]}"
- # Deleting a tracker also publishes a DELETE event to Kafka so the
- # location and decoder services stop tracking that MAC address.
- curl -s -X DELETE "$BASE_URL/reslevis/removeTracker/${TRACKER_IDS[0]}"
- sleep 1
-
- echo -e "\n\n20. Verifying delete — Tracker ${TRACKER_IDS[0]} should be gone"
- curl -s -X GET "$BASE_URL/reslevis/getTrackers" | jq -c '.[]'
- else
- echo "No trackers found — skipping update/delete tests"
- fi
-
- # ---------------------------------------------------------------------------
- # PAGINATION SHOWCASE
- # All list endpoints accept ?limit=N&offset=N query parameters.
- # Default: limit=100, offset=0.
- # Use limit + offset to page through large result sets.
- # ---------------------------------------------------------------------------
- echo -e "\n\n=========================================="
- echo "PAGINATION SHOWCASE"
- echo "All list endpoints support ?limit=N&offset=N"
- echo "Default: limit=100, offset=0"
- echo "=========================================="
-
- echo -e "\n--- Gateways: first page (limit=5, offset=0) ---"
- curl -s -X GET "$BASE_URL/reslevis/getGateways?limit=5&offset=0" | jq '.'
-
- echo -e "\n--- Gateways: second page (limit=5, offset=5) ---"
- # offset moves the window forward by the page size; combine with the same
- # limit to get the next batch of results.
- curl -s -X GET "$BASE_URL/reslevis/getGateways?limit=5&offset=5" | jq '.'
-
- echo -e "\n--- Trackers: first page (limit=3, offset=0) ---"
- curl -s -X GET "$BASE_URL/reslevis/getTrackers?limit=3&offset=0" | jq '.'
-
- echo -e "\n--- Trackers: second page (limit=3, offset=3) ---"
- curl -s -X GET "$BASE_URL/reslevis/getTrackers?limit=3&offset=3" | jq '.'
-
- echo -e "\n--- Zones: first page (limit=10, offset=0) ---"
- curl -s -X GET "$BASE_URL/reslevis/getZones?limit=10&offset=0" | jq '.'
-
- echo -e "\n--- TrackerZones: first page (limit=10, offset=0) ---"
- curl -s -X GET "$BASE_URL/reslevis/getTrackerZones?limit=10&offset=0" | jq '.'
-
- echo -e "\n--- Alerts: first page (limit=10, offset=0) ---"
- # Alerts are created automatically when a tracker enters a restricted zone.
- # Use offset to page through the alert history.
- curl -s -X GET "$BASE_URL/reslevis/alerts?limit=10&offset=0" | jq '.'
-
- echo -e "\n--- Latest tracker positions: first page (limit=5, offset=0) ---"
- # getTracks (no uuid) returns the most recent position record per tracker.
- curl -s -X GET "$BASE_URL/reslevis/getTracks?limit=5&offset=0" | jq '.'
-
- echo -e "\n\n=========================================="
- echo "ALL TESTS COMPLETED"
- echo "=========================================="
|