Merge test folder of test branch
[iotivity.git] / test / src / tc / ic / junit / src / org / iotivity / test / ic / tc / helper / OcAccountManagerAdapter.java
1 /******************************************************************
2 *
3 * Copyright 2018 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.ic.tc.helper;
22
23 import java.util.List;
24
25 import org.iotivity.base.OcAccountManager.OnDeleteListener;
26 import org.iotivity.base.OcAccountManager.OnGetListener;
27 import org.iotivity.base.OcConnectivityType;
28 import org.iotivity.base.OcRepresentation;
29 import org.iotivity.base.OcResource;
30 import org.iotivity.base.OcPlatform;
31 import org.iotivity.base.OcException;
32 import org.iotivity.base.OcAccountManager.OnObserveListener;
33 import org.iotivity.base.PlatformConfig;
34 import org.iotivity.base.ServiceType;
35 import org.iotivity.base.ModeType;
36 import org.iotivity.base.QualityOfService;
37 import org.iotivity.base.EntityHandlerResult;
38 import org.iotivity.base.OcResourceRequest;
39 import org.iotivity.base.ResourceProperty;
40 import org.iotivity.base.OcResourceHandle;
41 import org.iotivity.base.OcAccountManager.OnPostListener;
42 import org.iotivity.base.OcHeaderOption;
43 import org.iotivity.base.OcRepresentation;
44 import org.iotivity.base.ErrorCode;
45
46 import org.iotivity.testcase.IoTivityLog;
47 import org.iotivity.testcase.IoTivityTc;
48 import static org.iotivity.test.ic.tc.helper.ICHelperStaticUtil.*;
49
50 public class OcAccountManagerAdapter implements OnPostListener, OnGetListener,
51         OnDeleteListener, OnObserveListener {
52
53     public static String  sInviteeUuid                  = null;
54     public static String  sGroupId                      = null;
55     public static boolean sIsObserveCompleted           = false;
56     public static boolean sIsonDeleteCompletedCBInvoked = false;
57     public static boolean sIssendInvitationCBInvoked    = false;
58     public static boolean onGetCompletedOk              = false;
59
60     @Override
61     public void onPostCompleted(List<OcHeaderOption> list,
62             OcRepresentation ocRepresentation) {
63         IoTivityLog.i(TAG,
64                 "createGroup or sendInviation or adProertyvalue were successful");
65         sIssendInvitationCBInvoked = true;
66         try {
67             sGroupId = ocRepresentation.getValue("gid");
68             IoTivityLog.d("\tgroupId: ", sGroupId);
69         } catch (OcException e) {
70             IoTivityLog.e(TAG, e.toString());
71         }
72     }
73
74     @Override
75     public void onPostFailed(Throwable throwable) {
76         IoTivityLog.e(TAG,
77                 "Failed to createGroup/sendInovation/add/delete/updatepropertyGroup");
78         sIssendInvitationCBInvoked = false;
79         if (throwable instanceof OcException) {
80             OcException ocEx = (OcException) throwable;
81             IoTivityLog.e(TAG, ocEx.toString());
82             ErrorCode errCode = ocEx.getErrorCode();
83         }
84     }
85
86     @Override
87     public void onDeleteCompleted(List<OcHeaderOption> arg0) {
88         IoTivityLog.i(TAG, "successed to delete operation");
89         sIsonDeleteCompletedCBInvoked = true;
90     }
91
92     @Override
93     public void onDeleteFailed(Throwable arg0) {
94         IoTivityLog.e(TAG, "failed to delete operation");
95         sIsonDeleteCompletedCBInvoked = false;
96     }
97
98     @Override
99     public void onObserveCompleted(List<OcHeaderOption> list,
100             OcRepresentation ocRepresentation, int i) {
101         System.out.println("observeGroup was successful");
102         sIsObserveCompleted = true;
103         try {
104             if (REGISTER == i) {
105                 System.out.println("REGISTER was successful");
106             } else if (DEREGISTER == i) {
107                 System.out.println("DEREGISTER was successful");
108             } else {
109                 String gid = ocRepresentation.getValue("gid");
110                 System.out.println("\tGroupID: " + gid);
111
112                 String gmid = ocRepresentation.getValue("gmid");
113                 System.out.println("\tGroupMasterID: " + gmid);
114
115                 String[] midlist = ocRepresentation.getValue("midlist");
116                 if (midlist == null || midlist.length == 0) {
117                     System.out.println("\tMember List is empty");
118                 } else {
119                     System.out.println("\tMember List(" + midlist.length + ")");
120                     for (String mid : midlist) {
121                         System.out.println("\t : " + mid);
122                     }
123                 }
124
125                 String[] dilist = ocRepresentation.getValue("dilist");
126                 if (dilist == null || dilist.length == 0) {
127                     System.out.println("\tDevice List is empty");
128                 } else {
129                     System.out.println("\tDevice List(" + dilist.length + ")");
130                     for (String di : dilist) {
131                         System.out.println("\t : " + di);
132                     }
133                 }
134             }
135         } catch (OcException e) {
136             e.printStackTrace();
137         }
138     }
139
140     @Override
141     public void onObserveFailed(Throwable throwable) {
142         System.out.println("Failed to observeGroup");
143         sIsObserveCompleted = false;
144         if (throwable instanceof OcException) {
145             OcException ocEx = (OcException) throwable;
146             IoTivityLog.e(TAG, ocEx.toString());
147             ErrorCode errCode = ocEx.getErrorCode();
148             System.out.println("Error code: " + errCode);
149         }
150     }
151
152     @Override
153     public synchronized void onGetCompleted(List<OcHeaderOption> list,
154             OcRepresentation ocRepresentation) {
155         System.out.println("searchUser was successful");
156         onGetCompletedOk = true;
157         IoTivityLog.i(TAG, "onGetCompletedOk value is " + onGetCompletedOk);
158
159     }
160
161     @Override
162     public synchronized void onGetFailed(Throwable throwable) {
163         System.out.println("Failed to searchUser");
164         if (throwable instanceof OcException) {
165             OcException ocEx = (OcException) throwable;
166             IoTivityLog.e(TAG, ocEx.toString());
167             ErrorCode errCode = ocEx.getErrorCode();
168             System.out.println("Error code: " + errCode);
169         }
170     }
171 }