Check devID for owned device
[platform/upstream/iotivity.git] / resource / unittests / OCMQResourceTest.cpp
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
22 #include <OCPlatform.h>
23 #include <OCApi.h>
24 #include <gtest/gtest.h>
25 #include <string>
26 #include <map>
27
28 namespace OCMQResourceTest
29 {
30     using namespace OC;
31
32 #ifdef MQ_SUBSCRIBER
33     void onSubscribe(const HeaderOptions, const OCRepresentation&, const int&, const int&)
34     {
35     }
36
37     void onReqPub(const HeaderOptions&, const OCRepresentation&, const int)
38     {
39     }
40 #endif
41
42 #ifdef MQ_PUBLISHER
43     void onPublish(const HeaderOptions&, const OCRepresentation&, const int)
44     {
45     }
46 #endif
47
48     void foundResource(const int, const std::string, std::shared_ptr<OCResource>)
49     {
50     }
51
52     void createdTopic(const int, const std::string, std::shared_ptr<OCResource>)
53     {
54     }
55
56     //Helper method
57     OCResource::Ptr ConstructResourceObject(std::string host, std::string uri)
58     {
59         OCConnectivityType connectivityType = CT_DEFAULT;
60         std::vector<std::string> types = {"oic.ps"};
61         std::vector<std::string> ifaces = {DEFAULT_INTERFACE};
62
63         auto ret = OCPlatform::constructResourceObject(host, uri,
64                 connectivityType, false, types, ifaces);
65
66         if (!ret)
67         {
68             ADD_FAILURE() << "ConstructResourceObject result was null";
69             return nullptr;
70         }
71
72         return ret;
73     }
74
75     // Message Queue Test
76     TEST(MessageQueueTest, DiscoveryMQTopicsValid)
77     {
78         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
79         EXPECT_TRUE(resource != NULL);
80         QueryParamsMap query = {};
81         EXPECT_EQ(OC_STACK_OK, resource->discoveryMQTopics(query, &foundResource,
82                                                            QualityOfService::LowQos));
83     }
84
85     TEST(MessageQueueTest, CreateMQTopicValid)
86     {
87         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
88         EXPECT_TRUE(resource != NULL);
89         OCRepresentation rep;
90         QueryParamsMap query = {};
91         EXPECT_EQ(OC_STACK_OK, resource->createMQTopic(rep, "/lightTopic", query, &createdTopic,
92                                                        QualityOfService::LowQos));
93     }
94
95 #ifdef MQ_PUBLISHER
96     TEST(MessageQueueTest, PublishMQTopicValid)
97     {
98         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
99         EXPECT_TRUE(resource != NULL);
100         OCRepresentation rep;
101         QueryParamsMap query = {};
102         EXPECT_EQ(OC_STACK_OK, resource->publishMQTopic(rep, query, &onPublish,
103                                                         QualityOfService::LowQos));
104     }
105 #endif
106
107 #ifdef MQ_SUBSCRIBER
108     TEST(MessageQueueTest, SubscribeMQTopicValid)
109     {
110         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
111         EXPECT_TRUE(resource != NULL);
112         QueryParamsMap query = {};
113         EXPECT_EQ(OC_STACK_OK, resource->subscribeMQTopic(ObserveType::Observe, query,
114                                                           &onSubscribe, QualityOfService::LowQos));
115     }
116
117     TEST(MessageQueueTest, RequestMQPublishValid)
118     {
119         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
120         EXPECT_TRUE(resource != NULL);
121         QueryParamsMap query = {};
122         EXPECT_EQ(OC_STACK_OK, resource->requestMQPublish(query, &onReqPub,
123                                                           QualityOfService::LowQos));
124     }
125 #endif
126 }
127