Search in sources :

Example 1 with GatewayConfigDTO

use of org.wso2.carbon.apimgt.core.template.dto.GatewayConfigDTO in project carbon-apimgt by wso2.

the class APIFileUtilsTestCase method testAPIFileUtils.

@Test
public void testAPIFileUtils() throws Exception {
    APIFileUtils.createDirectory(dirPath);
    Assert.assertTrue(Files.exists(Paths.get(dirPath)), "Directory doesn't exists");
    String filePath = dirPath + File.separatorChar + fileName;
    APIFileUtils.createFile(filePath);
    Assert.assertTrue(Files.exists(Paths.get(filePath)), "File doesn't exists");
    // write content to file
    APIFileUtils.writeToFile(dirPath + File.separatorChar + fileName, "This is some test content");
    // read content as text
    String contentRead = APIFileUtils.readFileContentAsText(dirPath + File.separatorChar + fileName);
    Assert.assertEquals(contentRead, "This is some test content");
    // read content as stream
    InputStream contentReadInputStream = APIFileUtils.readFileContentAsStream(dirPath + File.separatorChar + fileName);
    Assert.assertEquals(IOUtils.toString(contentReadInputStream), "This is some test content");
    // write stream to filesystem
    APIFileUtils.writeStreamToFile(dirPath + File.separatorChar + "write-stream-to-file", contentReadInputStream);
    Assert.assertTrue(Files.exists(Paths.get(dirPath + File.separatorChar + "write-stream-to-file")), "File doesn't exists");
    // create archive
    APIFileUtils.archiveDirectory(new File(dirPath).getAbsolutePath(), new File(parentDir + File.separatorChar).getAbsolutePath(), "my-test-archive");
    Assert.assertTrue(Files.exists(Paths.get(parentDir + File.separatorChar + "my-test-archive.zip")), "File doesn't exists");
    // find in file system
    Assert.assertNotNull(APIFileUtils.findInFileSystem(new File(dirPath), fileName));
    // write object as json
    GatewayConfigDTO configDTO = new GatewayConfigDTO();
    configDTO.setApiName("api1");
    configDTO.setConfig("config");
    configDTO.setContext("context");
    APIFileUtils.writeObjectAsJsonToFile(configDTO, dirPath + File.separatorChar + "object.json");
    Assert.assertTrue(Files.exists(Paths.get(dirPath + File.separatorChar + "object.json")), "File doesn't exists");
    // write string as json
    APIFileUtils.writeStringAsJsonToFile("{\"config\":\"config\"}", dirPath + File.separatorChar + "object-2.json");
    Assert.assertTrue(Files.exists(Paths.get(dirPath + File.separatorChar + "object-2.json")), "File doesn't exists");
    // list directories
    Assert.assertTrue(APIFileUtils.getDirectoryList(parentDir).size() > 0);
    // export API Definition
    FileApi fileApi = new FileApi();
    fileApi.setApiDefinition("api-definition");
    fileApi.setName("test-api");
    fileApi.setVersion("1.0.0");
    fileApi.setProvider("p1");
    fileApi.setId("1x1x");
    APIFileUtils.exportApiDefinitionToFileSystem(fileApi, parentDir);
    Assert.assertTrue(Files.exists(Paths.get(parentDir + File.separatorChar + "api-1x1x.json")), "File doesn't exists");
    // export swagger definition
    String swaggerDefinition = "{\"swaggerVersion\": \"1.2\",\"apis\": [{\"path\": " + "\"http://localhost:8000/listings/greetings\"," + "\"description\": \"Generating greetings in our application.\"}]}";
    APIFileUtils.exportSwaggerDefinitionToFileSystem(swaggerDefinition, new API.APIBuilder("p", "name", "1.0").id("1x1x").build(), parentDir);
    Assert.assertTrue(Files.exists(Paths.get(parentDir + File.separatorChar + "swagger-1x1x.json")), "File doesn't exists");
    // export endpoint
    APIFileUtils.exportEndpointToFileSystem(SampleTestObjectCreator.createMockEndpoint(), parentDir);
    Assert.assertTrue(Files.exists(Paths.get(parentDir + File.separatorChar + "Endpoint1.json")), "Endpoint doesn't exists");
    // export gatewayconfig
    APIFileUtils.exportGatewayConfigToFileSystem("gateway-config-content-goes-here", new API.APIBuilder("p", "name-5", "1.0").id("5x5x").build(), parentDir);
    Assert.assertTrue(Files.exists(Paths.get(parentDir + File.separatorChar + "gateway-configuration")), "Gateway config doesn't exists");
    // delete file
    APIFileUtils.deleteFile(filePath);
    Assert.assertFalse(Files.exists(Paths.get(filePath)), "File has not deleted");
    // delete directory
    APIFileUtils.deleteDirectory(dirPath);
    Assert.assertFalse(Files.exists(Paths.get(dirPath)), "Directory has not deleted");
}
Also used : GatewayConfigDTO(org.wso2.carbon.apimgt.core.template.dto.GatewayConfigDTO) InputStream(java.io.InputStream) API(org.wso2.carbon.apimgt.core.models.API) File(java.io.File) FileApi(org.wso2.carbon.apimgt.core.models.FileApi) Test(org.testng.annotations.Test)

Aggregations

File (java.io.File)1 InputStream (java.io.InputStream)1 Test (org.testng.annotations.Test)1 API (org.wso2.carbon.apimgt.core.models.API)1 FileApi (org.wso2.carbon.apimgt.core.models.FileApi)1 GatewayConfigDTO (org.wso2.carbon.apimgt.core.template.dto.GatewayConfigDTO)1