Hemant Zope 3be4902444 [C] C generator refactored (#2463)
* New modified model header and body mustache for c client generator

* remove uncrustify from cmake as it is used during code generation, also remove valgrind as it is not used

* add function to encode and decode binary data

* update model mustache

* update api body and header mustache for handling all types of parameters

* update model mustache with variable names and address few more issues to generate working codes

* updated api body and header mustaches with support for various new parameters and fix some issues as per new changes in code flow structure

* update apiClient header and body mustache as per new modifications for handling binary data and few more stuff

* updated samples generated by new modified mustache

* update handling of file and binary data type to binary_t

* update samples with recent commit on master regarding c-generator

* update cmakelist which was ignored by .openapi-generator-ignore, cleanup external folder

* update CMakeList mustache to show how to use compiled libary to compile source files

* update samples with new cmake

* Add comments explaining what each command is doing inshort

* remove freeing of base path as it is not memory allocated

* update samples to free apiclient object when the requirement is over

* add missing cJSON delete to fix memory not freed bugs

* use uncrustify to beautify manual written test code
2019-04-15 11:27:34 +08:00

124 lines
3.5 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "../api/UserAPI.h"
#define USER_ID 1234
#define USER_NAME "example123"
#define FIRST_NAME "Example1"
#define LAST_NAME "Example2Last"
#define LAST_NAME1 "LastName"
#define EMAIL "example@example.com"
#define PASSWORD "thisisexample!123"
#define PHONE "+123456789"
#define USER_STATUS 4
int main() {
// create user test
apiClient_t *apiClient = apiClient_create();
char *username = malloc(strlen(USER_NAME) + 1);
strcpy(username, USER_NAME);
char *firstname = malloc(strlen(FIRST_NAME) + 1);
strcpy(firstname, FIRST_NAME);
char *lastname = malloc(strlen(LAST_NAME) + 1);
strcpy(lastname, LAST_NAME);
char *email = malloc(strlen(EMAIL) + 1);
strcpy(email, EMAIL);
char *password = malloc(strlen(PASSWORD) + 1);
strcpy(password, PASSWORD);
char *phone = malloc(strlen(PHONE) + 1);
strcpy(phone, PHONE);
user_t *newuser = user_create(USER_ID,
username,
firstname,
lastname,
email,
password,
phone,
USER_STATUS);
UserAPI_createUser(apiClient, newuser);
user_free(newuser);
apiClient_free(apiClient);
// get user by name test
apiClient_t *apiClient1 = apiClient_create();
user_t *returnUser = UserAPI_getUserByName(apiClient1, USER_NAME);
cJSON *JSONNODE = user_convertToJSON(returnUser);
char *dataToPrint = cJSON_Print(JSONNODE);
printf("User is: \n%s\n", dataToPrint);
user_free(returnUser);
cJSON_Delete(JSONNODE);
free(dataToPrint);
apiClient_free(apiClient1);
// update user test
{
apiClient_t *apiClient2 = apiClient_create();
char *username1 = malloc(strlen(USER_NAME) + 1);
strcpy(username1, USER_NAME);
char *firstname = malloc(strlen(FIRST_NAME) + 1);
strcpy(firstname, FIRST_NAME);
char *lastname = malloc(strlen(LAST_NAME) + 1);
strcpy(lastname, LAST_NAME);
char *email = malloc(strlen(EMAIL) + 1);
strcpy(email, EMAIL);
char *password = malloc(strlen(PASSWORD) + 1);
strcpy(password, PASSWORD);
char *phone = malloc(strlen(PHONE) + 1);
strcpy(phone, PHONE);
user_t *newuser1 = user_create(USER_ID,
username1,
firstname,
lastname,
email,
password,
phone,
USER_STATUS);
UserAPI_updateUser(apiClient2, username1, newuser1);
user_free(newuser1);
apiClient_free(apiClient2);
}
// login user test
{
char *username1 = malloc(strlen(USER_NAME) + 1);
strcpy(username1, USER_NAME);
char *password = malloc(strlen(PASSWORD) + 1);
strcpy(password, PASSWORD);
apiClient_t *apiClient3 = apiClient_create();
char *loginuserreturn = UserAPI_loginUser(apiClient3,
username1,
password);
printf("Login User: %s\n", loginuserreturn);
free(loginuserreturn);
free(username1);
free(password);
apiClient_free(apiClient3);
}
// logout user test
apiClient_t *apiClient4 = apiClient_create();
UserAPI_logoutUser(apiClient4);
apiClient_free(apiClient4);
// delete user test
apiClient_t *apiClient5 = apiClient_create();
UserAPI_deleteUser(apiClient5, "example123");
apiClient_free(apiClient5);
}