Line data Source code
1 : /*
2 : * Copyright (c) 2025 Project CHIP Authors
3 : *
4 : * Licensed under the Apache License, Version 2.0 (the "License");
5 : * you may not use this file except in compliance with the License.
6 : * You may obtain a copy of the License at
7 : *
8 : * http://www.apache.org/licenses/LICENSE-2.0
9 : *
10 : * Unless required by applicable law or agreed to in writing, software
11 : * distributed under the License is distributed on an "AS IS" BASIS,
12 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 : * See the License for the specific language governing permissions and
14 : * limitations under the License.
15 : */
16 : #include <app/server-cluster/testing/ValidateGlobalAttributes.h>
17 :
18 : namespace chip::Testing {
19 :
20 70 : bool IsAttributesListEqualTo(app::ServerClusterInterface & cluster,
21 : std::initializer_list<const app::DataModel::AttributeEntry> expected)
22 : {
23 70 : std::vector<app::DataModel::AttributeEntry> attributes(expected.begin(), expected.end());
24 70 : return IsAttributesListEqualTo(cluster, std::move(attributes));
25 70 : }
26 :
27 77 : bool IsAttributesListEqualTo(app::ServerClusterInterface & cluster, const std::vector<app::DataModel::AttributeEntry> & expected)
28 : {
29 77 : VerifyOrDie(cluster.GetPaths().size() == 1);
30 77 : auto path = cluster.GetPaths()[0];
31 77 : ReadOnlyBufferBuilder<app::DataModel::AttributeEntry> attributesBuilder;
32 154 : if (CHIP_ERROR err = cluster.Attributes(path, attributesBuilder); err != CHIP_NO_ERROR)
33 : {
34 0 : ChipLogError(Test, "Failed to get attributes list from cluster. Error: %" CHIP_ERROR_FORMAT, err.Format());
35 0 : return false;
36 : }
37 :
38 77 : ReadOnlyBufferBuilder<app::DataModel::AttributeEntry> expectedBuilder;
39 :
40 77 : SuccessOrDie(expectedBuilder.EnsureAppendCapacity(expected.size()));
41 430 : for (const auto & entry : expected)
42 : {
43 353 : SuccessOrDie(expectedBuilder.Append(entry));
44 : }
45 :
46 77 : SuccessOrDie(expectedBuilder.AppendElements(app::DefaultServerCluster::GlobalAttributes()));
47 :
48 77 : return EqualAttributeSets(attributesBuilder.TakeBuffer(), expectedBuilder.TakeBuffer());
49 77 : }
50 :
51 42 : bool IsAcceptedCommandsListEqualTo(app::ServerClusterInterface & cluster,
52 : std::initializer_list<const app::DataModel::AcceptedCommandEntry> expected)
53 : {
54 42 : VerifyOrDie(cluster.GetPaths().size() == 1);
55 42 : auto path = cluster.GetPaths()[0];
56 42 : ReadOnlyBufferBuilder<app::DataModel::AcceptedCommandEntry> commandsBuilder;
57 84 : if (CHIP_ERROR err = cluster.AcceptedCommands(path, commandsBuilder); err != CHIP_NO_ERROR)
58 : {
59 0 : ChipLogError(Test, "Failed to get accepted commands list from cluster. Error: %" CHIP_ERROR_FORMAT, err.Format());
60 0 : return false;
61 : }
62 :
63 42 : ReadOnlyBufferBuilder<app::DataModel::AcceptedCommandEntry> expectedBuilder;
64 :
65 42 : SuccessOrDie(expectedBuilder.EnsureAppendCapacity(expected.size()));
66 134 : for (const auto & entry : expected)
67 : {
68 92 : SuccessOrDie(expectedBuilder.Append(entry));
69 : }
70 :
71 42 : return EqualAcceptedCommandSets(commandsBuilder.TakeBuffer(), expectedBuilder.TakeBuffer());
72 42 : }
73 :
74 7 : bool IsGeneratedCommandsListEqualTo(app::ServerClusterInterface & cluster, std::initializer_list<const CommandId> expected)
75 : {
76 7 : VerifyOrDie(cluster.GetPaths().size() == 1);
77 7 : auto path = cluster.GetPaths()[0];
78 7 : ReadOnlyBufferBuilder<CommandId> commandsBuilder;
79 14 : if (CHIP_ERROR err = cluster.GeneratedCommands(path, commandsBuilder); err != CHIP_NO_ERROR)
80 : {
81 0 : ChipLogError(Test, "Failed to get generated commands list from cluster. Error: %" CHIP_ERROR_FORMAT, err.Format());
82 0 : return false;
83 : }
84 :
85 7 : ReadOnlyBufferBuilder<CommandId> expectedBuilder;
86 :
87 7 : SuccessOrDie(expectedBuilder.EnsureAppendCapacity(expected.size()));
88 12 : for (const auto & entry : expected)
89 : {
90 5 : SuccessOrDie(expectedBuilder.Append(entry));
91 : }
92 :
93 7 : return EqualGeneratedCommandSets(commandsBuilder.TakeBuffer(), expectedBuilder.TakeBuffer());
94 7 : }
95 :
96 : } // namespace chip::Testing
|