<commit_before>#!/bin/sh

########################################################################
#
# title:       Virtual-Vehicles Project Integration Tests
# author:      <NAME> (https://programmaticponderings.com)
# url:         https://github.com/garystafford/virtual-vehicles-docker  
# description: Performs integration tests on the Virtual-Vehicles Java
#              microservices example REST API
# to run:      sh test.sh
#
########################################################################

echo --- Integration Tests ---
echo

# variables
hostname="localhost"
application="Test API Client"
secret="<KEY>"

#tests
echo "TEST: GET request should return 'true' in the response body"
curl -X GET -H 'Accept: application/json; charset=UTF-8' \
--url "http://${hostname}:8581/vehicles/utils/ping.json" \
| grep true > /dev/null
[ "$?" -ne 0 ] && echo "RESULT: fail" && exit 1
echo "RESULT: pass"
echo


echo "TEST: POST request should return a new client in the response body with an 'id'"
curl -X POST -H "Cache-Control: no-cache" -d '{
    "application": "${application}",
    "secret": "${secret}"
}' --url "http://${hostname}:8587/clients" \
| grep '"id":"[a-zA-Z0-9]\{24\}"' > /dev/null
[ "$?" -ne 0 ] && echo "RESULT: fail" && exit 1
echo "RESULT: pass"
echo


echo "SETUP: Get the new client's apiKey for next test"
apiKey=$(curl -X POST -H "Cache-Control: no-cache" -d '{
    "application": "${application}",
    "secret": "${secret}"
}' --url "http://${hostname}:8587/clients" \
| grep -o '"apiKey":"[a-zA-Z0-9]\{24\}"' \
| grep -o '[a-zA-Z0-9]\{24\}' \
| sed -e 's/^"//'  -e 's/"$//')

echo
#echo apiKey: ${apiKey} && echo


echo "TEST: GET request should return a new jwt in the response body"
curl -X GET -H "Cache-Control: no-cache" \
--url "http://${hostname}:8587/jwts?apiKey=${apiKey}&secret=<KEY>" \
| grep '[a-zA-Z0-9_-]\{1,\}\.[a-zA-Z0-9_-]\{1,\}\.[a-zA-Z0-9_-]\{1,\}' > /dev/null
[ "$?" -ne 0 ] && echo "RESULT: fail" && exit 1
echo "RESULT: pass"
echo


echo "SETUP: Get a new jwt using the new client for the next test"
 jwt=$(curl -X GET -H "Cache-Control: no-cache" \
--url "http://${hostname}:8587/jwts?apiKey=2bv1JOK6M3o4ah9dUDPi8SRs&secret=<KEY>" \
| grep '[a-zA-Z0-9_-]\{1,\}\.[a-zA-Z0-9_-]\{1,\}\.[a-zA-Z0-9_-]\{1,\}' \
| sed -e 's/^"//'  -e 's/"$//')

echo
#echo jwt: ${jwt} && echo


echo "TEST: POST request should return a new vehicle in the response body with an 'id'"
curl -X POST -H "Cache-Control: no-cache" \
-H "Authorization: Bearer ${jwt}" \
-d '{
    "year": 2015,
    "make": "Test",
    "model": "Foo",
    "color": "White",
    "type": "Sedan",
    "mileage": 250
}' --url "http://${hostname}:8581/vehicles" \
| grep '"id":"[a-zA-Z0-9]\{24\}"' > /dev/null
[ "$?" -ne 0 ] && echo "RESULT: fail" && exit 1
echo "RESULT: pass"
echo<commit_msg>Adding integration tests in bash<commit_after>#!/bin/sh

########################################################################
#
# title:       Virtual-Vehicles Project Integration Tests
# author:      <NAME> (https://programmaticponderings.com)
# url:         https://github.com/garystafford/virtual-vehicles-docker  
# description: Performs integration tests on the Virtual-Vehicles
#              microservices
# to run:      sh test.sh
#
########################################################################

echo --- Integration Tests ---
echo

### VARIABLES ###
hostname="localhost"
application="Test API Client"
secret="<KEY>"

# http://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux
RED='\033[0;31m'
GREEN='\033[0;32m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color

### TESTS ###
echo "TEST: GET request should return 'true' in the response body"
url="http://${hostname}:8581/vehicles/utils/ping.json"
echo ${url}
curl -X GET -H 'Accept: application/json; charset=UTF-8' \
--url "${url}" \
| grep true > /dev/null
[ "$?" -ne 0 ] && echo "${RED}RESULT: fail${NC}" && exit 1
echo "${GREEN}RESULT: pass${NC}"
echo

