You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

220 line
8.7 KiB

  1. #!/bin/bash
  2. # Full API smoke test: list, update, and delete for gateways, zones,
  3. # tracker zones, and trackers. Followed by a pagination showcase for
  4. # every list endpoint.
  5. #
  6. # The script reads existing records from the database and exercises
  7. # update + delete on them, so it is safe to run against a populated
  8. # dev environment. Nothing is created — only existing rows are touched.
  9. #
  10. # Requires: jq (https://stedolan.github.io/jq/)
  11. #
  12. # Usage:
  13. # ./api/smoke_test.sh
  14. # BASE_URL=http://host:port ./api/smoke_test.sh
  15. set -e
  16. SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
  17. . "${SCRIPT_DIR}/../_common.sh"
  18. # ---------------------------------------------------------------------------
  19. # GATEWAYS
  20. # ---------------------------------------------------------------------------
  21. echo "=========================================="
  22. echo "GATEWAY API TESTS"
  23. echo "=========================================="
  24. # List all gateways. jq -c '.[]' prints one compact JSON object per line
  25. # so we can iterate and extract the id field.
  26. echo "1. Listing all Gateways"
  27. LIST=$(curl -s -X GET "$BASE_URL/reslevis/getGateways" | jq -c '.[]')
  28. GATEWAY_IDS=()
  29. IFS=$'\n'
  30. for r in $LIST; do
  31. echo "$r"
  32. GATEWAY_IDS+=($(echo "$r" | jq -r '.id'))
  33. done
  34. sleep 1
  35. # Update and delete only when there are at least 2 gateways so we don't
  36. # accidentally destroy the only one in the system.
  37. if [ ${#GATEWAY_IDS[@]} -gt 1 ]; then
  38. echo -e "\n\n2. Updating Gateway ${GATEWAY_IDS[1]}"
  39. # PUT replaces the full record — all fields must be supplied.
  40. curl -s -X PUT "$BASE_URL/reslevis/updateGateway/${GATEWAY_IDS[1]}" \
  41. -H "Content-Type: application/json" \
  42. -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\"}"
  43. sleep 1
  44. echo -e "\n\n3. Listing Gateways after update (verify name changed)"
  45. curl -s -X GET "$BASE_URL/reslevis/getGateways" | jq -c '.[]'
  46. sleep 1
  47. echo -e "\n\n4. Deleting Gateway ${GATEWAY_IDS[1]}"
  48. curl -s -X DELETE "$BASE_URL/reslevis/removeGateway/${GATEWAY_IDS[1]}"
  49. sleep 1
  50. echo -e "\n\n5. Verifying delete — Gateway ${GATEWAY_IDS[1]} should be gone"
  51. curl -s -X GET "$BASE_URL/reslevis/getGateways" | jq -c '.[]'
  52. else
  53. echo "Not enough gateways to test update/delete (need at least 2)"
  54. fi
  55. # ---------------------------------------------------------------------------
  56. # ZONES
  57. # ---------------------------------------------------------------------------
  58. echo -e "\n\n=========================================="
  59. echo "ZONE API TESTS"
  60. echo "=========================================="
  61. echo "6. Listing all Zones"
  62. LIST=$(curl -s -X GET "$BASE_URL/reslevis/getZones" | jq -c '.[]')
  63. ZONE_IDS=()
  64. for r in $LIST; do
  65. echo "$r"
  66. ZONE_IDS+=($(echo "$r" | jq -r '.id'))
  67. done
  68. sleep 1
  69. if [ ${#ZONE_IDS[@]} -gt 0 ]; then
  70. echo -e "\n\n7. Updating Zone ${ZONE_IDS[0]}"
  71. # updateZone takes the id inside the body (no path parameter).
  72. curl -s -X PUT "$BASE_URL/reslevis/updateZone" -H "Content-Type: application/json" \
  73. -d "{\"id\": \"${ZONE_IDS[0]}\", \"name\": \"Zone-Updated\", \"groups\": [\"security\", \"logistics\"]}"
  74. sleep 1
  75. echo -e "\n\n8. Listing Zones after update (verify name and groups changed)"
  76. curl -s -X GET "$BASE_URL/reslevis/getZones" | jq -c '.[]'
  77. sleep 1
  78. echo -e "\n\n9. Deleting Zone ${ZONE_IDS[0]}"
  79. curl -s -X DELETE "$BASE_URL/reslevis/removeZone/${ZONE_IDS[0]}"
  80. sleep 1
  81. echo -e "\n\n10. Verifying delete — Zone ${ZONE_IDS[0]} should be gone"
  82. curl -s -X GET "$BASE_URL/reslevis/getZones" | jq -c '.[]'
  83. else
  84. echo "No zones found — skipping update/delete tests"
  85. fi
  86. # ---------------------------------------------------------------------------
  87. # TRACKER ZONES
  88. # ---------------------------------------------------------------------------
  89. echo -e "\n\n=========================================="
  90. echo "TRACKERZONE API TESTS"
  91. echo "=========================================="
  92. echo "11. Listing all TrackerZones"
  93. LIST=$(curl -s -X GET "$BASE_URL/reslevis/getTrackerZones" | jq -c '.[]')
  94. TRACKERZONE_IDS=()
  95. for r in $LIST; do
  96. echo "$r"
  97. TRACKERZONE_IDS+=($(echo "$r" | jq -r '.id'))
  98. done
  99. sleep 1
  100. if [ ${#TRACKERZONE_IDS[@]} -gt 0 ]; then
  101. echo -e "\n\n12. Updating TrackerZone ${TRACKERZONE_IDS[0]}"
  102. # updateTrackerZone also takes the id inside the body.
  103. curl -s -X PUT "$BASE_URL/reslevis/updateTrackerZone" -H "Content-Type: application/json" \
  104. -d "{\"id\": \"${TRACKERZONE_IDS[0]}\", \"name\": \"TrackerZone-Updated\"}"
  105. sleep 1
  106. echo -e "\n\n13. Listing TrackerZones after update"
  107. curl -s -X GET "$BASE_URL/reslevis/getTrackerZones" | jq -c '.[]'
  108. sleep 1
  109. echo -e "\n\n14. Deleting TrackerZone ${TRACKERZONE_IDS[0]}"
  110. curl -s -X DELETE "$BASE_URL/reslevis/removeTrackerZone/${TRACKERZONE_IDS[0]}"
  111. sleep 1
  112. echo -e "\n\n15. Verifying delete — TrackerZone ${TRACKERZONE_IDS[0]} should be gone"
  113. curl -s -X GET "$BASE_URL/reslevis/getTrackerZones" | jq -c '.[]'
  114. else
  115. echo "No tracker zones found — skipping update/delete tests"
  116. fi
  117. # ---------------------------------------------------------------------------
  118. # TRACKERS
  119. # ---------------------------------------------------------------------------
  120. echo -e "\n\n=========================================="
  121. echo "TRACKER API TESTS"
  122. echo "=========================================="
  123. echo "16. Listing all Trackers"
  124. LIST=$(curl -s -X GET "$BASE_URL/reslevis/getTrackers" | jq -c '.[]')
  125. TRACKER_IDS=()
  126. for r in $LIST; do
  127. echo "$r"
  128. TRACKER_IDS+=($(echo "$r" | jq -r '.id'))
  129. done
  130. sleep 1
  131. if [ ${#TRACKER_IDS[@]} -gt 0 ]; then
  132. echo -e "\n\n17. Updating Tracker ${TRACKER_IDS[0]}"
  133. # Only the fields provided are updated; omitted fields retain their values
  134. # because the server does a full Save (not a partial patch).
  135. curl -s -X PUT "$BASE_URL/reslevis/updateTracker" -H "Content-Type: application/json" \
  136. -d "{\"id\": \"${TRACKER_IDS[0]}\", \"name\": \"Tracker-Updated\", \"battery\": 85, \"status\": \"inactive\"}"
  137. sleep 1
  138. echo -e "\n\n18. Listing Trackers after update (verify name/status changed)"
  139. curl -s -X GET "$BASE_URL/reslevis/getTrackers" | jq -c '.[]'
  140. sleep 1
  141. echo -e "\n\n19. Deleting Tracker ${TRACKER_IDS[0]}"
  142. # Deleting a tracker also publishes a DELETE event to Kafka so the
  143. # location and decoder services stop tracking that MAC address.
  144. curl -s -X DELETE "$BASE_URL/reslevis/removeTracker/${TRACKER_IDS[0]}"
  145. sleep 1
  146. echo -e "\n\n20. Verifying delete — Tracker ${TRACKER_IDS[0]} should be gone"
  147. curl -s -X GET "$BASE_URL/reslevis/getTrackers" | jq -c '.[]'
  148. else
  149. echo "No trackers found — skipping update/delete tests"
  150. fi
  151. # ---------------------------------------------------------------------------
  152. # PAGINATION SHOWCASE
  153. # All list endpoints accept ?limit=N&offset=N query parameters.
  154. # Default: limit=100, offset=0.
  155. # Use limit + offset to page through large result sets.
  156. # ---------------------------------------------------------------------------
  157. echo -e "\n\n=========================================="
  158. echo "PAGINATION SHOWCASE"
  159. echo "All list endpoints support ?limit=N&offset=N"
  160. echo "Default: limit=100, offset=0"
  161. echo "=========================================="
  162. echo -e "\n--- Gateways: first page (limit=5, offset=0) ---"
  163. curl -s -X GET "$BASE_URL/reslevis/getGateways?limit=5&offset=0" | jq '.'
  164. echo -e "\n--- Gateways: second page (limit=5, offset=5) ---"
  165. # offset moves the window forward by the page size; combine with the same
  166. # limit to get the next batch of results.
  167. curl -s -X GET "$BASE_URL/reslevis/getGateways?limit=5&offset=5" | jq '.'
  168. echo -e "\n--- Trackers: first page (limit=3, offset=0) ---"
  169. curl -s -X GET "$BASE_URL/reslevis/getTrackers?limit=3&offset=0" | jq '.'
  170. echo -e "\n--- Trackers: second page (limit=3, offset=3) ---"
  171. curl -s -X GET "$BASE_URL/reslevis/getTrackers?limit=3&offset=3" | jq '.'
  172. echo -e "\n--- Zones: first page (limit=10, offset=0) ---"
  173. curl -s -X GET "$BASE_URL/reslevis/getZones?limit=10&offset=0" | jq '.'
  174. echo -e "\n--- TrackerZones: first page (limit=10, offset=0) ---"
  175. curl -s -X GET "$BASE_URL/reslevis/getTrackerZones?limit=10&offset=0" | jq '.'
  176. echo -e "\n--- Alerts: first page (limit=10, offset=0) ---"
  177. # Alerts are created automatically when a tracker enters a restricted zone.
  178. # Use offset to page through the alert history.
  179. curl -s -X GET "$BASE_URL/reslevis/alerts?limit=10&offset=0" | jq '.'
  180. echo -e "\n--- Latest tracker positions: first page (limit=5, offset=0) ---"
  181. # getTracks (no uuid) returns the most recent position record per tracker.
  182. curl -s -X GET "$BASE_URL/reslevis/getTracks?limit=5&offset=0" | jq '.'
  183. echo -e "\n\n=========================================="
  184. echo "ALL TESTS COMPLETED"
  185. echo "=========================================="