Skip to content

Commit

Permalink
ADD mapper and thMapper transformations tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fgalan committed May 28, 2024
1 parent 8587216 commit 90ca88e
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 5 deletions.
46 changes: 46 additions & 0 deletions doc/manuals/orion-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@
- [`log2`](#log2)
- [`sqrt`](#sqrt)
- [`replaceStr`](#replaceStr)
- [`mapper`](#mapper)
- [`thMapper`](#thmapper)
- [Failsafe cases](#failsafe-cases)
- [Known limitations](#known-limitations)
- [Oneshot Subscriptions](#oneshot-subscriptions)
Expand Down Expand Up @@ -2767,6 +2769,50 @@ results in
"fuubar"
```

#### mapper

Returns a value among several choices based in one to one mapping. This function is based in an array of *values* and an array of *choices* (which length is exactly the same). Thus, if the input value is equal to the *i*-th item of *values*, then *i*-th item of *choices* is returned.

This transformation returns `null` if some problem with the arguments is found (i.e. input is not found among the values, choices length is not exacly the same as values, the input is not an string, etc.)

Extra arguments:
* values array
* choices array

Example (being context `{"c": "fr", "values": ["es", "fr", "de"], "choices": ["Spain", "France", "Germany"]}`):

```
c|mapper(values,choices)
```

results in

```
"France"
```

#### thMapper

Returns a value among several choices based in threshold values. This function is based in an array of *values* and an array of *choices* (which length is exactly the same as values plus one). Thus, if the input value is between the *i*-th and the *i+1*-th item of *values*, then *i*+1-th item of *choices* is returned.

This transformation returns `null` if some problem with the arguments is found (i.e. choices length is not exacly the same as values plus one, some of the items in the values array is not a number, etc.)

Extra arguments:
* values array
* choices array

Example (being context `{"c": 0.5, "values": [-1, 1], "choices": ["low", "medium", "high"]}`):

```
c|thMapper(values,choices)
```

results in

```
"medium"
```

### Failsafe cases

As failsafe behaviour, evaluation returns `null` in the following cases:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ accumulatorStart --pretty-print

#
# 01. Create custom sub using all transformations
# 02. Create entity E1 with A to P attributes
# 02. Create entity E1 with A to V attributes
# 03. Dump accumulator and see expected notification
#

Expand Down Expand Up @@ -71,7 +71,9 @@ payload='{
"Q",
"R",
"S",
"T"
"T",
"U",
"V"
],
"httpCustom": {
"url": "http://127.0.0.1:'${LISTENER_PORT}'/notify",
Expand Down Expand Up @@ -155,6 +157,14 @@ payload='{
"T": {
"type": "Number",
"value": "${T|sqrt}"
},
"U": {
"type": "Text",
"value": "${U|mapper(['\''es'\'','\''fr'\''],['\''Spain'\'','\''France'\''])}"
},
"V": {
"type": "Text",
"value": "${V|thMapper([1,2],['\''low'\'','\''medium'\'','\''high'\''])}"
}
}
}
Expand All @@ -165,7 +175,7 @@ echo
echo


echo "02. Create entity E1 with A to P attributes"
echo "02. Create entity E1 with A to V attributes"
echo "==========================================="
payload='{
"id": "E1",
Expand Down Expand Up @@ -254,6 +264,14 @@ payload='{
"T": {
"type": "Number",
"value": 25
},
"U": {
"type": "Text",
"value": "fr"
},
"V": {
"type": "Number",
"value": 1.5
}
}'
orionCurl --url /v2/entities --payload "$payload"
Expand All @@ -279,7 +297,7 @@ Content-Length: 0



02. Create entity E1 with A to P attributes
02. Create entity E1 with A to V attributes
===========================================
HTTP/1.1 201 Created
Date: REGEX(.*)
Expand All @@ -293,7 +311,7 @@ Content-Length: 0
==================================================
POST http://127.0.0.1:REGEX(\d+)/notify
Fiware-Servicepath: /
Content-Length: 1076
Content-Length: 1178
User-Agent: orion/REGEX(\d+\.\d+\.\d+.*)
Ngsiv2-Attrsformat: normalized
Host: 127.0.0.1:REGEX(\d+)
Expand Down Expand Up @@ -407,6 +425,16 @@ Fiware-Correlator: REGEX([0-9a-f\-]{36}); cbnotif=1
"type": "Number",
"value": 5
},
"U": {
"metadata": {},
"type": "Text",
"value": "France"
},
"V": {
"metadata": {},
"type": "Text",
"value": "medium"
},
"id": "E1",
"type": "T"
}
Expand Down

0 comments on commit 90ca88e

Please sign in to comment.