#!/bin/bash
set -e

echo "Test /api/dencode should b64 encode secret springData having yaml input and yaml output"
curl --silent --show-error --request POST 'http://localhost/ssw/api/dencode' \
  --header 'Content-Type: application/yaml' \
  --header 'Accept: application/yaml' \
  --data-binary '@stringData.yaml' \
  | diff --strip-trailing-cr --ignore-blank-lines data.yaml -

echo "Test /api/dencode should b64 decode secret data having yaml input and yaml output"
curl --silent --show-error --request POST 'http://localhost/ssw/api/dencode' \
  --header 'Content-Type: application/yaml' \
  --header 'Accept: application/yaml' \
  --data-binary '@data.yaml' \
  | tee /dev/stderr \
  | yq --prettyPrint | diff --strip-trailing-cr --ignore-blank-lines stringData.yaml -

echo "Test /api/dencode should b64 encode secret springData having yaml input and json output"
curl --silent --show-error --request POST 'http://localhost/ssw/api/dencode' \
  --header 'Content-Type: application/yaml' \
  --header 'Accept: application/json' \
  --data-binary '@stringData.yaml' \
  | tee /dev/stderr \
  | jq --sort-keys . \
  | diff <(jq --sort-keys . data.json)  -

echo "Test /api/dencode should b64 decode secret data having yaml input and json output"
curl --silent --show-error --request POST 'http://localhost/ssw/api/dencode' \
  --header 'Content-Type: application/yaml' \
  --header 'Accept: application/json' \
  --data-binary '@data.yaml' \
  | tee /dev/stderr \
  | jq --sort-keys . \
  | diff <(jq --sort-keys . stringData.json) -

echo "Test /api/dencode should b64 encode secret springData having json input and json output"
curl --silent --show-error --request POST 'http://localhost/ssw/api/dencode' \
  --header 'Content-Type: application/json' \
  --header 'Accept: application/json' \
  --data-binary '@stringData.json' \
  | tee /dev/stderr \
  | jq --sort-keys . \
  | diff <(jq --sort-keys . data.json)  -

echo "Test /api/dencode should b64 decode secret data having json input and json output"
curl --silent --show-error --request POST 'http://localhost/ssw/api/dencode' \
  --header 'Content-Type: application/json' \
  --header 'Accept: application/json' \
  --data-binary '@data.json' \
  | tee /dev/stderr \
  | jq --sort-keys . \
  | diff <(jq --sort-keys . stringData.json) -


echo "Test /api/dencode should b64 encode secret springData having json input and yaml output"
curl --silent --show-error --request POST 'http://localhost/ssw/api/dencode' \
  --header 'Content-Type: application/json' \
  --header 'Accept: application/yaml' \
  --data-binary '@stringData.json' \
  | diff --strip-trailing-cr --ignore-blank-lines data.yaml -

echo "Test /api/dencode should b64 decode secret data having json input and yaml output"
curl --silent --show-error --request POST 'http://localhost/ssw/api/dencode' \
  --header 'Content-Type: application/json' \
  --header 'Accept: application/yaml' \
  --data-binary '@data.json' \
  | tee /dev/stderr \
  | yq --prettyPrint | diff --strip-trailing-cr --ignore-blank-lines stringData.yaml -
