Search in sources :

Example 16 with Enum

use of com.spectralogic.ds3autogen.c.models.Enum in project ds3_autogen by SpectraLogic.

the class CCodeGenerator_Test method testComplexFreeTypedefStruct.

@Test
public void testComplexFreeTypedefStruct() 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("void ds3_list_all_my_buckets_result_response_free(ds3_list_all_my_buckets_result_response* response) {"));
    assertTrue(output.contains("    if (response == NULL) {"));
    assertTrue(output.contains("        return;"));
    assertTrue(output.contains("    }"));
    assertTrue(output.contains("    for (index = 0; index < response->num_buckets; index++) {"));
    assertTrue(output.contains("        ds3_bucket_details_response_free(response->buckets[index]);"));
    assertTrue(output.contains("    }"));
    assertTrue(output.contains("    g_free(response->buckets);"));
    assertTrue(output.contains("    ds3_user_response_free(response->owner);"));
    assertTrue(output.contains("    g_free(response);"));
    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 17 with Enum

use of com.spectralogic.ds3autogen.c.models.Enum in project ds3_autogen by SpectraLogic.

the class CCodeGenerator_Test method testGenerateSingleTypeEnumConstantMatcher.

@Test
public void testGenerateSingleTypeEnumConstantMatcher() throws IOException, TemplateModelException {
    final TestFileUtilsImpl fileUtils = new TestFileUtilsImpl();
    final Map<String, Object> testMap = new HashMap<>();
    final Ds3EnumConstant inProgress = new Ds3EnumConstant("IN_PROGRESS", null);
    final Ds3EnumConstant completed = new Ds3EnumConstant("COMPLETED", null);
    final Ds3EnumConstant canceled = new Ds3EnumConstant("CANCELED", null);
    final ImmutableList<Ds3EnumConstant> enumConstants = ImmutableList.of(inProgress, completed, canceled);
    final Enum enumEntry = EnumConverter.toEnum(new Ds3Type("JobStatus", null, null, enumConstants));
    testMap.put("enumEntry", enumEntry);
    final CCodeGenerator codeGenerator = new CCodeGenerator();
    codeGenerator.processTemplate(testMap, "source-templates/TypedefEnumMatcher.ftl", fileUtils.getOutputStream());
    final ByteArrayOutputStream bstream = (ByteArrayOutputStream) fileUtils.getOutputStream();
    final String output = new String(bstream.toByteArray());
    final String expectedOutput = "static ds3_job_status _match_ds3_job_status(const ds3_log* log, const xmlChar* text) {" + "\n" + "    if (xmlStrcmp(text, (const xmlChar*) \"IN_PROGRESS\") == 0) {" + "\n" + "        return DS3_JOB_STATUS_IN_PROGRESS;" + "\n" + "    } else if (xmlStrcmp(text, (const xmlChar*) \"COMPLETED\") == 0) {" + "\n" + "        return DS3_JOB_STATUS_COMPLETED;" + "\n" + "    } else if (xmlStrcmp(text, (const xmlChar*) \"CANCELED\") == 0) {" + "\n" + "        return DS3_JOB_STATUS_CANCELED;" + "\n" + "    } else {" + "\n" + "        ds3_log_message(log, DS3_ERROR, \"ERROR: Unknown value of '%s'.  Returning DS3_JOB_STATUS_IN_PROGRESS for safety.\", text);" + "\n" + "        return DS3_JOB_STATUS_IN_PROGRESS;" + "\n" + "    }" + "\n" + "}" + "\n";
    assertThat(expectedOutput, is(output));
}
Also used : Enum(com.spectralogic.ds3autogen.c.models.Enum) TestFileUtilsImpl(com.spectralogic.ds3autogen.utils.TestFileUtilsImpl) HashMap(java.util.HashMap) Ds3Type(com.spectralogic.ds3autogen.api.models.apispec.Ds3Type) Ds3EnumConstant(com.spectralogic.ds3autogen.api.models.apispec.Ds3EnumConstant) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 18 with Enum

use of com.spectralogic.ds3autogen.c.models.Enum in project ds3_autogen by SpectraLogic.

