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 Testing {
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 381 : TestServerClusterContext() :
51 381 : mTestContext{
52 : .eventsGenerator = mTestEventsGenerator,
53 : .dataModelChangeListener = mTestDataModelChangeListener,
54 : .actionContext = mNullActionContext,
55 : },
56 381 : mContext{
57 : .provider = mTestProvider,
58 : .storage = mTestStorage,
59 : .attributeStorage = mDefaultAttributePersistenceProvider,
60 381 : .interactionContext = mTestContext,
61 381 : }
62 : {
63 381 : SuccessOrDie(mDefaultAttributePersistenceProvider.Init(&mTestStorage));
64 381 : }
65 :
66 : /// Get a stable pointer to the underlying context
67 310 : app::ServerClusterContext & Get() { return mContext; }
68 :
69 6 : LogOnlyEvents & EventsGenerator() { return mTestEventsGenerator; }
70 19 : TestProviderChangeListener & ChangeListener() { return mTestDataModelChangeListener; }
71 235 : TestPersistentStorageDelegate & StorageDelegate() { return mTestStorage; }
72 51 : app::DefaultAttributePersistenceProvider & AttributePersistenceProvider() { return mDefaultAttributePersistenceProvider; }
73 6 : app::DataModel::InteractionModelContext & ImContext() { return mTestContext; }
74 :
75 : private:
76 : NullActionContext mNullActionContext;
77 : LogOnlyEvents mTestEventsGenerator;
78 : TestProviderChangeListener mTestDataModelChangeListener;
79 : EmptyProvider mTestProvider;
80 : TestPersistentStorageDelegate mTestStorage;
81 : app::DefaultAttributePersistenceProvider mDefaultAttributePersistenceProvider;
82 :
83 : app::DataModel::InteractionModelContext mTestContext;
84 :
85 : app::ServerClusterContext mContext;
86 : };
87 :
88 : } // namespace Testing
89 : } // namespace chip
|