Merge "Merge remote-tracking branch 'origin/master' into notification-service" into...
[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 onObserve(const HeaderOptions, const OCRepresentation&, const int&, const int&)
34     {
35     }
36 #endif
37
38 #ifdef MQ_PUBLISHER
39     void onGetPut(const HeaderOptions&, const OCRepresentation& , const int eCode)
40     {
41     }
42 #endif
43
44     void foundResource(std::shared_ptr<OCResource>)
45     {
46     }
47
48     void createdTopic(const HeaderOptions &, const OCRepresentation &, const int,
49                       std::shared_ptr<OCResource>)
50     {
51     }
52
53     //Helper method
54     OCResource::Ptr ConstructResourceObject(std::string host, std::string uri)
55     {
56         OCConnectivityType connectivityType = CT_DEFAULT;
57         std::vector<std::string> types = {"tkss.wk"};
58         std::vector<std::string> ifaces = {DEFAULT_INTERFACE};
59
60         auto ret = OCPlatform::constructResourceObject(host, uri,
61                 connectivityType, false, types, ifaces);
62
63         if (!ret)
64         {
65             ADD_FAILURE() << "ConstructResourceObject result was null";
66             return nullptr;
67         }
68
69         return ret;
70     }
71
72     // Message Queue Test
73     TEST(MessageQueueTest, DiscoveryMQTopicsValid)
74     {
75         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
76         EXPECT_TRUE(resource != NULL);
77         QueryParamsMap query = {};
78         EXPECT_EQ(OC_STACK_OK, resource->discoveryMQTopics(query, &foundResource));
79     }
80
81     TEST(MessageQueueTest, CreateMQTopicValid)
82     {
83         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
84         EXPECT_TRUE(resource != NULL);
85         OCRepresentation rep;
86         QueryParamsMap query = {};
87         EXPECT_EQ(OC_STACK_OK, resource->createMQTopic(rep, "/ps/nweTopic", query, &createdTopic));
88     }
89
90 #ifdef MQ_PUBLISHER
91     TEST(MessageQueueTest, PublishMQTopicValid)
92     {
93         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
94         EXPECT_TRUE(resource != NULL);
95         OCRepresentation rep;
96         QueryParamsMap query = {};
97         EXPECT_EQ(OC_STACK_OK, resource->publishMQTopic(rep, query, &onGetPut));
98     }
99 #endif
100
101 #ifdef MQ_SUBSCRIBER
102     TEST(MessageQueueTest, SubscribeMQTopicValid)
103     {
104         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
105         EXPECT_TRUE(resource != NULL);
106         QueryParamsMap query = {};
107         EXPECT_EQ(OC_STACK_OK, resource->subscribeMQTopic(ObserveType::Observe, query, &onObserve));
108     }
109
110     TEST(MessageQueueTest, RequestMQPublishValid)
111     {
112         OCResource::Ptr resource = ConstructResourceObject("coap://192.168.1.2:5000", "/resource");
113         EXPECT_TRUE(resource != NULL);
114         QueryParamsMap query = {};
115         EXPECT_EQ(OC_STACK_OK, resource->requestMQPublish(query, &onGetPut));
116     }
117 #endif
118 }
119