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 13 : TestServerClusterContext() :
51 13 : mTestContext{
52 : .eventsGenerator = mTestEventsGenerator,
53 : .dataModelChangeListener = mTestDataModelChangeListener,
54 : .actionContext = mNullActionContext,
55 : },
56 13 : mContext{
57 : .provider = mTestProvider,
58 : .storage = mTestStorage,
59 : .attributeStorage = mDefaultAttributePersistenceProvider,
60 13 : .interactionContext = mTestContext,
61 13 : }
62 : {
63 13 : mDefaultAttributePersistenceProvider.Init(&mTestStorage);
64 13 : }
65 :
66 : /// Get a stable pointer to the underlying context
67 3 : app::ServerClusterContext & Get() { return mContext; }
68 :
69 : /// Create a new context bound to this test context
70 9 : app::ServerClusterContext Create()
71 : {
72 : return {
73 : .provider = mTestProvider,
74 : .storage = mTestStorage,
75 : .attributeStorage = mDefaultAttributePersistenceProvider,
76 9 : .interactionContext = mTestContext,
77 9 : };
78 : };
79 :
80 : LogOnlyEvents & EventsGenerator() { return mTestEventsGenerator; }
81 11 : TestProviderChangeListener & ChangeListener() { return mTestDataModelChangeListener; }
82 4 : TestPersistentStorageDelegate & StorageDelegate() { return mTestStorage; }
83 : app::DefaultAttributePersistenceProvider & AttributePersistenceProvider() { return mDefaultAttributePersistenceProvider; }
84 4 : app::DataModel::InteractionModelContext & ImContext() { return mTestContext; }
85 :
86 : private:
87 : NullActionContext mNullActionContext;
88 : LogOnlyEvents mTestEventsGenerator;
89 : TestProviderChangeListener mTestDataModelChangeListener;
90 : EmptyProvider mTestProvider;
91 : TestPersistentStorageDelegate mTestStorage;
92 : app::DefaultAttributePersistenceProvider mDefaultAttributePersistenceProvider;
93 :
94 : app::DataModel::InteractionModelContext mTestContext;
95 :
96 : app::ServerClusterContext mContext;
97 : };
98 :
99 : } // namespace Test
100 : } // namespace chip
|