4b062462f1a37a605ef2db964a5e709548acc2f6
[iotivity.git] / cloud / resourcedirectory / src / test / java / org / iotivity / cloud / testrdserver / RDServerTestUtils.java
1 package org.iotivity.cloud.testrdserver;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5
6 import org.iotivity.cloud.base.protocols.IRequest;
7 import org.iotivity.cloud.base.protocols.MessageBuilder;
8 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
9 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
10 import org.iotivity.cloud.rdserver.Constants;
11 import org.iotivity.cloud.rdserver.db.MongoDB;
12 import org.iotivity.cloud.util.Cbor;
13
14 public class RDServerTestUtils {
15     public static final String DI                 = "98f7483c-5a31-4161-ba7e-9c13e0d";
16     public static final String RD_REQ_URI         = Constants.RD_FULL_URI;
17     public static final String DISCOVERY_REQ_URI  = Constants.WELL_KNOWN_FULL_URI;
18     public static final String DEVICE_PRS_REQ_URI = Constants.DEVICE_PRESENCE_FULL_URI;
19     public static final String RES_PRS_URI        = Constants.RESOURCE_PRESENCE_FULL_URI;
20
21     public static IRequest makePublishRequest() throws Exception {
22
23         HashMap<Object, Object> payload = new HashMap<>();
24         payload.put(Constants.DEVICE_ID, DI);
25         ArrayList<HashMap<Object, Object>> publishLinks = new ArrayList<>();
26         HashMap<Object, Object> link = new HashMap<>();
27         link.put(Constants.HREF, "/a/light");
28         ArrayList<String> rt = new ArrayList<String>();
29         rt.add("core.light");
30         ArrayList<String> itf = new ArrayList<String>();
31         itf.add("oic.if.baseline");
32         HashMap<String, Object> policy = new HashMap<>();
33         policy.put(Constants.BITMAP, 5);
34         link.put(Constants.RESOURCE_TYPE, rt);
35         link.put(Constants.INTERFACE, itf);
36         link.put(Constants.POLICY, policy);
37         link.put(Constants.INS, 0);
38         link.put(Constants.RESOURCE_TTL, 3000);
39         publishLinks.add(link);
40         payload.put(Constants.LINKS, publishLinks);
41         Cbor<HashMap<Object, Object>> cbor = new Cbor<>();
42         IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
43                 Constants.RD_URI,
44                 Constants.RESOURCE_TYPE + "="
45                         + Constants.RESOURCE_TYPE_RDPUBLISH,
46                 ContentFormat.APPLICATION_CBOR,
47                 cbor.encodingPayloadToCbor(payload));
48         return request;
49     }
50
51     public static void resetRDDatabase() throws Exception {
52         MongoDB mongoDB = new MongoDB(Constants.RD_DB_NAME);
53         mongoDB.createTable(Constants.RD_TABLE);
54         mongoDB.createTable(Constants.PRESENCE_TABLE);
55     }
56 }