1 package org.iotivity.cloud.testaccountserver;
3 import io.netty.channel.ChannelHandlerContext;
4 import io.netty.channel.SimpleChannelInboundHandler;
6 import java.net.InetSocketAddress;
7 import java.nio.charset.StandardCharsets;
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;
21 public class TestAccountServer {
23 private AuthResource authResource = new AuthResource();
24 private AccountResource accountResource = new AccountResource();
26 private CoapServer coapServer = null;
27 private CoapClient coapClient = null;
28 private static String sessionCode = null;
30 static class CoapClientHandler extends
31 SimpleChannelInboundHandler<CoapResponse> {
33 ChannelHandlerContext connectCtx = null;
36 public void channelActive(ChannelHandlerContext ctx) throws Exception {
41 protected void channelRead0(ChannelHandlerContext arg0,
42 CoapResponse arg1) throws Exception {
44 if (arg1.getTokenString().equals("1111")) {
46 String json = arg1.getPayloadString();
48 sessionCode = JSONUtil.parseJSON(json, "session");
54 public void startServer() throws Exception {
56 coapServer = new CoapServer();
58 ResourceManager resourceManager = new ResourceManager();
59 coapServer.addHandler(resourceManager);
61 resourceManager.registerResource(new AuthResource());
62 resourceManager.registerResource(new AccountResource());
64 coapServer.startServer(new InetSocketAddress(5685));
67 public ChannelHandlerContext startClient() throws Exception {
69 coapClient = new CoapClient();
71 CoapClientHandler coapHandler = new CoapClientHandler();
72 coapClient.addHandler(coapHandler);
74 coapClient.startClient(new InetSocketAddress("127.0.0.1", 5685));
76 return coapHandler.connectCtx;
80 public void testHandleRegisterRequest() throws Exception {
82 System.out.println("Write your authCode before test by following url.");
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");
86 String authCode = "7243699de9726d05e74c"; // write your authCode here.
87 String authServer = "github";
89 String json = "{\"authcode\":\"" + authCode + "\",\"authprovider\":\""
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));
99 ChannelHandlerContext ctx = startClient();
100 authResource.onRequestReceived(ctx, request);
102 coapClient.stopClient();
103 coapServer.stopServer();
107 public void testHandlerLoginReqeust() throws Exception {
109 String json = "{\"session\":\"" + sessionCode + "\"}";
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));
118 ChannelHandlerContext ctx = startClient();
119 authResource.onRequestReceived(ctx, request);
121 coapClient.sendRequest(request);
123 coapClient.stopClient();
124 coapServer.stopServer();
128 public void testHandlerPublishReqeust() throws Exception {
130 String userId = "eyedglen";
131 String deviceId = "d0001";
132 String json = "{\"userid\":\"" + userId + "\",\"deviceId\":\""
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));
142 ChannelHandlerContext ctx = startClient();
143 accountResource.onRequestReceived(ctx, request);
145 coapClient.stopClient();
146 coapServer.stopServer();
150 public void testHandlerFindReqeust() throws Exception {
152 String userId = "eyedglen";
153 String json = "{\"userid\":\"" + userId + "\"}";
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));
162 ChannelHandlerContext ctx = startClient();
163 accountResource.onRequestReceived(ctx, request);
165 coapClient.stopClient();
166 coapServer.stopServer();