echo "TEST: POST request should return a new client in the response body with an 'id'"
url="http://${hostname}:8587/clients"
echo ${url}
curl -X POST -H "Cache-Control: no-cache" -d '{
    "application": "Test API Client",
    "secret": "<KEY>"
}' --url "${url}" \
| grep '"id":"[a-zA-Z0-9]\{24\}"' > /dev/null
[ "$?" -ne 0 ] && echo "${RED}RESULT: fail${NC}" && exit 1
echo "${GREEN}RESULT: pass${NC}"
echo

echo "SETUP: Get the new client's apiKey for next test"
url="http://${hostname}:8587/clients"
echo ${url}
apiKey=$(curl -X POST -H "Cache-Control: no-cache" -d '{
    "application": "Test API Client",
    "secret": "<KEY>"
}' --url "${url}" \
| grep -o '"apiKey":"[a-zA-Z0-9]\{24\}"' \
| grep -o '[a-zA-Z0-9]\{24\}' \
| sed -e 's/^"//'  -e 's/"$//')
echo ${CYAN}apiKey: ${apiKey}${NC}
echo

echo "TEST: GET request should return a new jwt in the response body"
url="http://${hostname}:8587/jwts?apiKey=${apiKey}&secret=${secret}"
echo ${url}
curl -X GET -H "Cache-Control: no-cache" \
--url "${url}" \
| grep '[a-zA-Z0-9_-]\{1,\}\.[a-zA-Z0-9_-]\{1,\}\.[a-zA-Z0-9_-]\{1,\}' > /dev/null
[ "$?" -ne 0 ] && echo "${RED}RESULT: fail${NC}" && exit 1
echo "${GREEN}RESULT: pass${NC}"
echo

echo "SETUP: Get a new jwt using the new client for the next test"
url="http://${hostname}:8587/jwts?apiKey=${apiKey}&secret=${secret}"
echo ${url}
jwt=$(curl -X GET -H "Cache-Control: no-cache" \
--url "${url}" \
| grep '[a-zA-Z0-9_-]\{1,\}\.[a-zA-Z0-9_-]\{1,\}\.[a-zA-Z0-9_-]\{1,\}' \
| sed -e 's/^"//'  -e 's/"$//')
echo ${CYAN}jwt: ${jwt}${NC}
echo

echo "TEST: POST request should return a new vehicle in the response body with an 'id'"
url="http://${hostname}:8581/vehicles"
echo ${url}
curl -X POST -H "Cache-Control: no-cache" \
-H "Authorization: Bearer ${jwt}" \
-d '{
    "year": 2015,
    "make": "Test",
    "model": "Foo",
    "color": "White",
    "type": "Sedan",
    "mileage": 250
}' --url "${url}" \
| grep '"id":"[a-zA-Z0-9]\{24\}"' > /dev/null
[ "$?" -ne 0 ] && echo "${RED}RESULT: fail${NC}" && exit 1
echo "${GREEN}RESULT: pass${NC}"
echo

echo "SETUP: Get id from new vehicle for the next test"
url="http://${hostname}:8581/vehicles?filter=make::Test|model::Foo&limit=1"
echo ${url}
id=$(curl -X GET -H "Cache-Control: no-cache" \
-H "Authorization: Bearer ${jwt}" \
--url "${url}" \
| grep '"id":"[a-zA-Z0-9]\{24\}"' \
| grep -o '[a-zA-Z0-9]\{24\}' \
| tail -1 \
| sed -e 's/^"//'  -e 's/"$//')
echo ${CYAN}vehicle id: ${id}${NC}
echo

echo "TEST: GET request should return a vehicle in the response body with the requested 'id'"
url="http://${hostname}:8581/vehicles/${id}"
echo ${url}
curl -X GET -H "Cache-Control: no-cache" \
-H "Authorization: Bearer ${jwt}" \
--url "${url}" \
| grep '"id":"[a-zA-Z0-9]\{24\}"' > /dev/null
[ "$?" -ne 0 ] && echo "${RED}RESULT: fail${NC}" && exit 1
echo "${GREEN}RESULT: pass${NC}"
echo
{'fraction_non_alphanumeric': 0.27294117647058824, 'fraction_numerical': 0.03058823529411765, 'mean_word_length': 3.60148849797023, 'pattern_counts': {'":': 28, '<': 11, '<?xml version=': 0, '>': 20, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5536428', 'n_tokens_mistral': 2708, 'n_tokens_neox': 2536, 'n_words': 670}