Hubiquitus Android  0.3
Android client for hubiquitus protocol
HStructureTest.java
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) Novedia Group 2012.
00003  *
00004  *     This file is part of Hubiquitus.
00005  *
00006  *     Hubiquitus is free software: you can redistribute it and/or modify
00007  *     it under the terms of the GNU General Public License as published by
00008  *     the Free Software Foundation, either version 3 of the License, or
00009  *     (at your option) any later version.
00010  *
00011  *     Hubiquitus is distributed in the hope that it will be useful,
00012  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *     GNU General Public License for more details.
00015  *
00016  *     You should have received a copy of the GNU General Public License
00017  *     along with Hubiquitus.  If not, see <http://www.gnu.org/licenses/>.
00018  */
00019 
00020 
00021 package org.hubiquitus.hapi.test;
00022 
00023 import static org.junit.Assert.*;
00024 
00025 import java.util.Calendar;
00026 import org.hubiquitus.hapi.hStructures.ConnectionError;
00027 import org.hubiquitus.hapi.hStructures.ConnectionStatus;
00028 import org.hubiquitus.hapi.hStructures.HAck;
00029 import org.hubiquitus.hapi.hStructures.HAckValue;
00030 import org.hubiquitus.hapi.hStructures.HAlert;
00031 import org.hubiquitus.hapi.hStructures.HCommand;
00032 import org.hubiquitus.hapi.hStructures.HConvState;
00033 import org.hubiquitus.hapi.hStructures.HLocation;
00034 import org.hubiquitus.hapi.hStructures.HMeasure;
00035 import org.hubiquitus.hapi.hStructures.HMessage;
00036 import org.hubiquitus.hapi.hStructures.HMessagePriority;
00037 import org.hubiquitus.hapi.hStructures.HResult;
00038 import org.hubiquitus.hapi.hStructures.HStatus;
00039 import org.hubiquitus.hapi.hStructures.ResultStatus;
00040 import org.hubiquitus.hapi.util.DateISO8601;
00041 import org.hubiquitus.hapi.util.HJsonDictionnary;
00042 import org.json.JSONException;
00043 import org.json.JSONObject;
00044 import org.junit.Assert;
00045 import org.junit.Test;
00046 
00051 public class HStructureTest {
00052          
00053          //HMessage test
00054          @Test
00055          public void HMessageGetTest() {
00056                   JSONObject jsonObj = new JSONObject();
00057                   try {
00058                            String msgid = "msgid:123456789";
00059                            jsonObj.put("msgid", msgid);
00060                            
00061                            String chid = "chid:123456789";
00062                            jsonObj.put("chid", chid);
00063                            
00064                            String convid = "convid:123456789";
00065                            jsonObj.put("convid", convid);
00066                            
00067                            String type = "type:123456";
00068                            jsonObj.put("type", type);
00069                            
00070                            jsonObj.put("priority", 1);
00071                            
00072                            String dateIso = DateISO8601.now();
00073                            Calendar date = DateISO8601.toCalendar(dateIso);
00074                            jsonObj.put("relevance", dateIso);
00075                            
00076                            Boolean _transient = false;
00077                            jsonObj.put("transient", _transient);
00078                            
00079                            HLocation location = new HLocation();
00080                            location.setLat(100);
00081                            location.setLng(100);
00082                            location.setZip("79000");
00083                            jsonObj.put("location",location.toJSON());
00084                            
00085                            String author = "Mysth";
00086                            jsonObj.put("author", author);
00087                            
00088                            String publisher = "j.desousag";
00089                            jsonObj.put("publisher", publisher);
00090                            
00091                            jsonObj.put("published", dateIso);
00092 
00093                            JSONObject headers = new JSONObject();
00094                            headers.put("header1", "1");
00095                            headers.put("header2", "2");
00096                            jsonObj.put("headers", headers);
00097                            
00098                            JSONObject payload = new JSONObject();
00099                            payload.put("payload", "payload");
00100                            HJsonDictionnary payloadResult = new HJsonDictionnary();
00101                            payloadResult.put("payload", "payload");
00102                            jsonObj.put("payload", payload);
00103                            
00104                            HMessage hmessage =  new HMessage(jsonObj);
00105                            
00106                            jsonObj = hmessage.toJSON();
00107                            
00108                            Assert.assertEquals(hmessage.getAuthor(), author);
00109                            Assert.assertEquals(hmessage.getChid(), chid);
00110                            Assert.assertEquals(hmessage.getConvid(), convid);
00111                            Assert.assertEquals(hmessage.getMsgid(), msgid);
00112                            Assert.assertEquals(hmessage.getPublisher(), publisher);
00113                            Assert.assertEquals(hmessage.getType(), type);
00114                            Assert.assertEquals(hmessage.getHeaders().toJSON().toString(), headers.toString());
00115                            Assert.assertEquals(hmessage.getLocation().toString(), location.toString());
00116                            Assert.assertEquals(hmessage.getPayload().toString(), payloadResult.toString());
00117                            Assert.assertEquals(hmessage.getPriority(), HMessagePriority.INFO);
00118                            Assert.assertEquals(hmessage.getPublished(), date);
00119                            Assert.assertEquals(hmessage.getRelevance(), date);
00120                            Assert.assertEquals(hmessage.getTransient(), _transient);
00121                            
00122                   } catch (JSONException e) {
00123                            e.printStackTrace();
00124                            fail("fail");
00125                   }
00126          }
00127          
00128          @Test
00129          public void HMessageSetTest() {
00130                   JSONObject jsonObj = new JSONObject();
00131                   try {
00132                            String msgid = "msgid:123456789";
00133                            
00134                            String chid = "chid:123456789";
00135                            
00136                            String convid = "convid:123456789";
00137                            
00138                            String type = "type:123456";
00139                            
00140                            HMessagePriority priority = HMessagePriority.INFO;
00141                            
00142                            String dateIso = DateISO8601.now();
00143                            Calendar date = DateISO8601.toCalendar(dateIso);
00144                            
00145                            Boolean _transient = false;
00146                            
00147                            HLocation location = new HLocation();
00148                            location.setLat(100);
00149                            location.setLng(100);
00150                            location.setZip("79000");
00151                            
00152                            String author = "Mysth";
00153                            
00154                            String publisher = "j.desousag";
00155 
00156                            HJsonDictionnary headers = new HJsonDictionnary();
00157                            headers.put("header1", "1");
00158                            headers.put("header2", "2");
00159                            
00160                            HJsonDictionnary payload = new HJsonDictionnary();
00161                            payload.put("payload", "payload");
00162                            
00163                            HMessage hmessage =  new HMessage();
00164                            hmessage.setAuthor(author);
00165                            hmessage.setChid(chid);
00166                            hmessage.setConvid(convid);
00167                            hmessage.setHeaders(headers);
00168                            hmessage.setLocation(location);
00169                            hmessage.setMsgid(msgid);
00170                            hmessage.setPayload(payload);
00171                            hmessage.setPriority(priority);
00172                            hmessage.setPublished(date);
00173                            hmessage.setPublisher(publisher);
00174                            hmessage.setRelevance(date);
00175                            hmessage.setTransient(_transient);
00176                            hmessage.setType(type);
00177                            
00178                            jsonObj = hmessage.toJSON();
00179                            
00180                            Assert.assertEquals(jsonObj.get("author"), author);
00181                            Assert.assertEquals(jsonObj.get("chid"), chid);
00182                            Assert.assertEquals(jsonObj.get("convid"), convid);
00183                            Assert.assertEquals(jsonObj.get("msgid"), msgid);
00184                            Assert.assertEquals(jsonObj.get("publisher"), publisher);
00185                            Assert.assertEquals(jsonObj.get("type"), type);
00186                            Assert.assertEquals(jsonObj.get("headers").toString(), headers.toJSON().toString());
00187                            Assert.assertEquals(jsonObj.get("location").toString(), location.toString());
00188                            Assert.assertEquals(jsonObj.get("payload"), payload.toJSON());
00189                            Assert.assertEquals(jsonObj.get("priority"), priority.value());
00190                            Assert.assertEquals(jsonObj.get("published"), dateIso);
00191                            Assert.assertEquals(jsonObj.get("relevance"), dateIso);
00192                            Assert.assertEquals(jsonObj.get("transient"), _transient);
00193                   } catch (JSONException e) {
00194                            e.printStackTrace();
00195                            fail("fail");
00196                   }
00197          }
00198          
00199          //HConv test
00200          @Test
00201          public void HConvStateGetTest() {
00202                   JSONObject jsonObj = new JSONObject();
00203                   try {
00204                            String status = "test";
00205                            jsonObj.put("status", status);
00206                            
00207                            HConvState hconvstate = new HConvState(jsonObj);
00208                            Assert.assertEquals(hconvstate.getStatus(), status);
00209                   } catch (JSONException e) {
00210                            e.printStackTrace();
00211                            fail("fail");
00212                   }
00213          }
00214          
00215          @Test
00216          public void HConvStateSetTest() {
00217                   JSONObject jsonObj = new JSONObject();
00218                   try {
00219                            String status = "test";
00220                            
00221                            HConvState hconvstate = new HConvState();
00222                            hconvstate.setStatus(status);
00223                            
00224                            jsonObj = hconvstate.toJSON();
00225                            
00226                            Assert.assertEquals(jsonObj.get("status"), status);
00227                   } catch (JSONException e) {
00228                            e.printStackTrace();
00229                            fail("fail");
00230                   }
00231          }
00232          
00233          //HLocation test
00234          @Test
00235          public void HlocationGetTest() {
00236                   JSONObject jsonObj = new JSONObject();
00237                   try {
00238                            Double longitude = 100.0;
00239                            jsonObj.put("lng", longitude);
00240                            
00241                            Double latitude = 100.0;
00242                            jsonObj.put("lat", latitude);
00243                            
00244                            String zip = "79000";
00245                            jsonObj.put("zip", zip);
00246                            
00247                            String address = "48 rue des Anges";
00248                            jsonObj.put("addr", address);
00249                            
00250                            String country = "France";
00251                            jsonObj.put("country", country);
00252                            
00253                            String city = "Paris";
00254                            jsonObj.put("city", city);
00255                            
00256                            HJsonDictionnary extra = new HJsonDictionnary();
00257                            extra.put("test", "temp");
00258                            
00259                            HLocation hlocation = new HLocation(jsonObj);
00260 
00261                            Assert.assertEquals(hlocation.getLng(), longitude, 0);
00262                            Assert.assertEquals(hlocation.getLat(), latitude, 0);
00263                            Assert.assertEquals(hlocation.getZip(), zip);
00264                            Assert.assertEquals(hlocation.getAddress(), address);
00265                            Assert.assertEquals(hlocation.getCountry(), country);
00266                            Assert.assertEquals(hlocation.getCity(), city);
00267                   } catch (JSONException e) {
00268                            e.printStackTrace();
00269                            fail("fail");
00270                   }
00271          }
00272          
00273          @Test
00274          public void HlocationSetTest() {
00275                   JSONObject jsonObj = new JSONObject();
00276                   try {
00277                            Double longitude = 100.0;
00278                            
00279                            Double latitude = 100.0;
00280                            
00281                            String zip = "79000";
00282                            
00283                            String address = "48 rue des Anges";
00284                            
00285                            String country = "France";
00286                            
00287                            String city = "Paris";
00288                            
00289                            HJsonDictionnary extra = new HJsonDictionnary();
00290                            extra.put("test", "temp");
00291                            
00292                            HLocation hlocation = new HLocation();
00293                            hlocation.setAddress(address);
00294                            hlocation.setCity(city);
00295                            hlocation.setCountry(country);
00296                            hlocation.setLat(latitude);
00297                            hlocation.setLng(longitude);
00298                            hlocation.setZip(zip);
00299                            jsonObj = hlocation.toJSON();
00300                            
00301                            Assert.assertEquals(jsonObj.getDouble("lng"), longitude, 0);
00302                            Assert.assertEquals(jsonObj.getDouble("lat"), latitude, 0);
00303                            Assert.assertEquals(jsonObj.get("zip"), zip);
00304                            Assert.assertEquals(jsonObj.get("addr"), address);
00305                            Assert.assertEquals(jsonObj.get("country"), country);
00306                            Assert.assertEquals(jsonObj.get("city"), city);
00307                   } catch (JSONException e) {
00308                            e.printStackTrace();
00309                            fail("fail");
00310                   }
00311          }
00312          
00313          //HAck test
00314          @Test
00315          public void HAckSetTest() {
00316                   JSONObject jsonObj = new JSONObject();
00317                   try {
00318                            String ack = "read";
00319                            jsonObj.put("ack", ack);
00320                            
00321                            String ackid = "ackid:123456789";
00322                            jsonObj.put("ackid", ackid);
00323                                                       
00324                            HAck hack = new HAck(jsonObj);
00325 
00326                            Assert.assertEquals(hack.getAck(), HAckValue.READ);
00327                            Assert.assertEquals(hack.getAckid(), ackid);
00328                   } catch (JSONException e) {
00329                            e.printStackTrace();
00330                            fail("fail");
00331                   }
00332          }
00333          
00334          @Test
00335          public void HAckGetTest() {
00336                   JSONObject jsonObj = new JSONObject();
00337                   try {
00338                            HAckValue ack = HAckValue.READ;                       
00339                            String ackid = "ackid:123456789";
00340                                                       
00341                            HAck hack = new HAck();
00342                            hack.setAck(ack);
00343                            hack.setAckid(ackid);
00344                            
00345                            jsonObj = hack.toJSON();
00346                            Assert.assertEquals(jsonObj.get("ack"), ack.value());
00347                            Assert.assertEquals(jsonObj.get("ackid"), ackid);
00348                   } catch (JSONException e) {
00349                            e.printStackTrace();
00350                            fail("fail");
00351                   }
00352          }
00353          
00354          //HCommand test
00355          @Test
00356          public void HCommandGetTest() {
00357                   JSONObject jsonObj = new JSONObject();
00358                   try {
00359                            String cmd = "publish";
00360                            jsonObj.put("cmd", cmd);
00361                            
00362                            String entity = "me";
00363                            jsonObj.put("entity", entity);
00364                            
00365                            String reqid = "reqid:123456789";
00366                            jsonObj.put("reqid", reqid);
00367                            
00368                            String requester = "you";
00369                            jsonObj.put("requester", requester);
00370                            
00371                            String sender = "him";
00372                            jsonObj.put("sender", sender);
00373                            
00374                            String dateIso = DateISO8601.now();
00375                            Calendar date = DateISO8601.toCalendar(dateIso);
00376                            jsonObj.put("sent", dateIso);
00377                            
00378                            HJsonDictionnary params = new HJsonDictionnary();
00379                            params.put("text", "test");
00380                            jsonObj.put("params", params.toJSON());
00381                            
00382                            Boolean _transient = false; 
00383                            jsonObj.put("transient", _transient);
00384                            
00385                            HCommand hcommand = new HCommand(jsonObj);
00386 
00387                            Assert.assertEquals(hcommand.getCmd(), cmd);
00388                            Assert.assertEquals(hcommand.getEntity(), entity);
00389                            Assert.assertEquals(hcommand.getReqid(), reqid);
00390                            Assert.assertEquals(hcommand.getRequester(), requester);
00391                            Assert.assertEquals(hcommand.getSender(), sender);
00392                            Assert.assertEquals(hcommand.getSent(), date);
00393                            Assert.assertEquals(hcommand.getParams().toString(), params.toString());
00394                            Assert.assertEquals(hcommand.getTransient(), _transient);
00395                            
00396                   } catch (JSONException e) {
00397                            e.printStackTrace();
00398                            fail("fail");
00399                   }
00400          }
00401          
00402          @Test
00403          public void HCommandSetTest() {
00404                   JSONObject jsonObj = new JSONObject();
00405                   try {
00406                            String cmd = "publish";
00407                            
00408                            String entity = "me";
00409                            
00410                            String reqid = "reqid:123456789";
00411                            
00412                            String requester = "you";
00413                            
00414                            String sender = "him";
00415                            
00416                            String dateIso = DateISO8601.now();
00417                            Calendar date = DateISO8601.toCalendar(dateIso);
00418                            
00419                            Boolean _transient = false; 
00420                            
00421                            HCommand hcommand = new HCommand();
00422                            hcommand.setCmd(cmd);
00423                            hcommand.setEntity(entity);
00424                            hcommand.setReqid(reqid);
00425                            hcommand.setRequester(requester);
00426                            hcommand.setSender(sender);
00427                            hcommand.setSent(date);
00428                            hcommand.setTransient(_transient);
00429                            
00430                            jsonObj = hcommand.toJSON();
00431 
00432                            Assert.assertEquals(jsonObj.get("cmd"), cmd);
00433                            Assert.assertEquals(jsonObj.get("entity"), entity);
00434                            Assert.assertEquals(jsonObj.get("reqid"), reqid);
00435                            Assert.assertEquals(jsonObj.get("requester"), requester);
00436                            Assert.assertEquals(jsonObj.get("sender"), sender);
00437                            Assert.assertEquals(jsonObj.get("sent"), dateIso);
00438                            Assert.assertEquals(jsonObj.get("transient"), _transient);
00439                   } catch (JSONException e) {
00440                            e.printStackTrace();
00441                            fail("fail");
00442                   }
00443          }
00444          
00445          //HAlert test
00446          @Test
00447          public void HAlertSetTest() {
00448                   JSONObject jsonObj = new JSONObject();
00449                   try {
00450                            String alert = "warning";
00451                            jsonObj.put("alert", alert);
00452                                                       
00453                            HAlert halert = new HAlert(jsonObj);
00454 
00455                            Assert.assertEquals(halert.getAlert(), alert);
00456                   } catch (JSONException e) {
00457                            e.printStackTrace();
00458                            fail("fail");
00459                   }
00460          }
00461          
00462          @Test
00463          public void HAlertGetTest() {
00464                   JSONObject jsonObj = new JSONObject();
00465                   try {
00466                            String alert = "warning";
00467                                                       
00468                            HAlert halert = new HAlert();
00469                            halert.setAlert(alert);
00470                            
00471                            jsonObj = halert.toJSON();
00472                            
00473                            Assert.assertEquals(jsonObj.get("alert"), alert);
00474                   } catch (JSONException e) {
00475                            e.printStackTrace();
00476                            fail("fail");
00477                   }
00478          }
00479 
00480          //HMeasure test
00481          @Test
00482          public void HMeasureSetTest() {
00483                   JSONObject jsonObj = new JSONObject();
00484                   try {
00485                            String unit = "metre";
00486                            jsonObj.put("unit", unit);
00487                            
00488                            String value ="17";
00489                            jsonObj.put("value", value);
00490                                                       
00491                            HMeasure hmeasure = new HMeasure(jsonObj);
00492 
00493                            Assert.assertEquals(hmeasure.getUnit(), unit);
00494                            Assert.assertEquals(hmeasure.getValue(), value);
00495                   } catch (JSONException e) {
00496                            e.printStackTrace();
00497                            fail("fail");
00498                   }
00499          }
00500          
00501          @Test
00502          public void HMeasureGetTest() {
00503                   JSONObject jsonObj = new JSONObject();
00504                   try {
00505                            String unit = "metre";
00506                            
00507                            String value ="17";
00508                                                       
00509                            HMeasure hmeasure = new HMeasure();
00510                            hmeasure.setUnit(unit);
00511                            hmeasure.setValue(value);
00512                            jsonObj = hmeasure.toJSON();
00513 
00514                            Assert.assertEquals(jsonObj.get("unit"), unit);
00515                            Assert.assertEquals(jsonObj.get("value"), value);
00516                   } catch (JSONException e) {
00517                            e.printStackTrace();
00518                            fail("fail");
00519                   }
00520          }
00521          
00522          //HStatus test
00523          @Test
00524          public void HStatusSetTest() {
00525                   JSONObject jsonObj = new JSONObject();
00526                   try {
00527                            ConnectionStatus status = ConnectionStatus.CONNECTING;
00528                            jsonObj.put("status", status.value());
00529                            
00530                            ConnectionError errorCode = ConnectionError.CONN_PROGRESS;
00531                            jsonObj.put("errorCode", errorCode.value());
00532                            
00533                            String errorMsg ="message d'erreur";
00534                            jsonObj.put("errorMsg", errorMsg);
00535                                                       
00536                            HStatus hstatus = new HStatus(jsonObj);
00537 
00538                            Assert.assertEquals(hstatus.getStatus(), status);
00539                            Assert.assertEquals(hstatus.getErrorCode(), errorCode);
00540                            Assert.assertEquals(hstatus.getErrorMsg(), errorMsg);
00541                   } catch (JSONException e) {
00542                            e.printStackTrace();
00543                            fail("fail");
00544                   }
00545          }
00546          
00547          @Test
00548          public void HStatusGetTest() {
00549                   JSONObject jsonObj = new JSONObject();
00550                   try {
00551                            ConnectionStatus status = ConnectionStatus.CONNECTING;
00552                            
00553                            ConnectionError errorCode = ConnectionError.CONN_PROGRESS;
00554                            
00555                            String errorMsg ="message d'erreur";
00556                                                       
00557                            HStatus hstatus = new HStatus();
00558                            hstatus.setErrorCode(errorCode);
00559                            hstatus.setStatus(status);
00560                            hstatus.setErrorMsg(errorMsg);
00561                            jsonObj = hstatus.toJSON();
00562 
00563                            Assert.assertEquals(jsonObj.get("status"), status.value());
00564                            Assert.assertEquals(jsonObj.get("errorCode"), errorCode.value());
00565                            Assert.assertEquals(jsonObj.get("errorMsg"), errorMsg);
00566                   } catch (JSONException e) {
00567                            e.printStackTrace();
00568                            fail("fail");
00569                   }
00570          }
00571          
00572          //HResult test
00573          @Test
00574          public void HResultSetTest() {
00575                   JSONObject jsonObj = new JSONObject();
00576                   try {
00577                            ResultStatus status = ResultStatus.NO_ERROR;
00578                            jsonObj.put("status", status.value());
00579                            
00580                            String cmd = "hpublish";
00581                            jsonObj.put("cmd", cmd);
00582                            
00583                            String reqid ="reqid:123456789";
00584                            jsonObj.put("reqid", reqid);
00585                            
00586                            HJsonDictionnary result = new HJsonDictionnary();
00587                            result.put("test", "test");
00588                            jsonObj.put("result", result.toJSON());
00589                                                       
00590                            HResult hresult = new HResult(jsonObj);
00591 
00592                            Assert.assertEquals(hresult.getStatus(), status);
00593                            Assert.assertEquals(hresult.getCmd(), cmd);
00594                            Assert.assertEquals(hresult.getReqid(), reqid);
00595                            Assert.assertEquals(hresult.getResult().toString(), result.toString());
00596                   } catch (JSONException e) {
00597                            e.printStackTrace();
00598                            fail("fail");
00599                   }
00600          }
00601          
00602          @Test
00603          public void HResultGetTest() {
00604                   JSONObject jsonObj = new JSONObject();
00605                   try {
00606                            ResultStatus status = ResultStatus.NO_ERROR;
00607                            
00608                            String cmd = "hpublish";
00609                            
00610                            String reqid ="reqid:123456789";
00611                            
00612                            HJsonDictionnary result = new HJsonDictionnary();
00613                            result.put("test", "test");
00614                                                       
00615                            HResult hresult = new HResult();
00616                            hresult.setCmd(cmd);
00617                            hresult.setReqid(reqid);
00618                            hresult.setResult(result);
00619                            hresult.setStatus(status);
00620                            jsonObj = hresult.toJSON();
00621 
00622                            Assert.assertEquals(jsonObj.get("status"), status.value());
00623                            Assert.assertEquals(jsonObj.get("cmd"), cmd);
00624                            Assert.assertEquals(jsonObj.get("reqid"), reqid);
00625                            Assert.assertEquals(jsonObj.get("result"), result.toJSON());
00626                   } catch (JSONException e) {
00627                            e.printStackTrace();
00628                            fail("fail");
00629                   }
00630          }
00631          
00632          
00633          
00634 }
00635 
 All Classes Namespaces Files Functions Variables