replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / notification / cpp-wrapper / unittest / NSConsumerServiceSimulator.h
1 //******************************************************************
2 //
3 // Copyright 2016 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #ifndef _NS_CONSUMER_SERVICE_SIMULATOR_H_
22 #define _NS_CONSUMER_SERVICE_SIMULATOR_H_
23
24 #include <iostream>
25
26 #include "OCPlatform.h"
27 #include "OCApi.h"
28
29 class NSConsumerSimulator
30 {
31     private:
32         std::function<void(const int &, const std::string &, const std::string &,
33                            const std::string &)> m_messageFunc;
34         std::function<void(const int &, const int &)> m_syncFunc;
35
36         std::shared_ptr<OC::OCResource> m_syncResource;
37         std::shared_ptr<OC::OCResource> m_msgResource;
38         std::shared_ptr<OC::OCResource> m_topicResource;
39
40     public:
41         NSConsumerSimulator()
42             : m_messageFunc(), m_syncFunc(),
43               m_syncResource()
44         {
45         }
46         ~NSConsumerSimulator() = default;
47
48         NSConsumerSimulator(const NSConsumerSimulator &) = delete;
49         NSConsumerSimulator &operator = (const NSConsumerSimulator &) = delete;
50
51         NSConsumerSimulator(NSConsumerSimulator &&) = delete;
52         NSConsumerSimulator &operator = (NSConsumerSimulator &&) = delete;
53
54         void findProvider()
55         {
56             OC::OCPlatform::findResource("", std::string("/oic/res?rt=x.org.iotivity.notification"),
57                                          OCConnectivityType::CT_DEFAULT,
58                                          std::bind(&NSConsumerSimulator::findResultCallback, this, std::placeholders::_1),
59                                          OC::QualityOfService::LowQos);
60         }
61
62         void syncToProvider(int &type, const int &id, const std::string &providerID)
63         {
64             if (m_syncResource == nullptr)
65             {
66                 return;
67             }
68
69             OC::OCRepresentation rep;
70             rep.setValue("x.org.iotivity.ns.providerid", providerID);
71             rep.setValue("x.org.iotivity.ns.messageid", id);
72             rep.setValue("x.org.iotivity.ns.state", type);
73
74             m_syncResource->post(rep, OC::QueryParamsMap(), &onPost, OC::QualityOfService::LowQos);
75         }
76
77         bool cancelObserves()
78         {
79             if ((msgResourceCancelObserve(OC::QualityOfService::HighQos) == OC_STACK_OK) &&
80                 (syncResourceCancelObserve(OC::QualityOfService::HighQos) == OC_STACK_OK))
81             {
82                 return true;
83             }
84             return false;
85         }
86
87         void setCallback(std::function<void(const int &, const std::string &,
88                                             const std::string &, const std::string &)> messageFunc,
89                          const std::function<void(const int &, const int &)> &syncFunc)
90         {
91             m_messageFunc = messageFunc;
92             m_syncFunc = syncFunc;
93         }
94
95     private:
96         static void onPost(const OC::HeaderOptions &/*headerOption*/,
97                            const OC::OCRepresentation & /*rep*/ , const int /*eCode*/)
98         {
99         }
100         void findResultCallback(std::shared_ptr<OC::OCResource> resource)
101         {
102
103
104             if (resource->uri() == "/notification")
105             {
106                 resource->get(OC::QueryParamsMap(),
107                               std::bind(&NSConsumerSimulator::onGet, this,
108                                         std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,
109                                         resource), OC::QualityOfService::LowQos);
110             }
111         }
112         void onGet(const OC::HeaderOptions &/*headerOption*/,
113                    const OC::OCRepresentation & /*rep*/ , const int /*eCode*/,
114                    std::shared_ptr<OC::OCResource> resource)
115         {
116
117             OC::QueryParamsMap map;
118             map.insert(std::pair<std::string, std::string>(std::string("x.org.iotivity.ns.consumerid"),
119                        std::string("123456789012345678901234567890123456")));
120
121             try
122             {
123
124                 std::vector<std::string> rts{"x.org.iotivity.notification"};
125
126                 m_msgResource
127                     = OC::OCPlatform::constructResourceObject(
128                           std::string(resource->host()), std::string(resource->uri() + "/message"),
129                           OCConnectivityType(resource->connectivityType()), true, rts,
130                           std::vector<std::string>(resource->getResourceInterfaces()));
131
132                 m_msgResource->observe(OC::ObserveType::Observe, map,
133                                        std::bind(&NSConsumerSimulator::onObserve, this,
134                                                  std::placeholders::_1, std::placeholders::_2,
135                                                  std::placeholders::_3, std::placeholders::_4, resource),
136                                        OC::QualityOfService::LowQos);
137             }
138             catch (std::exception &e)
139             {
140                 std::cout << "OC::ResoureInitException : " << e.what() << std::endl;
141             }
142             m_syncResource
143                 = OC::OCPlatform::constructResourceObject(resource->host(), resource->uri() + "/sync",
144                         resource->connectivityType(), true, resource->getResourceTypes(),
145                         resource->getResourceInterfaces());
146
147             m_syncResource->observe(OC::ObserveType::Observe, map,
148                                     std::bind(&NSConsumerSimulator::onObserve, this,
149                                               std::placeholders::_1, std::placeholders::_2,
150                                               std::placeholders::_3, std::placeholders::_4, resource),
151                                     OC::QualityOfService::LowQos);
152
153             m_topicResource
154                 = OC::OCPlatform::constructResourceObject(resource->host(), resource->uri() + "/topic",
155                         resource->connectivityType(), true, resource->getResourceTypes(),
156                         resource->getResourceInterfaces());
157         }
158         void onObserve(const OC::HeaderOptions &/*headerOption*/,
159                        const OC::OCRepresentation &rep , const int & /*eCode*/, const int &,
160                        std::shared_ptr<OC::OCResource> )
161         {
162
163             if (rep.getUri() == "/notification/message" && rep.hasAttribute("x.org.iotivity.ns.messageid")
164                 && rep.getValue<int>("x.org.iotivity.ns.messageid") != 1)
165             {
166                 m_messageFunc(int(rep.getValue<int>("x.org.iotivity.ns.messageid")),
167                               std::string(rep.getValueToString("x.org.iotivity.ns.title")),
168                               std::string(rep.getValueToString("x.org.iotivity.ns.contenttext")),
169                               std::string(rep.getValueToString(".x.org.iotivity.ns.source")));
170                 if (rep.getValue<int>("x.org.iotivity.ns.messageid") == 3)
171                 {
172                     m_topicResource->get(OC::QueryParamsMap(),
173                                          std::bind(&NSConsumerSimulator::onTopicGet, this, std::placeholders::_1,
174                                                    std::placeholders::_2, std::placeholders::_3, m_topicResource),
175                                          OC::QualityOfService::LowQos);
176                 }
177             }
178             else if (rep.getUri() == "/notification/sync")
179             {
180                 m_syncFunc(int(rep.getValue<int>("x.org.iotivity.ns.state")), int(rep.getValue<int>("x.org.iotivity.ns.messageid")));
181             }
182         }
183         void onTopicGet(const OC::HeaderOptions &/*headerOption*/,
184                         const OC::OCRepresentation & /*rep*/ , const int /*eCode*/,
185                         std::shared_ptr<OC::OCResource> /*resource*/)
186         {
187         }
188
189         OCStackResult msgResourceCancelObserve(OC::QualityOfService qos)
190         {
191             return m_msgResource->cancelObserve(qos);
192         }
193
194         OCStackResult syncResourceCancelObserve(OC::QualityOfService qos)
195         {
196             return m_syncResource->cancelObserve(qos);
197         }
198 };
199
200
201 #endif //_NS_CONSUMER_SERVICE_SIMULATOR_H_