Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief JSON buffer support (Recieve converted buffer)
0003  *
0004  * @file atmisv26.c
0005  */
0006 /* -----------------------------------------------------------------------------
0007  * Enduro/X Middleware Platform for Distributed Transaction Processing
0008  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0009  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0010  * This software is released under one of the following licenses:
0011  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0012  * See LICENSE file for full text.
0013  * -----------------------------------------------------------------------------
0014  * AGPL license:
0015  *
0016  * This program is free software; you can redistribute it and/or modify it under
0017  * the terms of the GNU Affero General Public License, version 3 as published
0018  * by the Free Software Foundation;
0019  *
0020  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0021  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0022  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0023  * for more details.
0024  *
0025  * You should have received a copy of the GNU Affero General Public License along 
0026  * with this program; if not, write to the Free Software Foundation, Inc.,
0027  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0028  *
0029  * -----------------------------------------------------------------------------
0030  * A commercial use license is available from Mavimax, Ltd
0031  * contact@mavimax.com
0032  * -----------------------------------------------------------------------------
0033  */
0034 
0035 #include <stdio.h>
0036 #include <stdlib.h>
0037 #include <ndebug.h>
0038 #include <atmi.h>
0039 #include <ndrstandard.h>
0040 #include <ubf.h>
0041 #include <test.fd.h>
0042 #include <string.h>
0043 #include "test026.h"
0044 
0045 /**
0046  * Receive some json & reply back with json, ok?
0047  */
0048 void TEST26_UBF2JSON(TPSVCINFO *p_svc)
0049 {
0050     int ret = EXSUCCEED;
0051     char *buf = p_svc->data;
0052     char type[16+1]={EXEOS};
0053     
0054     if (EXFAIL==tptypes(buf, type, NULL))
0055     {
0056         NDRX_LOG(log_error, "TESTERROR: TEST26_UBF2JSON cannot "
0057                 "determine buffer type");
0058         EXFAIL_OUT(ret);
0059     }
0060     
0061     if (0!=strcmp(type, "JSON"))
0062     {
0063         NDRX_LOG(log_error, "TESTERROR: Buffer not JSON, but [%s]!", type);
0064         EXFAIL_OUT(ret);
0065     }
0066     
0067     if (0!=strcmp(buf, "{\"T_STRING_FLD\":\"HELLO FROM UBF\",\"T_PTR_FLD\":{\"buftype\":\"UBF\",\"version\":1,\"data\":{\"T_STRING_FLD\":\"HELLO FROM INNER\"}}}"))
0068     {
0069         NDRX_LOG(log_error, "TESTERROR: Invalid request!");
0070         EXFAIL_OUT(ret);
0071     }
0072     
0073     if (NULL== (buf = tprealloc(buf, 1024)))
0074     {
0075         NDRX_LOG(log_error, "TESTERROR: realloc fail!");
0076         EXFAIL_OUT(ret);
0077     }
0078     
0079     strcpy(buf, "{\"T_STRING_FLD\":[\"HELLO FROM UBF\", \"HELLO FROM JSON!\"], "
0080             "\"T_LONG_FLD\":1001,"
0081             "\"T_PTR_FLD\":{\"buftype\":\"UBF\",\"version\":1,\"data\":{\"T_STRING_FLD\":\"HELLO FROM INNER\"}}}");
0082     
0083     NDRX_LOG(log_debug, "Sending buffer: [%s]", buf);
0084     
0085 out:
0086     
0087     tpreturn(EXSUCCEED==ret?TPSUCCESS:TPFAIL, 0, buf, 0L, 0L);
0088     
0089 }
0090 
0091 /**
0092  * Test auto convert from JSON-to-UBF!
0093  */
0094 void TEST26_JSON2UBF(TPSVCINFO *p_svc)
0095 {
0096     int ret = EXSUCCEED;
0097     UBFH *buf = (UBFH *)p_svc->data;
0098     char tmp[1024];
0099     char type[16+1]={EXEOS};
0100     
0101     if (EXFAIL==tptypes((char *)buf, type, NULL))
0102     {
0103         NDRX_LOG(log_error, "TESTERROR: TEST26_JSON2UBF cannot "
0104                 "determine buffer type");
0105         EXFAIL_OUT(ret);
0106     }
0107     
0108     if (0!=strcmp(type, "UBF"))
0109     {
0110         NDRX_LOG(log_error, "TESTERROR: Buffer not UBF buf: %s!", type);
0111         EXFAIL_OUT(ret);
0112     }
0113     
0114     if (EXSUCCEED!=Bget(buf, T_STRING_FLD, 0, tmp, 0L))
0115     {
0116         NDRX_LOG(log_error, "TESTERROR: T_STRING_FLD[0] not set!");
0117         EXFAIL_OUT(ret);
0118     }
0119     
0120     if (0!=strcmp(tmp, "HELLO UBF FROM JSON 1!"))
0121     {
0122         NDRX_LOG(log_error, "TESTERROR: Invalid request value [%s] in T_STRING_FLD[0]",
0123                 tmp);
0124         EXFAIL_OUT(ret);
0125     }
0126     
0127     if (EXSUCCEED!=Bget(buf, T_STRING_FLD, 1, tmp, 0L))
0128     {
0129         NDRX_LOG(log_error, "TESTERROR: T_STRING_FLD[1] not set!");
0130         EXFAIL_OUT(ret);
0131     }
0132     
0133     if (0!=strcmp(tmp, "HELLO UBF FROM JSON 2!"))
0134     {
0135         NDRX_LOG(log_error, "TESTERROR: Invalid request value [%s] in T_STRING_FLD[1]",
0136                 tmp);
0137         EXFAIL_OUT(ret);
0138     }
0139     
0140     if (EXSUCCEED!=Bchg(buf, T_STRING_FLD, 2, "HELLO FROM UBF!", 0L))
0141     {
0142         NDRX_LOG(log_error, "TESTERROR: Failed to set T_LONG_FLD!: %s", Bstrerror(Berror));
0143         EXFAIL_OUT(ret);
0144     }
0145     
0146 out:
0147     
0148     tpreturn(EXSUCCEED==ret?TPSUCCESS:TPFAIL, 0, (char *)buf, 0L, 0L);
0149     
0150 }
0151 
0152 /*
0153  * Do initialization
0154  */
0155 int NDRX_INTEGRA(tpsvrinit)(int argc, char **argv)
0156 {
0157     int ret=EXSUCCEED;
0158     
0159     NDRX_LOG(log_debug, "tpsvrinit called");
0160 
0161     if (EXSUCCEED!=tpadvertise("TEST26_UBF2JSON", TEST26_UBF2JSON))
0162     {
0163         NDRX_LOG(log_error, "Failed to initialize TEST26_UBF2JSON!");
0164         ret=EXFAIL;
0165     }
0166     
0167     if (EXSUCCEED!=tpadvertise("TEST26_JSON2UBF", TEST26_JSON2UBF))
0168     {
0169         NDRX_LOG(log_error, "Failed to initialize TEST26_JSON2UBF!");
0170         ret=EXFAIL;
0171     }
0172     
0173 out:
0174     return ret;
0175 }
0176 
0177 /**
0178  * Do de-initialization
0179  */
0180 void NDRX_INTEGRA(tpsvrdone)(void)
0181 {
0182     NDRX_LOG(log_debug, "tpsvrdone called");
0183 }
0184 
0185 /* vim: set ts=4 sw=4 et smartindent: */