# Copyright 2016 Telefonica Investigacion y Desarrollo, S.A.U
#
# This file is part of Orion Context Broker.
#
# Orion Context Broker is free software: you can redistribute it and/or
# modify it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# Orion Context Broker is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero
# General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Orion Context Broker. If not, see http://www.gnu.org/licenses/.
#
# For those usages not covered by this license please contact with
# iot_support at tid dot es

# VALGRIND_READY - to mark the test ready for valgrindTestSuite.sh

--NAME--
String filters for compound values of metadata: string match in subscriptions

--SHELL-INIT--
dbInit CB
brokerStart CB
accumulatorStart --pretty-print

--SHELL--

#
# 01. Create E1 with A1 with metadata M1 with compound value containing String, Integer and Float
# 02. Beginning: Create subscription 1 for mq=A1.M1.s~=test - notification
# 03. Dump and reset accumulator, see one notification
# 04. End: Create subscription 2 for mq=A1.M1.s~=ing - notification
# 05. Dump and reset accumulator, see one notification
# 06. Single-char: Create subscription 3 for mq=A1.M1.s~=_ - notification
# 07. Dump and reset accumulator, see one notification
# 08. No match: Create subscription 4 for mq=A1.M1.s~=fest_string - NO notification
# 09. Dump and reset accumulator, see no notification
# 10. Modify E1/A1 - provoke 3 notifications
# 11. Dump and reset accumulator, see 3 notifications
# 

echo "01. Create E1 with A1 with metadata M1 with compound value containing String, Integer and Float"
echo "==============================================================================================="
payload='{
  "id": "E1",
  "A1": {
    "value": "not important",
    "metadata": {
      "M1": {
        "value": {
          "i": 10,
          "f": 3.14,
          "s": "test_string",
          "modified": false
        }
      }
    }
  }
}'
orionCurl --url /v2/entities --payload "${payload}"
echo
echo


echo "02. Beginning: Create subscription 1 for mq=A1.M1.s~=test - notification"
echo "========================================================================"
payload='{
    "subject": {
        "entities": [
            {
                "idPattern": "E.*"
            }
        ],
        "condition": {
            "attrs": [ ],
            "expression": {
               "mq": "A1.M1.s~=test"
            }
         }
    },
    "notification": {
        "http": { "url": "http://localhost:'$LISTENER_PORT'/notify" },
        "attrs": [ ]
    },
    "expires": "2050-04-05T14:00:00.00Z"
}'
orionCurl --url /v2/subscriptions --payload "${payload}"
echo
echo


echo "03. Dump and reset accumulator, see one notification"
echo "===================================================="
accumulatorDump
accumulatorReset
echo
echo


echo "04. End: Create subscription 2 for mq=A1.M1.s~=ing - notification"
echo "================================================================="
payload='{
    "subject": {
        "entities": [
            {
                "idPattern": "E.*"
            }
        ],
        "condition": {
            "attrs": [ ],
            "expression": {
               "mq": "A1.M1.s~=ing"
            }
         }
    },
    "notification": {
        "http": { "url": "http://localhost:'$LISTENER_PORT'/notify" },
        "attrs": [ ]
    },
    "expires": "2050-04-05T14:00:00.00Z"
}'
orionCurl --url /v2/subscriptions --payload "${payload}"
echo
echo


echo "05. Dump and reset accumulator, see one notification"
echo "===================================================="
accumulatorDump
accumulatorReset
echo
echo


echo "06. Single-char: Create subscription 3 for mq=A1.M1.s~=_ - notification"
echo "======================================================================="
payload='{
    "subject": {
        "entities": [
            {
                "idPattern": "E.*"
            }
        ],
        "condition": {
            "attrs": [ ],
            "expression": {
               "mq": "A1.M1.s~=_"
            }
         }
    },
    "notification": {
        "http": { "url": "http://localhost:'$LISTENER_PORT'/notify" },
        "attrs": [ ]
    },
    "expires": "2050-04-05T14:00:00.00Z"
}'
orionCurl --url /v2/subscriptions --payload "${payload}"
echo
echo


echo "07. Dump and reset accumulator, see one notification"
echo "===================================================="
accumulatorDump
accumulatorReset
echo
echo


