2 * Copyright 2016 Samsung Electronics All Rights Reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.iotivity.service.ns.sample.provider;
19 import java.util.Iterator;
20 import java.util.Vector;
21 import java.util.concurrent.CountDownLatch;
23 import android.app.Application;
24 import android.content.Context;
25 import android.test.ApplicationTestCase;
26 import android.util.Log;
28 import org.iotivity.base.ModeType;
29 import org.iotivity.base.OcPlatform;
30 import org.iotivity.base.PlatformConfig;
31 import org.iotivity.base.QualityOfService;
32 import org.iotivity.base.ServiceType;
33 import org.iotivity.service.ns.common.NSException;
34 import org.iotivity.service.ns.common.NSErrorCode;
35 import org.iotivity.service.ns.common.SyncInfo;
36 import org.iotivity.service.ns.common.Topic;
38 import android.support.test.runner.AndroidJUnit4;
40 import org.iotivity.service.ns.consumer.ConsumerService;
41 import org.iotivity.service.ns.provider.Consumer;
42 import org.iotivity.service.ns.provider.ProviderService;
43 import org.junit.After;
44 import org.junit.BeforeClass;
45 import org.junit.Test;
46 import org.junit.Before;
47 import org.junit.runner.RunWith;
49 import java.util.concurrent.TimeUnit;
52 * To work on unit tests, switch the Test Artifact in the Build Variants view.
54 @RunWith(AndroidJUnit4.class)
55 public class ExampleUnitTest extends ApplicationTestCase<Application> {
56 public ExampleUnitTest() {
57 super(Application.class);
60 private static Context mContext = null;
61 private static SubscriptionCallbackListener subCb;
62 private static SyncCallbackListener syncCb;
63 private CountDownLatch lockObject;
64 private Response response;
65 private static ProviderService gProviderRes;
66 private static ConsumerService gConsumerRes;
67 private static ConsumerSimulator gConsumerSim;
68 private static String TAG = "UnitTest ProviderService";
70 public void start(boolean subControllability) {
72 gProviderRes.start(subCb, syncCb, subControllability, "ok", false);
73 gConsumerRes.start(gConsumerSim);
74 lockObject.await(4000, TimeUnit.MILLISECONDS);
75 } catch (Exception e) {
80 public org.iotivity.service.ns.common.Message createMessage() {
81 org.iotivity.service.ns.common.Message msg = null;
83 msg = gProviderRes.createMessage();
84 msg.setTitle("Title");
85 msg.setSourceName("Source");
86 msg.setContentText("ContentText");
87 } catch (Exception e) {
93 public org.iotivity.service.ns.common.Message sendMessage(
95 lockObject = new CountDownLatch(1);
96 response = new Response();
97 org.iotivity.service.ns.common.Message msg = null;
98 gConsumerSim.set(lockObject, response, "msg");
100 subCb.get().acceptSubscription(accepted);
101 CountDownLatch waitLock = new CountDownLatch(1);
102 waitLock.await(400, TimeUnit.MILLISECONDS);
103 msg = createMessage();
104 gProviderRes.sendMessage(msg);
105 lockObject.await(4000, TimeUnit.MILLISECONDS);
106 } catch (Exception e) {
113 public static void Initialize() {
114 PlatformConfig platformConfig = new PlatformConfig(mContext,
115 ServiceType.IN_PROC, ModeType.CLIENT_SERVER, "0.0.0.0",
116 0, // Uses randomly available port
117 QualityOfService.LOW);
119 OcPlatform.Configure(platformConfig);
121 OcPlatform.stopPresence(); // Initialize OcPlatform
122 } catch (Exception e) {
128 public void SetUp() {
129 Log.i(TAG, "SetUp - IN");
130 subCb = new SubscriptionCallbackListener();
131 syncCb = new SyncCallbackListener();
132 gConsumerRes = ConsumerService.getInstance();
133 gProviderRes = ProviderService.getInstance();
134 gConsumerSim = new ConsumerSimulator();
135 lockObject = new CountDownLatch(1);
136 response = new Response();
137 subCb.set(lockObject, response);
138 Log.i(TAG, "SetUp - OUT");
142 public void TearDown() {
143 Log.i(TAG, "TearDown - IN");
147 lockObject = new CountDownLatch(1);
148 lockObject.await(2000, TimeUnit.MILLISECONDS);
149 } catch (Exception e) {
152 Log.i(TAG, "TearDown - OUT");
156 public void StartProviderPositiveWithPolicyTrue() {
158 gProviderRes.start(subCb, syncCb, true, "ok", false);
160 } catch (NSException e) {
162 assertEquals(NSErrorCode.OK, e.getErrorCode());
167 public void StartProviderPositiveWithPolicyFalse() {
169 gProviderRes.start(subCb, syncCb, false, "ok", false);
171 } catch (NSException e) {
173 assertEquals(NSErrorCode.OK, e.getErrorCode());
178 public void StopProviderPositive() {
180 gProviderRes.start(subCb, syncCb, true, "ok", false);
182 } catch (NSException e) {
184 assertEquals(NSErrorCode.OK, e.getErrorCode());
189 public void ExpectCallbackWhenReceiveSubscribeRequestWithAccepterProvider() {
191 assertEquals(true, response.get());
195 public void NeverCallNotifyOnConsumerByAcceptIsFalse() {
197 assertEquals(true, response.get());
199 assertEquals(false, response.get());
203 public void ExpectCallNotifyOnConsumerByAcceptIsTrue() {
206 assertEquals(true, response.get());
208 assertEquals(true, response.get());
212 public void ExpectCallbackSyncOnReadToConsumer() {
214 assertEquals(true, response.get());
215 org.iotivity.service.ns.common.Message msg = sendMessage(true);
216 assertEquals(true, response.get());
218 lockObject = new CountDownLatch(1);
219 response = new Response();
220 gConsumerSim.set(lockObject, response, "sync");
221 gProviderRes.sendSyncInfo(msg.getMessageId(),
222 SyncInfo.SyncType.READ);
223 lockObject.await(4000, TimeUnit.MILLISECONDS);
224 } catch (Exception e) {
227 assertEquals(true, response.get());
231 public void ExpectCallbackSyncOnReadFromConsumer() {
234 assertEquals(true, response.get());
235 org.iotivity.service.ns.common.Message msg = sendMessage(true);
236 assertEquals(true, response.get());
238 lockObject = new CountDownLatch(1);
239 response = new Response();
240 syncCb.set(lockObject, response);
241 gConsumerSim.sendSyncInfo(msg.getMessageId(),
242 SyncInfo.SyncType.READ);
243 lockObject.await(4000, TimeUnit.MILLISECONDS);
244 } catch (Exception e) {
250 public void ExpectEqualAddedTopicsAndRegisteredTopics() {
251 boolean isSame = true;
252 String str[] = { "TOPIC1", "TOPIC2" };
254 gProviderRes.start(subCb, syncCb, true, "ok", false);
255 gProviderRes.registerTopic(str[0]);
256 gProviderRes.registerTopic(str[1]);
257 Vector<Topic> list = gProviderRes.getRegisteredTopicList()
259 Iterator<Topic> it = list.iterator();
260 if (list.size() != 2) {
262 Log.i(TAG, "Size is " + list.size());
265 while (it.hasNext()) {
266 Topic element = it.next();
267 Log.i(TAG, "element is " + element.getTopicName());
268 if (!str[count].equals(element.getTopicName())) {
274 } catch (Exception e) {
277 assertEquals(true, isSame);
281 public void ExpectEqualUnregisteredTopicsAndRegisteredTopics() {
282 boolean isSame = true;
283 String str[] = { "TOPIC1", "TOPIC2" };
285 gProviderRes.start(subCb, syncCb, true, "ok", false);
286 gProviderRes.registerTopic(str[0]);
287 gProviderRes.registerTopic(str[1]);
288 gProviderRes.unregisterTopic(str[0]);
289 Vector<Topic> list = gProviderRes.getRegisteredTopicList()
291 Iterator<Topic> it = list.iterator();
292 if (list.size() != 1) {
294 Log.i(TAG, "Size is " + list.size());
297 while (it.hasNext()) {
298 Topic element = it.next();
299 Log.i(TAG, "element is " + element.getTopicName());
300 if (!str[count].equals(element.getTopicName())) {
305 } catch (Exception e) {
308 assertEquals(true, isSame);
312 public void ExpectEqualSetConsumerTopicsAndGetConsumerTopics() {
313 boolean isSame = false;
315 assertEquals(true, response.get());
316 String str[] = { "TOPIC1", "TOPIC2" };
318 gProviderRes.registerTopic(str[0]);
319 gProviderRes.registerTopic(str[1]);
320 subCb.get().setTopic(str[0]);
322 Vector<Topic> list = subCb.get().getConsumerTopicList()
324 Iterator<Topic> it = list.iterator();
326 String[] compStr = new String[10];
327 Topic.TopicState[] state = new Topic.TopicState[10];
328 while (it.hasNext()) {
329 Topic element = it.next();
330 Log.i(TAG, "element is " + element.getTopicName());
331 compStr[count] = element.getTopicName();
332 state[count++] = element.getState();
334 if (compStr[0].compareTo(str[0]) == 0
335 && compStr[1].compareTo(str[1]) == 0
336 && state[0] == Topic.TopicState.SUBSCRIBED
337 && state[1] == Topic.TopicState.UNSUBSCRIBED) {
341 } catch (Exception e) {
344 assertEquals(true, isSame);
348 public void ExpectEqualUnSetConsumerTopicsAndGetConsumerTopics() {
349 boolean isSame = false;
351 assertEquals(true, response.get());
352 String str[] = { "TOPIC1", "TOPIC2" };
354 gProviderRes.registerTopic(str[0]);
355 gProviderRes.registerTopic(str[1]);
356 subCb.get().setTopic(str[0]);
357 subCb.get().setTopic(str[1]);
358 subCb.get().unsetTopic(str[0]);
360 Vector<Topic> list = subCb.get().getConsumerTopicList()
362 Iterator<Topic> it = list.iterator();
364 String[] compStr = new String[10];
365 Topic.TopicState[] state = new Topic.TopicState[10];
366 while (it.hasNext()) {
367 Topic element = it.next();
368 Log.i(TAG, "element is " + element.getTopicName());
369 compStr[count] = element.getTopicName();
370 state[count++] = element.getState();
372 if (compStr[0].compareTo(str[0]) == 0
373 && compStr[1].compareTo(str[1]) == 0
374 && state[0] == Topic.TopicState.UNSUBSCRIBED
375 && state[1] == Topic.TopicState.SUBSCRIBED) {
379 } catch (Exception e) {
382 assertEquals(true, isSame);
387 class SubscriptionCallbackListener
388 implements ProviderService.OnConsumerSubscribedListener {
389 private CountDownLatch mLockObject;
390 private Response mResponse;
391 private Consumer mConsumer;
392 private static String TAG = "UnitTest SubscriptionCallbackListener";
395 public void onConsumerSubscribed(Consumer consumer) {
396 Log.i(TAG, "onConsumerSubscribed");
397 mConsumer = consumer;
398 if (mResponse != null) {
400 mLockObject.countDown();
404 public void set(CountDownLatch lockObject, Response response) {
405 mLockObject = lockObject;
406 mResponse = response;
409 public Consumer get() {
414 class SyncCallbackListener
415 implements ProviderService.OnMessageSynchronizedListener {
416 private CountDownLatch mLockObject;
417 private Response mResponse;
418 private static String TAG = "UnitTest SyncCallbackListener";
421 public void onMessageSynchronized(SyncInfo syncInfo) {
422 Log.i(TAG, "onMessageSynchronized");
423 if (mResponse != null) {
425 mLockObject.countDown();
429 public void set(CountDownLatch lockObject, Response response) {
430 mLockObject = lockObject;
431 mResponse = response;
436 private boolean state;
442 public void set(boolean val) {
446 public boolean get() {