Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#
# This file is part of Astarte.
#
# Copyright 2025 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

defmodule Astarte.Core.Generators.Triggers.SimpleEvents.DeviceConnectedEvent do
@moduledoc """
This module provides generators for Astarte Trigger Simple Event DeviceConnectedEvent struct.
"""
use ExUnitProperties

import Astarte.Generators.Utilities.ParamsGen

alias Astarte.Core.Triggers.SimpleEvents.DeviceConnectedEvent

alias Astarte.Common.Generators.Ip, as: IpGenerator
alias Astarte.Utilities.Map, as: MapUtilities

@spec device_connected_event() :: StreamData.t(DeviceConnectedEvent.t())
@spec device_connected_event(keyword :: keyword()) :: StreamData.t(DeviceConnectedEvent.t())
def device_connected_event(params \\ []) do
params gen all device_ip_address <- device_ip_address(),
params: params do
%DeviceConnectedEvent{
device_ip_address: device_ip_address
}
end
end

@doc """
Convert this struct/stream to changes
"""
@spec to_changes(DeviceConnectedEvent.t()) :: StreamData.t(map())
def to_changes(data) when not is_struct(data, StreamData),
do: data |> constant() |> to_changes()

@spec to_changes(StreamData.t(DeviceConnectedEvent.t())) :: StreamData.t(map())
def to_changes(gen) do
gen all %DeviceConnectedEvent{
device_ip_address: device_ip_address
} <- gen do
%{
device_ip_address: device_ip_address
}
|> MapUtilities.clean()
end
end

defp device_ip_address,
do:
IpGenerator.ip(:ipv4)
|> map(&Tuple.to_list/1)
|> map(&Enum.join(&1, "."))
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#
# This file is part of Astarte.
#
# Copyright 2025 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

defmodule Astarte.Core.Generators.Triggers.SimpleEvents.DeviceDisconnectedEvent do
@moduledoc """
This module provides generators for Astarte Trigger Simple Event DeviceDisconnectedEvent struct.
"""
use ExUnitProperties

alias Astarte.Core.Triggers.SimpleEvents.DeviceDisconnectedEvent

alias Astarte.Utilities.Map, as: MapUtilities

@spec device_disconnected_event() :: StreamData.t(DeviceDisconnectedEvent.t())
def device_disconnected_event, do: constant(%DeviceDisconnectedEvent{})

@doc """
Convert this struct/stream to changes
"""
@spec to_changes(DeviceDisconnectedEvent.t()) :: StreamData.t(map())
def to_changes(data) when not is_struct(data, StreamData),
do: data |> constant() |> to_changes()

@spec to_changes(StreamData.t(DeviceDisconnectedEvent.t())) :: StreamData.t(map())
def to_changes(gen) do
gen all device_disconnected_event <- gen do
device_disconnected_event
|> Map.from_struct()
|> MapUtilities.clean()
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#
# This file is part of Astarte.
#
# Copyright 2025 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

defmodule Astarte.Core.Generators.Triggers.SimpleEvents.DeviceErrorEvent do
@moduledoc """
This module provides generators for Astarte Trigger Simple Event DeviceErrorEvent struct.
"""
use ExUnitProperties

import Astarte.Generators.Utilities.ParamsGen

alias Astarte.Core.Triggers.SimpleEvents.DeviceErrorEvent

alias Astarte.Utilities.Map, as: MapUtilities

@spec device_error_event() :: StreamData.t(DeviceErrorEvent.t())
@spec device_error_event(keyword :: keyword()) :: StreamData.t(DeviceErrorEvent.t())
def device_error_event(params \\ []) do
params gen all error_name <- error_name(),
metadata <- metadata(),
params: params do
%DeviceErrorEvent{
error_name: error_name,
metadata: metadata
}
end
end

@doc """
Convert this struct/stream to changes
"""
@spec to_changes(DeviceErrorEvent.t()) :: StreamData.t(map())
def to_changes(data) when not is_struct(data, StreamData),
do: data |> constant() |> to_changes()

@spec to_changes(StreamData.t(DeviceErrorEvent.t())) :: StreamData.t(map())
def to_changes(gen) do
gen all %DeviceErrorEvent{
error_name: error_name,
metadata: metadata
} <- gen do
%{
error_name: error_name,
metadata: metadata
}
|> MapUtilities.clean()
end
end

defp error_name, do: one_of([nil, string(:utf8)])

defp metadata,
do: map_of(string(:utf8), string(:utf8), min_length: 0, max_length: 10)
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#
# This file is part of Astarte.
#
# Copyright 2025 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

