Line data Source code
1 : /*
2 : * Copyright (c) 2025 Project CHIP Authors
3 : * All rights reserved.
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 : #pragma once
18 :
19 : #include <app/data-model-provider/ActionContext.h>
20 : #include <app/data-model-provider/Context.h>
21 : #include <app/data-model-provider/Provider.h>
22 : #include <app/persistence/DefaultAttributePersistenceProvider.h>
23 : #include <app/server-cluster/ServerClusterContext.h>
24 : #include <app/server-cluster/testing/EmptyProvider.h>
25 : #include <app/server-cluster/testing/TestEventGenerator.h>
26 : #include <app/server-cluster/testing/TestProviderChangeListener.h>
27 : #include <lib/support/TestPersistentStorageDelegate.h>
28 : #include <protocols/interaction_model/StatusCode.h>
29 :
30 : namespace chip {
31 : namespace Test {
32 :
33 : /// An action context that does not have a current exchange (just returns nullptr)
34 : class NullActionContext : public app::DataModel::ActionContext
35 : {
36 : public:
37 0 : Messaging::ExchangeContext * CurrentExchange() override { return nullptr; }
38 : };
39 :
40 : /// This is a ServerClusterContext that is initialized with VALID
41 : /// entries that can then be used during testing
42 : ///
43 : /// NOTE:
44 : /// At thist time, `interactionContext::actionContext::CurrentExchange` WILL return nullptr
45 : /// in the existing implementation as the exchange is too heavy of an object
46 : /// to create for testing
47 : class TestServerClusterContext
48 : {
49 : public:
50 9 : TestServerClusterContext() :
51 9 : mContext{
52 : .provider = &mTestProvider,
53 : .storage = &mTestStorage,
54 9 : .interactionContext = &mTestContext,
55 9 : }
56 : {
57 9 : mTestContext.eventsGenerator = &mTestEventsGenerator;
58 9 : mTestContext.dataModelChangeListener = &mTestDataModelChangeListener;
59 9 : mTestContext.actionContext = &mNullActionContext;
60 9 : }
61 :
62 : /// Get a stable pointer to the underlying context
63 1 : app::ServerClusterContext & Get() { return mContext; }
64 :
65 : /// Create a new context bound to this test context
66 5 : app::ServerClusterContext Create()
67 : {
68 5 : mDefaultAttributePersistenceProvider.Init(&mTestStorage);
69 :
70 : return {
71 : .provider = &mTestProvider,
72 : .storage = &mTestStorage,
73 : .attributeStorage = &mDefaultAttributePersistenceProvider,
74 5 : .interactionContext = &mTestContext,
75 :
76 5 : };
77 : };
78 :
79 : LogOnlyEvents & EventsGenerator() { return mTestEventsGenerator; }
80 2 : TestProviderChangeListener & ChangeListener() { return mTestDataModelChangeListener; }
81 4 : TestPersistentStorageDelegate & StorageDelegate() { return mTestStorage; }
82 : app::DefaultAttributePersistenceProvider & AttributePersistenceProvider() { return mDefaultAttributePersistenceProvider; }
83 4 : app::DataModel::InteractionModelContext & ImContext() { return mTestContext; }
84 :
85 : private:
86 : NullActionContext mNullActionContext;
87 : LogOnlyEvents mTestEventsGenerator;
88 : TestProviderChangeListener mTestDataModelChangeListener;
89 : EmptyProvider mTestProvider;
90 : TestPersistentStorageDelegate mTestStorage;
91 : app::DefaultAttributePersistenceProvider mDefaultAttributePersistenceProvider;
92 :
93 : app::DataModel::InteractionModelContext mTestContext;
94 :
95 : app::ServerClusterContext mContext;
96 : };
97 :
98 : } // namespace Test
99 : } // namespace chip
|