echo "08. No match: Create subscription 4 for mq=A1.M1.s~=fest_string - NO notification"
echo "================================================================================="
payload='{
    "subject": {
        "entities": [
            {
                "idPattern": "E.*"
            }
        ],
        "condition": {
            "attrs": [ ],
            "expression": {
               "mq": "A1.M1.s~=fest_string"
            }
         }
    },
    "notification": {
        "http": { "url": "http://localhost:'$LISTENER_PORT'/notify" },
        "attrs": [ ]
    },
    "expires": "2050-04-05T14:00:00.00Z"
}'
orionCurl --url /v2/subscriptions --payload "${payload}"
echo
echo


echo "09. Dump and reset accumulator, see no notification"
echo "==================================================="
accumulatorDump
accumulatorReset
echo
echo


echo "10. Modify E1/A1 - provoke 3 notifications"
echo "=========================================="
payload='{
  "A1": {
    "value": "modified",
    "metadata": {
      "M1": {
        "value": {
          "i": 10,
          "f": 3.14,
          "s": "test_string",
          "modified": true
        }
      }
    }
  }
}'
orionCurl --url /v2/entities/E1/attrs --payload "${payload}" -X PUT
echo
echo


echo "11. Dump and reset accumulator, see 3 notifications"
echo "==================================================="
accumulatorDump
accumulatorReset
echo
echo


--REGEXPECT--
01. Create E1 with A1 with metadata M1 with compound value containing String, Integer and Float
===============================================================================================
HTTP/1.1 201 Created
Content-Length: 0
Location: /v2/entities/E1?type=Thing
Fiware-Correlator: REGEX([0-9a-f\-]{36})
Date: REGEX(.*)



02. Beginning: Create subscription 1 for mq=A1.M1.s~=test - notification
========================================================================
HTTP/1.1 201 Created
Content-Length: 0
Location: /v2/subscriptions/REGEX([0-9a-f]{24})
Fiware-Correlator: REGEX([0-9a-f\-]{36})
Date: REGEX(.*)



03. Dump and reset accumulator, see one notification
====================================================
POST http://localhost:REGEX(\d+)/notify
Fiware-Servicepath: /
Content-Length: 232
User-Agent: orion/REGEX(\d+\.\d+\.\d+.*)
Ngsiv2-Attrsformat: normalized
Host: localhost:REGEX(\d+)
Accept: application/json
Content-Type: application/json; charset=utf-8
Fiware-Correlator: REGEX([0-9a-f\-]{36})

{
    "data": [
        {
            "A1": {
                "metadata": {
                    "M1": {
                        "type": "StructuredValue",
                        "value": {
                            "f": 3.14,
                            "i": 10,
                            "modified": false,
                            "s": "test_string"
                        }
                    }
                }, 
                "type": "Text", 
                "value": "not important"
            },
            "id": "E1", 
            "type": "Thing"
        }
    ],
    "subscriptionId": "REGEX([0-9a-f]{24})"
}
=======================================


04. End: Create subscription 2 for mq=A1.M1.s~=ing - notification
=================================================================
HTTP/1.1 201 Created
Content-Length: 0
Location: /v2/subscriptions/REGEX([0-9a-f]{24})
Fiware-Correlator: REGEX([0-9a-f\-]{36})
Date: REGEX(.*)



05. Dump and reset accumulator, see one notification
====================================================
POST http://localhost:REGEX(\d+)/notify
Fiware-Servicepath: /
Content-Length: 232
User-Agent: orion/REGEX(\d+\.\d+\.\d+.*)
Ngsiv2-Attrsformat: normalized
Host: localhost:REGEX(\d+)
Accept: application/json
Content-Type: application/json; charset=utf-8
Fiware-Correlator: REGEX([0-9a-f\-]{36})

{
    "data": [
        {
            "A1": {
                "metadata": {
                    "M1": {
                        "type": "StructuredValue",
                        "value": {
                            "f": 3.14,
                            "i": 10,
                            "modified": false,
                            "s": "test_string"
                        }
                    }
                }, 
                "type": "Text", 
                "value": "not important"
            },
            "id": "E1", 
            "type": "Thing"
        }
    ],
    "subscriptionId": "REGEX([0-9a-f]{24})"
}
=======================================


06. Single-char: Create subscription 3 for mq=A1.M1.s~=_ - notification
=======================================================================
HTTP/1.1 201 Created
Content-Length: 0
Location: /v2/subscriptions/REGEX([0-9a-f]{24})
Fiware-Correlator: REGEX([0-9a-f\-]{36})
Date: REGEX(.*)



