Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / cloud / account / src / test / java / org / iotivity / cloud / testaccountserver / TestAccountServer.java
1 package org.iotivity.cloud.testaccountserver;
2
3 import io.netty.channel.ChannelHandlerContext;
4 import io.netty.channel.SimpleChannelInboundHandler;
5
6 import java.net.InetSocketAddress;
7 import java.nio.charset.StandardCharsets;
8
9 import org.junit.Test;
10 import org.iotivity.cloud.accountserver.Constants;
11 import org.iotivity.cloud.accountserver.resources.AccountResource;
12 import org.iotivity.cloud.accountserver.resources.AuthResource;
13 import org.iotivity.cloud.base.CoapClient;
14 import org.iotivity.cloud.base.CoapServer;
15 import org.iotivity.cloud.base.ResourceManager;
16 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
17 import org.iotivity.cloud.base.protocols.coap.CoapResponse;
18 import org.iotivity.cloud.base.protocols.coap.enums.CoapMethod;
19 import org.iotivity.cloud.util.JSONUtil;
20
21 public class TestAccountServer {
22
23     private AuthResource    authResource    = new AuthResource();
24     private AccountResource accountResource = new AccountResource();
25
26     private CoapServer      coapServer      = null;
27     private CoapClient      coapClient      = null;
28     private static String   sessionCode     = null;
29
30     static class CoapClientHandler extends
31             SimpleChannelInboundHandler<CoapResponse> {
32
33         ChannelHandlerContext connectCtx = null;
34
35         @Override
36         public void channelActive(ChannelHandlerContext ctx) throws Exception {
37             connectCtx = ctx;
38         }
39
40         @Override
41         protected void channelRead0(ChannelHandlerContext arg0,
42                 CoapResponse arg1) throws Exception {
43
44             if (arg1.getTokenString().equals("1111")) {
45
46                 String json = arg1.getPayloadString();
47
48                 sessionCode = JSONUtil.parseJSON(json, "session");
49             }
50
51         }
52     }
53
54     public void startServer() throws Exception {
55
56         coapServer = new CoapServer();
57
58         ResourceManager resourceManager = new ResourceManager();
59         coapServer.addHandler(resourceManager);
60
61         resourceManager.registerResource(new AuthResource());
62         resourceManager.registerResource(new AccountResource());
63
64         coapServer.startServer(new InetSocketAddress(5685));
65     }
66
67     public ChannelHandlerContext startClient() throws Exception {
68
69         coapClient = new CoapClient();
70
71         CoapClientHandler coapHandler = new CoapClientHandler();
72         coapClient.addHandler(coapHandler);
73
74         coapClient.startClient(new InetSocketAddress("127.0.0.1", 5685));
75
76         return coapHandler.connectCtx;
77     }
78
79     @Test
80     public void testHandleRegisterRequest() throws Exception {
81
82         System.out.println("Write your authCode before test by following url.");
83         System.out
84                 .println("https://github.com/login?return_to=%2Flogin%2Foauth%2Fauthorize%3Fclient_id%3Dea9c18f540323b0213d0%26redirect_uri%3Dhttp%253A%252F%252Fwww.example.com%252Foauth_callback%252F");
85
86         String authCode = "7243699de9726d05e74c"; // write your authCode here.
87         String authServer = "github";
88
89         String json = "{\"authcode\":\"" + authCode + "\",\"authprovider\":\""
90                 + authServer + "\"}";
91
92         CoapRequest request = new CoapRequest(CoapMethod.POST);
93         request.setUriPath(Constants.AUTH_URI);
94         request.setUriQuery("reqtype=register");
95         request.setToken("1111".getBytes(StandardCharsets.UTF_8));
96         request.setPayload(json.getBytes(StandardCharsets.UTF_8));
97
98         startServer();
99         ChannelHandlerContext ctx = startClient();
100         authResource.onRequestReceived(ctx, request);
101
102         coapClient.stopClient();
103         coapServer.stopServer();
104     }
105
106     @Test
107     public void testHandlerLoginReqeust() throws Exception {
108
109         String json = "{\"session\":\"" + sessionCode + "\"}";
110
111         CoapRequest request = new CoapRequest(CoapMethod.POST);
112         request.setUriPath(Constants.AUTH_URI);
113         request.setUriQuery("reqtype=login");
114         request.setToken("1234".getBytes(StandardCharsets.UTF_8));
115         request.setPayload(json.getBytes(StandardCharsets.UTF_8));
116
117         startServer();
118         ChannelHandlerContext ctx = startClient();
119         authResource.onRequestReceived(ctx, request);
120
121         coapClient.sendRequest(request);
122
123         coapClient.stopClient();
124         coapServer.stopServer();
125     }
126
127     @Test
128     public void testHandlerPublishReqeust() throws Exception {
129
130         String userId = "eyedglen";
131         String deviceId = "d0001";
132         String json = "{\"userid\":\"" + userId + "\",\"deviceId\":\""
133                 + deviceId + "\"}";
134
135         CoapRequest request = new CoapRequest(CoapMethod.POST);
136         request.setUriPath(Constants.ACCOUNT_URI);
137         request.setUriQuery("reqtype=publish");
138         request.setToken("1234".getBytes(StandardCharsets.UTF_8));
139         request.setPayload(json.getBytes(StandardCharsets.UTF_8));
140
141         startServer();
142         ChannelHandlerContext ctx = startClient();
143         accountResource.onRequestReceived(ctx, request);
144
145         coapClient.stopClient();
146         coapServer.stopServer();
147     }
148
149     @Test
150     public void testHandlerFindReqeust() throws Exception {
151
152         String userId = "eyedglen";
153         String json = "{\"userid\":\"" + userId + "\"}";
154
155         CoapRequest request = new CoapRequest(CoapMethod.GET);
156         request.setUriPath(Constants.ACCOUNT_URI);
157         request.setUriQuery("reqtype=find");
158         request.setToken("1234".getBytes(StandardCharsets.UTF_8));
159         request.setPayload(json.getBytes(StandardCharsets.UTF_8));
160
161         startServer();
162         ChannelHandlerContext ctx = startClient();
163         accountResource.onRequestReceived(ctx, request);
164
165         coapClient.stopClient();
166         coapServer.stopServer();
167     }
168 }