Search in sources :

Example 6 with TestFileUtilsImpl

use of com.spectralogic.ds3autogen.utils.TestFileUtilsImpl in project ds3_autogen by SpectraLogic.

the class CCodeGeneratorSpectraS3Requests_Test method testGenerateSpectraS3GetBucketRequestPrototype.

@Test
public void testGenerateSpectraS3GetBucketRequestPrototype() throws IOException, TemplateModelException {
    final TestFileUtilsImpl fileUtils = new TestFileUtilsImpl();
    final Map<String, Object> testMap = new HashMap<>();
    final String responseType = StructHelper.getResponseTypeName("ListBucketResult");
    final Request requestEntry = new Request("get_bucket_request", Classification.spectrads3, HttpVerb.GET, "\"/_rest_/\", bucket_name, NULL", null, null, RequestConverter.getParamList(responseType, new Ds3DocSpecEmptyImpl()), ImmutableList.of(), ImmutableList.of(), true, false, null, responseType, false, null);
    testMap.put("requestEntry", requestEntry);
    final CCodeGenerator codeGenerator = new CCodeGenerator();
    codeGenerator.processTemplate(testMap, "header-templates/RequestPrototype.ftl", fileUtils.getOutputStream());
    final ByteArrayOutputStream bstream = (ByteArrayOutputStream) fileUtils.getOutputStream();
    final String output = new String(bstream.toByteArray());
    final String expectedOutput = "LIBRARY_API ds3_error* ds3_get_bucket_request(const ds3_client* client, const ds3_request* request, ds3_list_bucket_result_response** response);";
    assertEquals(expectedOutput, output);
}
Also used : TestFileUtilsImpl(com.spectralogic.ds3autogen.utils.TestFileUtilsImpl) HashMap(java.util.HashMap) Request(com.spectralogic.ds3autogen.c.models.Request) Ds3DocSpecEmptyImpl(com.spectralogic.ds3autogen.docspec.Ds3DocSpecEmptyImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 7 with TestFileUtilsImpl

use of com.spectralogic.ds3autogen.utils.TestFileUtilsImpl in project ds3_autogen by SpectraLogic.

the class CCodeGenerator_Test method testParserUnique.

@Test
public void testParserUnique() throws IOException, ParseException, TemplateModelException {
    final String inputSpecFile = "/input/ComplexTypedefStruct.xml";
    final TestFileUtilsImpl fileUtils = new TestFileUtilsImpl();
    final Ds3SpecParser parser = new Ds3SpecParserImpl();
    final Ds3ApiSpec spec = parser.getSpec(CCodeGenerator_Test.class.getResourceAsStream(inputSpecFile));
    final ImmutableList<Request> allRequests = CCodeGenerator.getAllRequests(spec, new Ds3DocSpecEmptyImpl());
    final ImmutableList<Enum> allEnums = CCodeGenerator.getAllEnums(spec);
    final ImmutableSet<String> enumNames = EnumHelper.getEnumNamesSet(allEnums);
    final ImmutableSet<String> arrayMemberTypes = CCodeGenerator.getArrayMemberTypes(spec, enumNames);
    final ImmutableSet<String> responseTypes = RequestHelper.getResponseTypes(allRequests);
    final ImmutableList<Struct> allStructs = CCodeGenerator.getAllStructs(spec, enumNames, responseTypes, ImmutableSet.of("ds3_user_response"), arrayMemberTypes, ImmutableSet.of());
    final ImmutableList<Struct> allOrderedStructs = StructHelper.getStructsOrderedList(allStructs, enumNames);
    final Source source = SourceConverter.toSource(allEnums, allOrderedStructs, CCodeGenerator.getAllRequests(spec, new Ds3DocSpecEmptyImpl()));
    final CCodeGenerator codeGenerator = new CCodeGenerator();
    codeGenerator.processTemplate(source, "source-templates/ds3_c.ftl", fileUtils.getOutputStream());
    final ByteArrayOutputStream bstream = (ByteArrayOutputStream) fileUtils.getOutputStream();
    final String output = new String(bstream.toByteArray());
    final Pattern arrayStructParserPattern = Pattern.compile(Pattern.quote("static ds3_error* _parse_ds3_bucket_details_response_array(const ds3_client* client, const xmlDocPtr doc, const xmlNodePtr root, GPtrArray** _response) {"));
    final Pattern arrayParserPattern = Pattern.compile(Pattern.quote("static ds3_error* _parse_ds3_user_response(const ds3_client* client, const xmlDocPtr doc, const xmlNodePtr root, ds3_user_response** _response) {"));
    final Pattern structParserPattern = Pattern.compile(Pattern.quote("static ds3_error* _parse_top_level_ds3_list_all_my_buckets_result_response(const ds3_client* client, const ds3_request* request, ds3_list_all_my_buckets_result_response** _response, GByteArray* xml_blob) {"));
    final Matcher arrayStructParserMatcher = arrayStructParserPattern.matcher(output);
    final Matcher arrayParserMatcher = arrayParserPattern.matcher(output);
    final Matcher structParserMatcher = structParserPattern.matcher(output);
    int arrayStructParserCount = 0;
    int arrayParserCount = 0;
    int structParserCount = 0;
    while (arrayStructParserMatcher.find()) {
        arrayStructParserCount += 1;
    }
    while (arrayParserMatcher.find()) {
        arrayParserCount += 1;
    }
    while (structParserMatcher.find()) {
        structParserCount += 1;
    }
    assert (arrayStructParserCount == 1);
    assert (arrayParserCount == 1);
    assert (structParserCount == 1);
}
Also used : Enum(com.spectralogic.ds3autogen.c.models.Enum) Pattern(java.util.regex.Pattern) Ds3SpecParser(com.spectralogic.ds3autogen.api.Ds3SpecParser) Matcher(java.util.regex.Matcher) Ds3SpecParserImpl(com.spectralogic.ds3autogen.Ds3SpecParserImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Ds3ApiSpec(com.spectralogic.ds3autogen.api.models.apispec.Ds3ApiSpec) TestFileUtilsImpl(com.spectralogic.ds3autogen.utils.TestFileUtilsImpl) Ds3DocSpecEmptyImpl(com.spectralogic.ds3autogen.docspec.Ds3DocSpecEmptyImpl) Test(org.junit.Test)

Example 8 with TestFileUtilsImpl

use of com.spectralogic.ds3autogen.utils.TestFileUtilsImpl in project ds3_autogen by SpectraLogic.

the class CCodeGenerator_Test method testSimpleFreeTypedefStruct.

@Test
public void testSimpleFreeTypedefStruct() throws IOException, ParseException, TemplateModelException {
    final TestFileUtilsImpl fileUtils = new TestFileUtilsImpl();
    final Map<String, Object> testMap = new HashMap<>();
    final Struct structEntry = new Struct("ds3_bucket_response", ImmutableList.of(new StructMember(new FreeableType("ds3_str", false), "creation_date"), new StructMember(new FreeableType("ds3_str", false), "data_policy_id"), new StructMember(new FreeableType("ds3_str", false), "id"), new StructMember(new PrimitiveType("uint64_t", false), "last_preferred_chunk_size_in_bytes"), new StructMember(new PrimitiveType("uint64_t", false), "logical_used_capacity"), new StructMember(new FreeableType("ds3_str", false), "name"), new StructMember(new FreeableType("ds3_str", false), "user_id")));
    testMap.put("structEntry", structEntry);
    final CCodeGenerator codeGenerator = new CCodeGenerator();
    codeGenerator.processTemplate(testMap, "source-templates/FreeStruct.ftl", fileUtils.getOutputStream());
    final ByteArrayOutputStream bstream = (ByteArrayOutputStream) fileUtils.getOutputStream();
    final String output = new String(bstream.toByteArray());
    final String expectedOutput = "void ds3_bucket_response_free(ds3_bucket_response* response) {" + "\n" + "    if (response == NULL) {" + "\n" + "        return;" + "\n" + "    }" + "\n" + "\n" + "    ds3_str_free(response->creation_date);" + "\n" + "    ds3_str_free(response->data_policy_id);" + "\n" + "    ds3_str_free(response->id);" + "\n" + "    ds3_str_free(response->name);" + "\n" + "    ds3_str_free(response->user_id);" + "\n" + "\n" + "    g_free(response);" + "\n" + "}" + "\n";
    assertEquals(expectedOutput, output);
}
Also used : TestFileUtilsImpl(com.spectralogic.ds3autogen.utils.TestFileUtilsImpl) HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 9 with TestFileUtilsImpl

use of com.spectralogic.ds3autogen.utils.TestFileUtilsImpl in project ds3_autogen by SpectraLogic.

the class CCodeGenerator_Test method testComplexResponseStructParser.

@Test
public void testComplexResponseStructParser() throws IOException, ParseException, TemplateModelException {
    final String inputSpecFile = "/input/ComplexTypedefStruct.xml";
    final TestFileUtilsImpl fileUtils = new TestFileUtilsImpl();
    final Ds3SpecParser parser = new Ds3SpecParserImpl();
    final Ds3ApiSpec spec = parser.getSpec(CCodeGenerator_Test.class.getResourceAsStream(inputSpecFile));
    final ImmutableList<Request> allRequests = CCodeGenerator.getAllRequests(spec, new Ds3DocSpecEmptyImpl());
    final ImmutableList<Enum> allEnums = CCodeGenerator.getAllEnums(spec);
    final ImmutableSet<String> enumNames = EnumHelper.getEnumNamesSet(allEnums);
    final ImmutableSet<String> arrayMemberTypes = CCodeGenerator.getArrayMemberTypes(spec, enumNames);
    final ImmutableSet<String> responseTypes = RequestHelper.getResponseTypes(allRequests);
    final ImmutableList<Struct> allStructs = CCodeGenerator.getAllStructs(spec, enumNames, responseTypes, arrayMemberTypes, ImmutableSet.of(), ImmutableSet.of());
    final ImmutableList<Struct> allOrderedStructs = StructHelper.getStructsOrderedList(allStructs, enumNames);
    final Source source = SourceConverter.toSource(allEnums, allOrderedStructs, allRequests);
    final CCodeGenerator codeGenerator = new CCodeGenerator();
    codeGenerator.processTemplate(source, "source-templates/ds3_c.ftl", fileUtils.getOutputStream());
    final ByteArrayOutputStream bstream = (ByteArrayOutputStream) fileUtils.getOutputStream();
    final String output = new String(bstream.toByteArray());
    assertTrue(output.contains("static ds3_error* _parse_top_level_ds3_list_all_my_buckets_result_response(const ds3_client* client, const ds3_request* request, ds3_list_all_my_buckets_result_response** _response, GByteArray* xml_blob) {"));
    assertTrue(output.contains("    xmlDocPtr doc;"));
    assertTrue(output.contains("    xmlNodePtr root;"));
    assertTrue(output.contains("    xmlNodePtr child_node;"));
    assertTrue(output.contains("    ds3_error* error;"));
    assertTrue(output.contains("    ds3_list_all_my_buckets_result_response* response;"));
    assertTrue(output.contains("    error = _get_request_xml_nodes(xml_blob, &doc, &root, \"Data\");"));
    assertTrue(output.contains("    if (error != NULL) {"));
    assertTrue(output.contains("        return error;"));
    assertTrue(output.contains("    }"));
    assertTrue(output.contains("    for (child_node = root->xmlChildrenNode; child_node != NULL; child_node = child_node->next) {"));
    assertTrue(output.contains("        if (element_equal(child_node, \"Buckets\")) {"));
    assertTrue(output.contains("            GPtrArray* buckets_array;"));
    assertTrue(output.contains("            error = _parse_ds3_bucket_details_response_array(client, doc, child_node, &buckets_array);"));
    assertTrue(output.contains("            response->buckets = (ds3_bucket_details_response**)buckets_array->pdata;"));
    assertTrue(output.contains("            response->num_buckets = buckets_array->len;"));
    assertTrue(output.contains("            g_ptr_array_free(buckets_array, FALSE);"));
    assertTrue(output.contains("        } else if (element_equal(child_node, \"Owner\")) {"));
    assertTrue(output.contains("            error = _parse_ds3_user_response(client, doc, child_node, &response->owner);"));
    assertTrue(output.contains("        } else {"));
    assertTrue(output.contains("            ds3_log_message(client->log, DS3_ERROR, \"Unknown node[%s] of ds3_list_all_my_buckets_result_response [%s]\\n\", child_node->name, root->name);"));
    assertTrue(output.contains("        }"));
    assertTrue(output.contains("    }"));
    assertTrue(output.contains("    xmlFreeDoc(doc);"));
    assertTrue(output.contains("    if (error == NULL) {"));
    assertTrue(output.contains("        *_response = response;"));
    assertTrue(output.contains("    } else {"));
    assertTrue(output.contains("        ds3_list_all_my_buckets_result_response_free(response);"));
    assertTrue(output.contains("    }"));
    assertTrue(output.contains("    return error;"));
    assertTrue(output.contains("}"));
}
Also used : Enum(com.spectralogic.ds3autogen.c.models.Enum) Ds3SpecParser(com.spectralogic.ds3autogen.api.Ds3SpecParser) Ds3SpecParserImpl(com.spectralogic.ds3autogen.Ds3SpecParserImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Ds3ApiSpec(com.spectralogic.ds3autogen.api.models.apispec.Ds3ApiSpec) TestFileUtilsImpl(com.spectralogic.ds3autogen.utils.TestFileUtilsImpl) Ds3DocSpecEmptyImpl(com.spectralogic.ds3autogen.docspec.Ds3DocSpecEmptyImpl) Test(org.junit.Test)

Example 10 with TestFileUtilsImpl

use of com.spectralogic.ds3autogen.utils.TestFileUtilsImpl in project ds3_autogen by SpectraLogic.

the class CCodeGenerator_Test method testComplexResponseStructParserWithEnum.

@Test
public void testComplexResponseStructParserWithEnum() throws Exception {
    final String inputSpecFile = "/input/ComplexTypedefStructWithEnum.xml";
    final TestFileUtilsImpl fileUtils = new TestFileUtilsImpl();
    final Ds3SpecParser parser = new Ds3SpecParserImpl();
    final Ds3ApiSpec spec = parser.getSpec(CCodeGenerator_Test.class.getResourceAsStream(inputSpecFile));
    final ImmutableList<Request> allRequests = CCodeGenerator.getAllRequests(spec, new Ds3DocSpecEmptyImpl());
    final ImmutableList<Enum> allEnums = CCodeGenerator.getAllEnums(spec);
    final ImmutableSet<String> enumNames = EnumHelper.getEnumNamesSet(allEnums);
    final ImmutableSet<String> arrayMemberTypes = CCodeGenerator.getArrayMemberTypes(spec, enumNames);
    final ImmutableSet<String> responseTypes = RequestHelper.getResponseTypes(allRequests);
    final ImmutableList<Struct> allStructs = CCodeGenerator.getAllStructs(spec, enumNames, responseTypes, ImmutableSet.of("ds3_blob_response"), arrayMemberTypes, ImmutableSet.of());
    final ImmutableList<Struct> allOrderedStructs = StructHelper.getStructsOrderedList(allStructs, enumNames);
    final Source source = SourceConverter.toSource(allEnums, allOrderedStructs, allRequests);
    final CCodeGenerator codeGenerator = new CCodeGenerator();
    codeGenerator.processTemplate(source, "source-templates/ds3_c.ftl", fileUtils.getOutputStream());
    final ByteArrayOutputStream bstream = (ByteArrayOutputStream) fileUtils.getOutputStream();
    final String output = new String(bstream.toByteArray());
    assertTrue(output.contains("static ds3_error* _parse_ds3_blob_response(const ds3_client* client, const xmlDocPtr doc, const xmlNodePtr root, ds3_blob_response** _response) {"));
    assertTrue(output.contains("    xmlNodePtr child_node;"));
    assertTrue(output.contains("    ds3_blob_response* response;"));
    assertTrue(output.contains("    response = g_new0(ds3_blob_response, 1);"));
    assertTrue(output.contains("    for (child_node = root->xmlChildrenNode; child_node != NULL; child_node = child_node->next) {"));
    assertTrue(output.contains("        if (element_equal(child_node, \"ByteOffset\")) {"));
    assertTrue(output.contains("            response->byte_offset = xml_get_uint64(doc, child_node);"));
    assertTrue(output.contains("        } else if (element_equal(child_node, \"Checksum\")) {"));
    assertTrue(output.contains("            response->checksum = xml_get_string(doc, child_node);"));
    assertTrue(output.contains("        } else if (element_equal(child_node, \"ChecksumType\")) {"));
    assertTrue(output.contains("            xmlChar* text = xmlNodeListGetString(doc, child_node, 1);"));
    assertTrue(output.contains("            if (text == NULL) {"));
    assertTrue(output.contains("                continue;"));
    assertTrue(output.contains("            }"));
    assertTrue(output.contains("            response->checksum_type = _match_ds3_checksum_type(client->log, text);"));
    assertTrue(output.contains("        } else if (element_equal(child_node, \"Id\")) {"));
    assertTrue(output.contains("            response->id = xml_get_string(doc, child_node);"));
    assertTrue(output.contains("        } else if (element_equal(child_node, \"Length\")) {"));
    assertTrue(output.contains("            response->length = xml_get_uint64(doc, child_node);"));
    assertTrue(output.contains("        } else if (element_equal(child_node, \"ObjectId\")) {"));
    assertTrue(output.contains("            response->object_id = xml_get_string(doc, child_node);"));
    assertTrue(output.contains("        } else {"));
    assertTrue(output.contains("            ds3_log_message(client->log, DS3_ERROR, \"Unknown node[%s] of ds3_blob_response [%s]\\n\", child_node->name, root->name);"));
    assertTrue(output.contains("        }"));
    assertTrue(output.contains("        if (error != NULL) {"));
    assertTrue(output.contains("            break;"));
    assertTrue(output.contains("        }"));
    assertTrue(output.contains("    }"));
    assertTrue(output.contains("    *_response = response;"));
    assertTrue(output.contains("    return error;"));
    assertTrue(output.contains("}"));
}
Also used : Enum(com.spectralogic.ds3autogen.c.models.Enum) Ds3SpecParser(com.spectralogic.ds3autogen.api.Ds3SpecParser) Ds3SpecParserImpl(com.spectralogic.ds3autogen.Ds3SpecParserImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Ds3ApiSpec(com.spectralogic.ds3autogen.api.models.apispec.Ds3ApiSpec) TestFileUtilsImpl(com.spectralogic.ds3autogen.utils.TestFileUtilsImpl) Ds3DocSpecEmptyImpl(com.spectralogic.ds3autogen.docspec.Ds3DocSpecEmptyImpl) Test(org.junit.Test)

Aggregations

TestFileUtilsImpl (com.spectralogic.ds3autogen.utils.TestFileUtilsImpl)43 Test (org.junit.Test)43 ByteArrayOutputStream (java.io.ByteArrayOutputStream)40 Ds3DocSpecEmptyImpl (com.spectralogic.ds3autogen.docspec.Ds3DocSpecEmptyImpl)35 Ds3SpecParserImpl (com.spectralogic.ds3autogen.Ds3SpecParserImpl)23 Ds3SpecParser (com.spectralogic.ds3autogen.api.Ds3SpecParser)23 Ds3ApiSpec (com.spectralogic.ds3autogen.api.models.apispec.Ds3ApiSpec)23 HashMap (java.util.HashMap)21 Enum (com.spectralogic.ds3autogen.c.models.Enum)15 Request (com.spectralogic.ds3autogen.c.models.Request)14 Source (com.spectralogic.ds3autogen.c.models.Source)9 Ds3Request (com.spectralogic.ds3autogen.api.models.apispec.Ds3Request)3 NameMapper (com.spectralogic.ds3autogen.NameMapper)2 Ds3DocSpecParser (com.spectralogic.ds3autogen.api.Ds3DocSpecParser)2 Ds3Param (com.spectralogic.ds3autogen.api.models.apispec.Ds3Param)2 Ds3Type (com.spectralogic.ds3autogen.api.models.apispec.Ds3Type)2 Ds3DocSpec (com.spectralogic.ds3autogen.api.models.docspec.Ds3DocSpec)2 Ds3DocSpecParserImpl (com.spectralogic.ds3autogen.docspec.Ds3DocSpecParserImpl)2 CodeGenerator (com.spectralogic.ds3autogen.api.CodeGenerator)1 FileUtils (com.spectralogic.ds3autogen.api.FileUtils)1