07. Dump and reset accumulator, see one notification
====================================================
POST http://localhost:REGEX(\d+)/notify
Fiware-Servicepath: /
Content-Length: 232
User-Agent: orion/REGEX(\d+\.\d+\.\d+.*)
Ngsiv2-Attrsformat: normalized
Host: localhost:REGEX(\d+)
Accept: application/json
Content-Type: application/json; charset=utf-8
Fiware-Correlator: REGEX([0-9a-f\-]{36})

{
    "data": [
        {
            "A1": {
                "metadata": {
                    "M1": {
                        "type": "StructuredValue",
                        "value": {
                            "f": 3.14,
                            "i": 10,
                            "modified": false,
                            "s": "test_string"
                        }
                    }
                }, 
                "type": "Text", 
                "value": "not important"
            },
            "id": "E1", 
            "type": "Thing"
        }
    ],
    "subscriptionId": "REGEX([0-9a-f]{24})"
}
=======================================


08. No match: Create subscription 4 for mq=A1.M1.s~=fest_string - NO notification
=================================================================================
HTTP/1.1 201 Created
Content-Length: 0
Location: /v2/subscriptions/REGEX([0-9a-f]{24})
Fiware-Correlator: REGEX([0-9a-f\-]{36})
Date: REGEX(.*)



09. Dump and reset accumulator, see no notification
===================================================


10. Modify E1/A1 - provoke 3 notifications
==========================================
HTTP/1.1 204 No Content
Fiware-Correlator: REGEX([0-9a-f\-]{36})
Date: REGEX(.*)



11. Dump and reset accumulator, see 3 notifications
===================================================
POST http://localhost:REGEX(\d+)/notify
Fiware-Servicepath: /
Content-Length: 226
User-Agent: orion/REGEX(\d+\.\d+\.\d+.*)
Ngsiv2-Attrsformat: normalized
Host: localhost:REGEX(\d+)
Accept: application/json
Content-Type: application/json; charset=utf-8
Fiware-Correlator: REGEX([0-9a-f\-]{36})

{
    "data": [
        {
            "A1": {
                "metadata": {
                    "M1": {
                        "type": "StructuredValue",
                        "value": {
                            "f": 3.14,
                            "i": 10,
                            "modified": true,
                            "s": "test_string"
                        }
                    }
                }, 
                "type": "Text", 
                "value": "modified"
            },
            "id": "E1", 
            "type": "Thing"
        }
    ],
    "subscriptionId": "REGEX([0-9a-f]{24})"
}
=======================================
POST http://localhost:REGEX(\d+)/notify
Fiware-Servicepath: /
Content-Length: 226
User-Agent: orion/REGEX(\d+\.\d+\.\d+.*)
Ngsiv2-Attrsformat: normalized
Host: localhost:REGEX(\d+)
Accept: application/json
Content-Type: application/json; charset=utf-8
Fiware-Correlator: REGEX([0-9a-f\-]{36})

{
    "data": [
        {
            "A1": {
                "metadata": {
                    "M1": {
                        "type": "StructuredValue",
                        "value": {
                            "f": 3.14,
                            "i": 10,
                            "modified": true,
                            "s": "test_string"
                        }
                    }
                }, 
                "type": "Text", 
                "value": "modified"
            },
            "id": "E1", 
            "type": "Thing"
        }
    ],
    "subscriptionId": "REGEX([0-9a-f]{24})"
}
=======================================
POST http://localhost:REGEX(\d+)/notify
Fiware-Servicepath: /
Content-Length: 226
User-Agent: orion/REGEX(\d+\.\d+\.\d+.*)
Ngsiv2-Attrsformat: normalized
Host: localhost:REGEX(\d+)
Accept: application/json
Content-Type: application/json; charset=utf-8
Fiware-Correlator: REGEX([0-9a-f\-]{36})

{
    "data": [
        {
            "A1": {
                "metadata": {
                    "M1": {
                        "type": "StructuredValue",
                        "value": {
                            "f": 3.14,
                            "i": 10,
                            "modified": true,
                            "s": "test_string"
                        }
                    }
                }, 
                "type": "Text", 
                "value": "modified"
            },
            "id": "E1", 
            "type": "Thing"
        }
    ],
    "subscriptionId": "REGEX([0-9a-f]{24})"
}
=======================================


--TEARDOWN--
brokerStop CB
accumulatorStop
dbDrop CB
