Skip to content

Commit a39ec8b

Browse files
device_connected_event + test
Signed-off-by: Gabriele Ghio <gabriele.ghio@secomind.com>
1 parent af86f17 commit a39ec8b

File tree

2 files changed

+135
-0
lines changed

2 files changed

+135
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#
2+
# This file is part of Astarte.
3+
#
4+
# Copyright 2025 SECO Mind Srl
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
defmodule Astarte.Core.Generators.Triggers.SimpleEvents.DeviceConnectedEvent do
20+
@moduledoc """
21+
This module provides generators for Astarte Trigger Simple Event DeviceConnectedEvent struct.
22+
"""
23+
use ExUnitProperties
24+
25+
import Astarte.Generators.Utilities.ParamsGen
26+
27+
alias Astarte.Core.Triggers.SimpleEvents.DeviceConnectedEvent
28+
29+
alias Astarte.Common.Generators.Ip, as: IpGenerator
30+
alias Astarte.Utilities.Map, as: MapUtilities
31+
32+
@spec device_connected_event() :: StreamData.t(DeviceConnectedEvent.t())
33+
@spec device_connected_event(keyword :: keyword()) :: StreamData.t(DeviceConnectedEvent.t())
34+
def device_connected_event(params \\ []) do
35+
params gen all device_ip_address <- device_ip_address(),
36+
params: params do
37+
%DeviceConnectedEvent{
38+
device_ip_address: device_ip_address
39+
}
40+
end
41+
end
42+
43+
@doc """
44+
Convert this struct/stream to changes
45+
"""
46+
@spec to_changes(DeviceConnectedEvent.t()) :: StreamData.t(map())
47+
def to_changes(data) when not is_struct(data, StreamData),
48+
do: data |> constant() |> to_changes()
49+
50+
@spec to_changes(StreamData.t(DeviceConnectedEvent.t())) :: StreamData.t(map())
51+
def to_changes(gen) do
52+
gen all %DeviceConnectedEvent{
53+
device_ip_address: device_ip_address
54+
} <- gen do
55+
%{
56+
device_ip_address: device_ip_address
57+
}
58+
|> MapUtilities.clean()
59+
end
60+
end
61+
62+
defp device_ip_address,
63+
do:
64+
IpGenerator.ip(:ipv4)
65+
|> map(&Tuple.to_list/1)
66+
|> map(&Enum.join(&1, "."))
67+
end
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#
2+
# This file is part of Astarte.
3+
#
4+
# Copyright 2025 SECO Mind Srl
5+
#
6+
# Licensed under the Apache License, Version 2.0 (the "License");
7+
# you may not use this file except in compliance with the License.
8+
# You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing, software
13+
# distributed under the License is distributed on an "AS IS" BASIS,
14+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
# See the License for the specific language governing permissions and
16+
# limitations under the License.
17+
#
18+
19+
defmodule Astarte.Core.Generators.Triggers.SimpleEvents.DeviceConnectedEventTest do
20+
use ExUnit.Case, async: true
21+
use ExUnitProperties
22+
23+
alias Astarte.Core.Triggers.SimpleEvents.DeviceConnectedEvent
24+
25+
alias Astarte.Core.Generators.Triggers.SimpleEvents.DeviceConnectedEvent,
26+
as: DeviceConnectedEventGenerator
27+
28+
@moduletag :trigger
29+
@moduletag :simple_event
30+
@moduletag :device_connected_event
31+
32+
@doc false
33+
describe "triggers device_connected_event generator" do
34+
@describetag :success
35+
@describetag :ut
36+
property "generates valid device_connected_event" do
37+
check all device_connected_event <- DeviceConnectedEventGenerator.device_connected_event() do
38+
assert %DeviceConnectedEvent{} = device_connected_event
39+
end
40+
end
41+
42+
property "device_connected_event generates valid ip address ipv4" do
43+
check all %DeviceConnectedEvent{
44+
device_ip_address: device_ip_address
45+
} <- DeviceConnectedEventGenerator.device_connected_event(),
46+
ipv4_address = String.to_charlist(device_ip_address) do
47+
assert {:ok, _} = :inet.parse_ipv4_address(ipv4_address)
48+
end
49+
end
50+
51+
property "generates valid changes using to_changes (gen)" do
52+
gen_device_connected_event_changes =
53+
DeviceConnectedEventGenerator.device_connected_event()
54+
|> DeviceConnectedEventGenerator.to_changes()
55+
56+
check all changes <- gen_device_connected_event_changes do
57+
assert is_map(changes)
58+
end
59+
end
60+
61+
property "generates valid changes using to_changes (struct)" do
62+
check all device_connected_event <- DeviceConnectedEventGenerator.device_connected_event(),
63+
changes <- DeviceConnectedEventGenerator.to_changes(device_connected_event) do
64+
assert is_map(changes)
65+
end
66+
end
67+
end
68+
end

0 commit comments

Comments
 (0)