Merge test folder of test branch
[iotivity.git] / test / src / tc / ic / gtest / src / helper / ICResource.cpp
1 /******************************************************************
2 *
3 * Copyright 2017 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 #include "ICHelper.h"
22 #include "ICResource.h"
23 using namespace OC;
24 using namespace std;
25
26 ICResource::ICResource(string uri, vector<string> rt, vector<string> itf)
27 {
28     m_representation.setUri(uri);
29     m_representation.setResourceTypes(rt);
30     m_representation.setResourceInterfaces(itf);
31 }
32
33 string ICResource::getResourceUri()
34 {
35     return m_representation.getUri();
36 }
37
38 vector<string> ICResource::getResourceType()
39 {
40     return m_representation.getResourceTypes();
41 }
42
43 vector<string> ICResource::getInterfaces()
44 {
45     return m_representation.getResourceInterfaces();
46 }
47
48 OCRepresentation ICResource::getRepresentation(void)
49 {
50     m_representation.clearChildren();
51     for (auto it = m_childResources.begin(); it != m_childResources.end(); it++)
52     {
53       m_representation.addChild((*it)->getRepresentation());
54     }
55     return m_representation;
56 }
57
58 OCStackResult ICResource::addChildResource(ICResource  *childResource)
59 {
60     m_childResources.push_back(childResource);
61     return OCPlatform::bindResource(m_handle, childResource->m_handle);
62 }
63
64 OCStackResult ICResource::sendRepresentation(shared_ptr<OCResourceRequest> pRequest)
65 {
66     auto pResponse = make_shared<OC::OCResourceResponse>();
67     pResponse->setRequestHandle(pRequest->getRequestHandle());
68     pResponse->setResourceHandle(pRequest->getResourceHandle());
69
70     QueryParamsMap queryParamsMap = pRequest->getQueryParameters();
71
72     cout << "\t\t\tquery params: \n";
73     for (auto it = queryParamsMap.begin(); it != queryParamsMap.end(); it++)
74     {
75       cout << "\t\t\t\t" << it->first << ":" << it->second << endl;
76     }
77
78     auto findRes = queryParamsMap.find("if");
79
80     if (findRes != queryParamsMap.end())
81     {
82       pResponse->setResourceRepresentation(getRepresentation(), findRes->second);
83     }
84     else
85     {
86       pResponse->setResourceRepresentation(getRepresentation(), DEFAULT_INTERFACE);
87     }
88
89     pResponse->setResponseResult(OC_EH_OK);
90
91     return OCPlatform::sendResponse(pResponse);
92 }
93
94 OCStackResult ICResource::propagate()
95 {
96     if (m_interestedObservers.size() > 0)
97     {
98      shared_ptr<OCResourceResponse> resourceResponse =
99      {
100        make_shared<OCResourceResponse>()
101      };
102      resourceResponse->setResourceRepresentation(getRepresentation(), DEFAULT_INTERFACE);
103
104      return OCPlatform::notifyListOfObservers(m_handle,
105      m_interestedObservers,
106      resourceResponse);
107     }
108
109     return OC_STACK_OK;
110 }