the class StructHelper_Test method testGenerateParserForTopLevelStruct.

@Test
public void testGenerateParserForTopLevelStruct() throws IOException, ParseException, TemplateModelException {
    final String inputSpecFile = "/input/ResponseTypeGetSystemInfo.xml";
    final TestFileUtilsImpl fileUtils = new TestFileUtilsImpl();
    final Ds3SpecParser parser = new Ds3SpecParserImpl();
    final Ds3ApiSpec spec = parser.getSpec(CCodeGenerator_Test.class.getResourceAsStream(inputSpecFile));
    final Ds3DocSpecParser docSpecParser = new Ds3DocSpecParserImpl(new NameMapper());
    final Ds3DocSpec docSpec = docSpecParser.getDocSpec(CCodeGenerator_Test.class.getResourceAsStream("/input/testCommandDocumentation.json"));
    final ImmutableList<Request> allRequests = CCodeGenerator.getAllRequests(spec, docSpec);
    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 Source source = SourceConverter.toSource(allEnums, allStructs, 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_system_information_response(const ds3_client* client, const ds3_request* request, ds3_system_information_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_system_information_response* response;"));
    assertTrue(output.contains("    ds3_error* error;"));
    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("    response = g_new0(ds3_system_information_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, \"ApiVersion\")) {"));
    assertTrue(output.contains("            response->api_version = xml_get_string(doc, child_node);"));
    assertTrue(output.contains("        } else if (element_equal(child_node, \"BackendActivated\")) {"));
    assertTrue(output.contains("            response->backend_activated = xml_get_bool(client->log, doc, child_node);"));
    assertTrue(output.contains("        } else if (element_equal(child_node, \"BuildInformation\")) {"));
    assertTrue(output.contains("            error = _parse_ds3_build_information_response(client, doc, child_node, &response->build_information);"));
    assertTrue(output.contains("        } else if (element_equal(child_node, \"InstanceId\")) {"));
    assertTrue(output.contains("            response->instance_id = xml_get_string(doc, child_node);"));
    assertTrue(output.contains("        } else if (element_equal(child_node, \"SerialNumber\")) {"));
    assertTrue(output.contains("            response->serial_number = 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_system_information_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("    xmlFreeDoc(doc);"));
    assertTrue(output.contains("    if (error == NULL) {"));
    assertTrue(output.contains("        *_response = response;"));
    assertTrue(output.contains("    } else {"));
    assertTrue(output.contains("        ds3_system_information_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) Ds3DocSpecParserImpl(com.spectralogic.ds3autogen.docspec.Ds3DocSpecParserImpl) Ds3DocSpec(com.spectralogic.ds3autogen.api.models.docspec.Ds3DocSpec) Ds3SpecParserImpl(com.spectralogic.ds3autogen.Ds3SpecParserImpl) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Ds3ApiSpec(com.spectralogic.ds3autogen.api.models.apispec.Ds3ApiSpec) TestFileUtilsImpl(com.spectralogic.ds3autogen.utils.TestFileUtilsImpl) NameMapper(com.spectralogic.ds3autogen.NameMapper) Ds3DocSpecParser(com.spectralogic.ds3autogen.api.Ds3DocSpecParser) Test(org.junit.Test)

Example 19 with Enum

use of com.spectralogic.ds3autogen.c.models.Enum in project ds3_autogen by SpectraLogic.

the class SourceConverter_Test method testGetQueryParamEnumsRequiredAndOptional.

@Test
public void testGetQueryParamEnumsRequiredAndOptional() {
    final Enum keepEnum1 = new Enum("ds3_pool_type", ImmutableList.of("NEARLINE", "ONLINE"));
    final Enum keepEnum2 = new Enum("ds3_checksum_type", ImmutableList.of("CRC_32", "CRC_32C", "MD5", "SHA_256", "SHA_512"));
    final Enum filterEnum = new Enum("ds3_other_enum", ImmutableList.of("ALPHA", "BRAVO", "CHARLIE"));
    final ImmutableList<Enum> allEnums = ImmutableList.of(keepEnum1, keepEnum2, filterEnum);
    final ImmutableList<Request> allRequests = ImmutableList.of(new Request("GetWidgetRequestHandler", Classification.spectrads3, HttpVerb.GET, null, null, null, ImmutableList.of(), ImmutableList.of(new Parameter(ParameterModifier.CONST, "ds3_pool_type", "type", ParameterPointerType.NONE, false)), ImmutableList.of(new Parameter(ParameterModifier.CONST, "ds3_checksum_type", "type", ParameterPointerType.NONE, false)), false, false, null, "ds3_widget", false, null));
    final ImmutableSet<Enum> filteredEnums = SourceConverter.filterQueryParamEnums(allEnums, allRequests);
    assertThat(filteredEnums.size(), is(2));
    assertTrue(filteredEnums.contains(keepEnum1));
    assertTrue(filteredEnums.contains(keepEnum2));
    assertFalse(filteredEnums.contains(filterEnum));
}
Also used : Enum(com.spectralogic.ds3autogen.c.models.Enum) Test(org.junit.Test)

Example 20 with Enum

use of com.spectralogic.ds3autogen.c.models.Enum in project ds3_autogen by SpectraLogic.

the class CCodeGenerator_Test method testGenerateSingleTypedefEnum.

@Test
public void testGenerateSingleTypedefEnum() throws IOException, TemplateModelException {
    final TestFileUtilsImpl fileUtils = new TestFileUtilsImpl();
    final Map<String, Object> testMap = new HashMap<>();
    final Enum enumEntry = new Enum("ds3_job_status", ImmutableList.of("IN_PROGRESS", "COMPLETED", "CANCELED"));
    testMap.put("enumEntry", enumEntry);
    final CCodeGenerator codeGenerator = new CCodeGenerator();
    codeGenerator.processTemplate(testMap, "header-templates/TypedefEnum.ftl", fileUtils.getOutputStream());
    final ByteArrayOutputStream bstream = (ByteArrayOutputStream) fileUtils.getOutputStream();
    final String output = new String(bstream.toByteArray());
    final String expectedOutput = "typedef enum {" + "\n" + "    IN_PROGRESS," + "\n" + "    COMPLETED," + "\n" + "    CANCELED" + "\n" + "}ds3_job_status;" + "\n";
    assertThat(expectedOutput, is(output));
    // verify no trailing comma
    assertFalse(output.contains("    CANCELED,"));
}
Also used : Enum(com.spectralogic.ds3autogen.c.models.Enum) TestFileUtilsImpl(com.spectralogic.ds3autogen.utils.TestFileUtilsImpl) HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

Enum (com.spectralogic.ds3autogen.c.models.Enum)24 Test (org.junit.Test)24 TestFileUtilsImpl (com.spectralogic.ds3autogen.utils.TestFileUtilsImpl)16 Ds3SpecParserImpl (com.spectralogic.ds3autogen.Ds3SpecParserImpl)15 Ds3SpecParser (com.spectralogic.ds3autogen.api.Ds3SpecParser)15 Ds3ApiSpec (com.spectralogic.ds3autogen.api.models.apispec.Ds3ApiSpec)15 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 Ds3DocSpecEmptyImpl (com.spectralogic.ds3autogen.docspec.Ds3DocSpecEmptyImpl)13 Ds3EnumConstant (com.spectralogic.ds3autogen.api.models.apispec.Ds3EnumConstant)5 Ds3Type (com.spectralogic.ds3autogen.api.models.apispec.Ds3Type)5 Ds3DocSpec (com.spectralogic.ds3autogen.api.models.docspec.Ds3DocSpec)4 NameMapper (com.spectralogic.ds3autogen.NameMapper)3 Ds3DocSpecParser (com.spectralogic.ds3autogen.api.Ds3DocSpecParser)3 Ds3DocSpecParserImpl (com.spectralogic.ds3autogen.docspec.Ds3DocSpecParserImpl)3 HashMap (java.util.HashMap)3 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSet (com.google.common.collect.ImmutableSet)1