Skip to content

Commit

Permalink
#10178 SetNTP + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
paoloo committed Jul 19, 2024
1 parent 9904ad4 commit 6b9c2f9
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 0 deletions.
58 changes: 58 additions & 0 deletions lib/devices/set_ntp.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
defmodule Onvif.Devices.SetNTP do
import SweetXml
import XmlBuilder

alias Onvif.Device
alias Onvif.Devices.NTP

def soap_action, do: "http://www.onvif.org/ver10/device/wsdl/SetNTP"

@spec request(Device.t(), list) :: {:ok, any} | {:error, map()}
def request(device, args) do
Onvif.Devices.request(device, args, __MODULE__)
end

def request_body(config: %NTP{} = ntp) do
element(:"s:Body", [
element(:"tds:SetNTP", [
element(:"tds:FromDHCP", ntp.from_dhcp),
ntp_manual_element(ntp, ntp.from_dhcp) |> List.flatten()
])
])
end

defp ntp_manual_element(%NTP{} = _ntp, true), do: []

defp ntp_manual_element(%NTP{} = ntp, false) do
[element(:"tds:NTPManual", ntp_add_manual_element(ntp.ntp_manual))]
end

defp ntp_add_manual_element(ntp_manual) do
[
element(:"tt:Type", ntp_manual.type),
ntp_manual_element_data(ntp_manual)
]
end

defp ntp_manual_element_data(ntp_manual) do
case ntp_manual.type do
"IPv4" -> element(:"tt:IPv4Address", ntp_manual.ipv4_address)
"IPv6" -> element(:"tt:IPv6Address", ntp_manual.ipv6_address)
"DNS" -> element(:"tt:DNSname", ntp_manual.dns_name)
end
end

def response(xml_response_body) do
res =
xml_response_body
|> parse(namespace_conformant: true, quiet: true)
|> xpath(
~x"//s:Envelope/s:Body/tds:SetNTPResponse/text()"s
|> add_namespace("s", "http://www.w3.org/2003/05/soap-envelope")
|> add_namespace("tds", "http://www.onvif.org/ver10/device/wsdl")
|> add_namespace("tt", "http://www.onvif.org/ver10/schema")
)

{:ok, res}
end
end
8 changes: 8 additions & 0 deletions test/devices/fixtures/set_ntp_response.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope
xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope"
xmlns:tds="http://www.onvif.org/ver10/device/wsdl">
<soapenv:Body>
<tds:SetNTPResponse/>
</soapenv:Body>
</soapenv:Envelope>
85 changes: 85 additions & 0 deletions test/devices/set_ntp.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
defmodule Onvif.Devices.SetNTPTest do
use ExUnit.Case, async: true

@moduletag capture_log: true

describe "SetNTP/2" do
test "check a dhcp-based ntp" do
xml_response = File.read!("test/devices/fixtures/set_ntp_response.xml")

device = Onvif.Factory.device()

Mimic.expect(Tesla, :request, fn _client, _opts ->
{:ok, %{status: 200, body: xml_response}}
end)

{:ok, service_capabilities} =
Onvif.Devices.SetNTP.request(device, config: %Onvif.Devices.NTP{from_dhcp: true})

assert service_capabilities == ""
end

test "check a manual ntp with IPv4" do
xml_response = File.read!("test/devices/fixtures/set_ntp_response.xml")

device = Onvif.Factory.device()

Mimic.expect(Tesla, :request, fn _client, _opts ->
{:ok, %{status: 200, body: xml_response}}
end)

{:ok, service_capabilities} =
Onvif.Devices.SetNTP.request(device,
config: %Onvif.Devices.NTP{
from_dhcp: false,
ntp_manual: %Onvif.Devices.NTP.NTPManual{type: "IPv4", ipv4_address: "6.6.6.0"}
}
)

assert service_capabilities == ""
end

test "check a manual ntp with IPv6" do
xml_response = File.read!("test/devices/fixtures/set_ntp_response.xml")

device = Onvif.Factory.device()

Mimic.expect(Tesla, :request, fn _client, _opts ->
{:ok, %{status: 200, body: xml_response}}
end)

{:ok, service_capabilities} =
Onvif.Devices.SetNTP.request(device,
config: %Onvif.Devices.NTP{
from_dhcp: false,
ntp_manual: %Onvif.Devices.NTP.NTPManual{
type: "IPv6",
ipv6_address: "2001:0db8:85a3:0000:0000:8a2e:0370:7334"
}
}
)

assert service_capabilities == ""
end

test "check a manual ntp with DNS" do
xml_response = File.read!("test/devices/fixtures/set_ntp_response.xml")

device = Onvif.Factory.device()

Mimic.expect(Tesla, :request, fn _client, _opts ->
{:ok, %{status: 200, body: xml_response}}
end)

{:ok, service_capabilities} =
Onvif.Devices.SetNTP.request(device,
config: %Onvif.Devices.NTP{
from_dhcp: false,
ntp_manual: %Onvif.Devices.NTP.NTPManual{type: "DNS", dns_name: "ntp.example.com"}
}
)

assert service_capabilities == ""
end
end
end

0 comments on commit 6b9c2f9

Please sign in to comment.