Merge test folder of test branch
[iotivity.git] / test / src / tc / es / junit / src / org / iotivity / test / es / tc / helper / ESEnrolleeHelper.java
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 package org.iotivity.test.es.tc.helper;
22
23 import org.iotivity.base.OcConnectivityType;
24 import org.iotivity.base.OcResource;
25 import org.iotivity.base.OcPlatform;
26 import org.iotivity.base.OcException;
27 import org.iotivity.base.PlatformConfig;
28 import org.iotivity.base.ServiceType;
29 import org.iotivity.service.easysetup.mediator.ESConstants;
30 import org.iotivity.service.easysetup.mediator.EasySetup;
31 import org.iotivity.service.easysetup.mediator.RemoteEnrollee;
32 import org.iotivity.base.ModeType;
33 import org.iotivity.base.QualityOfService;
34 import org.iotivity.base.EntityHandlerResult;
35 import org.iotivity.base.OcResourceRequest;
36 import org.iotivity.base.ResourceProperty;
37 import org.iotivity.base.OcResourceHandle;
38
39 import android.util.Log;
40 import java.util.EnumSet;
41 import java.util.HashMap;
42 import java.util.Map;
43
44 import android.content.Context;
45
46 public class ESEnrolleeHelper {
47     private static final String ALL_INTERFACE_TYPE                 = "0.0.0.0";
48     private static final String EMPTY_STRING                       = "";
49     private static final String FOUND_RESOURCE_IS_INVALID          = "Found resource is invalid";
50     private static final String FAIL_TO_FIND_RESOURCE              = "Fail to find resource";
51     private static final String FAIL_TO_REGISTER_ENROLLEE_RESOURCE = "Fail to register enrollee resource";
52     private static final String INTERFACE_TYPE_OF_THE_RESOURCE     = "Interface Type of the resource : ";
53     private static final String RESOURCE_TYPE_OF_THE_RESOURCE      = "Resource Type of the resource : ";
54     private static final String HOST_ADDRESS_OF_THE_RESOURCE       = "Host address of the resource: ";
55     private static final String URI_OF_THE_RESOURCE                = "URI of the resource: ";
56     private static final String ENROLLEEOUND                       = "Enrolleeound";
57     private static final String RT                                 = "?rt=";
58     Map<String, String>         queryParamsMap                     = new HashMap<String, String>();
59     private static final String TAG                                = "Enrollee Resource Creation: ";
60     private boolean             isResourceFound                    = false;
61     private OcResource          enrolleeResource;
62     private static final String RESOURCE_NAME                      = "/a/light";
63     private static final String BATCH_INTERFACE_TYPE               = OcPlatform.BATCH_INTERFACE;
64     public static final boolean callbackcalled = false;
65
66     public ESEnrolleeHelper() {
67         enrolleeResource = null;
68         isResourceFound = false;
69     }
70
71     private boolean configurePlatform(Context context) {
72         PlatformConfig cfg = new PlatformConfig(context, ServiceType.IN_PROC,
73                 ModeType.CLIENT_SERVER, ALL_INTERFACE_TYPE, 0,
74                 QualityOfService.LOW);
75         OcPlatform.Configure(cfg);
76         return true;
77     }
78
79     private boolean registerEnrolleeResource(String resourceName,
80             String resourceType, String interfaceType) {
81
82         OcPlatform.EntityHandler entityHandler = new OcPlatform.EntityHandler() {
83             @Override
84             public EntityHandlerResult handleEntity(
85                     OcResourceRequest ocResourceRequest) {
86                 return EntityHandlerResult.OK;
87             }
88         };
89
90         OcResourceHandle handler = null;
91
92         try {
93             handler = OcPlatform.registerResource(resourceName, resourceType,
94                     interfaceType, entityHandler,
95                     EnumSet.of(ResourceProperty.DISCOVERABLE));
96         } catch (OcException ex) {
97             Log.d(TAG, ex.getMessage());
98         }
99
100         if (handler == null) {
101             Log.d(TAG, FAIL_TO_REGISTER_ENROLLEE_RESOURCE);
102             return false;
103         }
104
105         return true;
106     }
107
108     OcPlatform.OnResourceFoundListener listener = new OcPlatform.OnResourceFoundListener() {
109
110         @Override
111         public void onFindResourceFailed(Throwable throwable, String s) {
112             Log.d(TAG, FAIL_TO_FIND_RESOURCE);
113             isResourceFound = true;
114         }
115
116         @Override
117         public void onResourceFound(OcResource ocResource) {
118             isResourceFound = true;
119             if (ocResource == null) {
120                 Log.d(TAG, FOUND_RESOURCE_IS_INVALID);
121             } else {
122                 enrolleeResource = ocResource;
123                 Log.d(TAG, ENROLLEEOUND);
124                 Log.d(TAG, URI_OF_THE_RESOURCE + ocResource.getUri());
125                 Log.d(TAG, HOST_ADDRESS_OF_THE_RESOURCE + ocResource.getHost());
126                 Log.d(TAG, RESOURCE_TYPE_OF_THE_RESOURCE
127                         + ocResource.getResourceTypes().get(0));
128                 Log.d(TAG, INTERFACE_TYPE_OF_THE_RESOURCE
129                         + ocResource.getResourceInterfaces().get(1));
130             }
131         }
132
133     };
134
135     private OcResource findEnrolleeResource(String resourceType) {
136
137         String requestUri = OcPlatform.WELL_KNOWN_QUERY + RT + resourceType;
138
139         try {
140             OcPlatform.findResource(EMPTY_STRING, requestUri,
141                     EnumSet.of(OcConnectivityType.CT_DEFAULT), listener);
142             ESUtility.waitUntilEnrolleeResponse(isResourceFound);
143         } catch (OcException ex) {
144             Log.d(TAG, ex.getMessage());
145         }
146         return null;
147     }
148
149     public OcResource findEnrolleeResource(Context context) {
150         String requestUri = OcPlatform.WELL_KNOWN_QUERY + RT
151                 + ESConstants.OC_RSRVD_ES_RES_TYPE_EASYSETUP;
152         configurePlatform(context);
153         try {
154             OcPlatform.findResource(EMPTY_STRING, requestUri,
155                     EnumSet.of(OcConnectivityType.CT_DEFAULT), listener);
156             ESUtility.waitUntilEnrolleeResponse(isResourceFound);
157         } catch (OcException ex) {
158             Log.d(TAG, ex.getMessage());
159         }
160         return enrolleeResource;
161     }
162
163     public OcResource createEnrolleeResource(Context context,
164             String resourceName, String resourceType, String interfaceType) {
165
166         if (configurePlatform(context) && registerEnrolleeResource(resourceName,
167                 resourceType, interfaceType)) {
168             findEnrolleeResource(resourceType);
169         }
170
171         return enrolleeResource;
172     }
173
174     public RemoteEnrollee createRemoteEnrollee(Context context) {
175         OcResource ocResource = createEnrolleeResource(context, RESOURCE_NAME,
176                 ESConstants.OC_RSRVD_ES_RES_TYPE_EASYSETUP, BATCH_INTERFACE_TYPE);
177         if (ocResource == null) {
178             return null;
179         }
180         return EasySetup.getInstance(context).createRemoteEnrollee(ocResource);
181     }
182 }
183