defmodule Astarte.Core.Generators.Triggers.SimpleEvents.IncomingDataEvent do
@moduledoc """
This module provides generators for Astarte Trigger Simple Event IncomingDataEvent struct.
"""
use ExUnitProperties

import Astarte.Generators.Utilities.ParamsGen

alias Astarte.Core.Triggers.SimpleEvents.IncomingDataEvent

alias Astarte.Core.Interface

alias Astarte.Core.Generators.Interface, as: InterfaceGenerator
alias Astarte.Core.Generators.Mapping.Value, as: ValueGenerator

@spec incoming_data_event() :: StreamData.t(IncomingDataEvent.t())
@spec incoming_data_event(keyword :: keyword()) :: StreamData.t(IncomingDataEvent.t())
def incoming_data_event(params \\ []) do
gen_fields =
params gen all :interface,
%Interface{name: interface_name} = interface <-
InterfaceGenerator.interface(),
value <- ValueGenerator.value(interface: interface),
params: params do
%{
interface: interface_name,
path: value.path,
bson_value: value.value
}
end

gen_fields
|> bind(fn fields ->
fields
|> Map.new(fn {k, v} -> {k, constant(v)} end)
|> optional_map()
end)
|> map(&struct(IncomingDataEvent, &1))
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#
# This file is part of Astarte.
#
# Copyright 2025 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

defmodule Astarte.Core.Generators.Triggers.SimpleEvents.IncomingIntrospectionEvent do
@moduledoc """
This module provides generators for Astarte Trigger Simple Event IncomingIntrospectionEvent struct.
"""
use ExUnitProperties

import Astarte.Generators.Utilities.ParamsGen

alias Astarte.Core.Interface
alias Astarte.Core.Triggers.SimpleEvents.IncomingIntrospectionEvent
alias Astarte.Core.Triggers.SimpleEvents.InterfaceVersion

alias Astarte.Core.Generators.Interface, as: InterfaceGenerator

@spec incoming_introspection_event() :: StreamData.t(IncomingIntrospectionEvent.t())
@spec incoming_introspection_event(keyword :: keyword()) ::
StreamData.t(IncomingIntrospectionEvent.t())
def incoming_introspection_event(params \\ []) do
params gen all interfaces <- InterfaceGenerator.interface() |> list_of(max_length: 10),
params: params do
introspection_map =
interfaces
|> Enum.map(fn %Interface{
name: name,
major_version: major_version,
minor_version: minor_version
} ->
{name, major_version, minor_version}
end)
|> Map.new(fn {name, major_version, minor_version} ->
{name,
%InterfaceVersion{
major: major_version,
minor: minor_version
}}
end)

%IncomingIntrospectionEvent{
introspection_map: introspection_map
}
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#
# This file is part of Astarte.
#
# Copyright 2025 SECO Mind Srl
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

defmodule Astarte.Core.Generators.Triggers.SimpleEvents.DeviceConnectedEventTest do
use ExUnit.Case, async: true
use ExUnitProperties

alias Astarte.Core.Triggers.SimpleEvents.DeviceConnectedEvent

alias Astarte.Core.Generators.Triggers.SimpleEvents.DeviceConnectedEvent,
as: DeviceConnectedEventGenerator

@moduletag :trigger
@moduletag :simple_event
@moduletag :device_connected_event

@doc false
describe "triggers device_connected_event generator" do
@describetag :success
@describetag :ut
property "generates valid device_connected_event" do
check all device_connected_event <- DeviceConnectedEventGenerator.device_connected_event() do
assert %DeviceConnectedEvent{} = device_connected_event
end
end

property "device_connected_event generates valid ip address ipv4" do
check all %DeviceConnectedEvent{
device_ip_address: device_ip_address
} <- DeviceConnectedEventGenerator.device_connected_event(),
ipv4_address = String.to_charlist(device_ip_address) do
assert {:ok, _} = :inet.parse_ipv4_address(ipv4_address)
end
end

property "generates valid changes using to_changes (gen)" do
gen_device_connected_event_changes =
DeviceConnectedEventGenerator.device_connected_event()
|> DeviceConnectedEventGenerator.to_changes()

check all changes <- gen_device_connected_event_changes do
assert is_map(changes)
end
end

property "generates valid changes using to_changes (struct)" do
check all device_connected_event <- DeviceConnectedEventGenerator.device_connected_event(),
changes <- DeviceConnectedEventGenerator.to_changes(device_connected_event) do
assert is_map(changes)
end
end
end
end
Loading
Loading