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 100 : bool IsAttributesListEqualTo(app::ServerClusterInterface & cluster,
21 : std::initializer_list<const app::DataModel::AttributeEntry> expected)
22 : {
23 100 : std::vector<app::DataModel::AttributeEntry> attributes(expected.begin(), expected.end());
24 100 : return IsAttributesListEqualTo(cluster, std::move(attributes));
25 100 : }
26 :
27 113 : bool IsAttributesListEqualTo(app::ServerClusterInterface & cluster, const std::vector<app::DataModel::AttributeEntry> & expected)
28 : {
29 113 : VerifyOrDie(cluster.GetPaths().size() == 1);
30 113 : auto path = cluster.GetPaths()[0];
31 113 : ReadOnlyBufferBuilder<app::DataModel::AttributeEntry> attributesBuilder;
32 226 : 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 113 : ReadOnlyBufferBuilder<app::DataModel::AttributeEntry> expectedBuilder;
39 :
40 113 : SuccessOrDie(expectedBuilder.EnsureAppendCapacity(expected.size()));
41 809 : for (const auto & entry : expected)
42 : {
43 696 : SuccessOrDie(expectedBuilder.Append(entry));
44 : }
45 :
46 113 : SuccessOrDie(expectedBuilder.AppendElements(app::DefaultServerCluster::GlobalAttributes()));
47 :
48 113 : return EqualAttributeSets(attributesBuilder.TakeBuffer(), expectedBuilder.TakeBuffer());
49 113 : }
50 :
51 59 : bool IsAcceptedCommandsListEqualTo(app::ServerClusterInterface & cluster,
52 : std::initializer_list<const app::DataModel::AcceptedCommandEntry> expected)
53 : {
54 59 : VerifyOrDie(cluster.GetPaths().size() == 1);
55 59 : auto path = cluster.GetPaths()[0];
56 59 : ReadOnlyBufferBuilder<app::DataModel::AcceptedCommandEntry> commandsBuilder;
57 118 : 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 59 : ReadOnlyBufferBuilder<app::DataModel::AcceptedCommandEntry> expectedBuilder;
64 :
65 59 : SuccessOrDie(expectedBuilder.EnsureAppendCapacity(expected.size()));
66 218 : for (const auto & entry : expected)
67 : {
68 159 : SuccessOrDie(expectedBuilder.Append(entry));
69 : }
70 :
71 59 : return EqualAcceptedCommandSets(commandsBuilder.TakeBuffer(), expectedBuilder.TakeBuffer());
72 59 : }
73 :
74 14 : bool IsGeneratedCommandsListEqualTo(app::ServerClusterInterface & cluster, std::initializer_list<const CommandId> expected)
75 : {
76 14 : VerifyOrDie(cluster.GetPaths().size() == 1);
77 14 : auto path = cluster.GetPaths()[0];
78 14 : ReadOnlyBufferBuilder<CommandId> commandsBuilder;
79 28 : 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 14 : ReadOnlyBufferBuilder<CommandId> expectedBuilder;
86 :
87 14 : SuccessOrDie(expectedBuilder.EnsureAppendCapacity(expected.size()));
88 23 : for (const auto & entry : expected)
89 : {
90 9 : SuccessOrDie(expectedBuilder.Append(entry));
91 : }
92 :
93 14 : return EqualGeneratedCommandSets(commandsBuilder.TakeBuffer(), expectedBuilder.TakeBuffer());
94 14 : }
95 :
96 : } // namespace chip::Testing
|