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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Device capabilities
- Device capability `purge_property_compression_format`

## [1.2.0] - 2024-07-01

## [1.2.0-rc.0] - 2024-05-28
Expand Down
43 changes: 43 additions & 0 deletions lib/astarte_core/device/capabilities.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#
# 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.
#
# SPDX-License-Identifier: Apache-2.0
#

defmodule Astarte.Core.Device.Capabilities do
use TypedEctoSchema
import Ecto.Changeset

alias Astarte.Core.Device.Capabilities

@required_fields []

@permitted_fields [:purge_properties_compression_format] ++ @required_fields

@primary_key false
typed_embedded_schema do
field :purge_properties_compression_format, Ecto.Enum,
values: [zlib: 0, plaintext: 1],
default: :zlib
end

def changeset(%Capabilities{} = capabilities, params \\ %{}) do
capabilities
|> cast(params, @permitted_fields)
|> validate_required(@required_fields)
end
end
63 changes: 63 additions & 0 deletions test/astarte_core/device/capabilities_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#
# 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.
#
# SPDX-License-Identifier: Apache-2.0
#

defmodule Astarte.Core.Device.CapabilitiesTest do
use ExUnit.Case

alias Astarte.Core.Device.Capabilities

test "capabilities with :purge_properties_compression_format :zlib" do
params = %{
"purge_properties_compression_format" => "zlib"
}

changeset = Capabilities.changeset(%Capabilities{}, params)

assert %Ecto.Changeset{valid?: true} = changeset

{:ok, capabilities} = Ecto.Changeset.apply_action(changeset, :insert)

assert %Capabilities{purge_properties_compression_format: :zlib} = capabilities
end

test "capabilities with :purge_properties_compression_format :plaintext" do
params = %{
"purge_properties_compression_format" => "plaintext"
}

changeset = Capabilities.changeset(%Capabilities{}, params)

assert %Ecto.Changeset{valid?: true} = changeset

{:ok, capabilities} = Ecto.Changeset.apply_action(changeset, :insert)

assert %Capabilities{purge_properties_compression_format: :plaintext} = capabilities
end

test "capabilities with invalid :purge_properties_compression_format fails" do
params = %{
purge_properties_compression_format: :invalid
}

changeset = Capabilities.changeset(%Capabilities{}, params)

assert %Ecto.Changeset{valid?: false} = changeset
end
end
Loading