Project

General

Profile

Download (3.34 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2014 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.remote;
10

    
11
import java.io.File;
12
import java.io.IOException;
13
import java.nio.charset.Charset;
14

    
15
import org.apache.commons.io.FileUtils;
16
import org.apache.log4j.Logger;
17
import org.junit.Test;
18

    
19
import eu.etaxonomy.cdm.remote.config.SwaggerGroupsConfig;
20
import eu.etaxonomy.cdm.remote.staticSwagger.StaticSwaggerApiDoc;
21

    
22
/**
23
 * The resources produced by this tests are delivered by the {@link StaticSwaggerApiDoc} controller.
24
 *
25
 * @author a.kohlbecker
26
 * @since Mar 3, 2014
27
 */
28
public class SwaggerStaticIT extends WebServiceTestBase {
29

    
30
    public static final Logger logger = Logger.getLogger(SwaggerStaticIT.class);
31

    
32
    private String[] swaggerResourcesPaths = new String[]{"", "/configuration/ui", "/configuration/security" };
33

    
34

    
35
    @Test
36
    public void fetchSwaggerResources() {
37

    
38
        String swagger2Endpoint= "/swagger-resources";
39

    
40
        String staticResourcesFolder = "./target/classes/"+ StaticSwaggerApiDoc.SWAGGER_STATIC + "/swagger-resources";
41

    
42
        logger.info("clearing old content ...");
43
        FileUtils.deleteQuietly(new File(staticResourcesFolder));
44
        FileUtils.deleteQuietly(new File(staticResourcesFolder + StaticSwaggerApiDoc.JSON));
45

    
46
        staticResourcesFolder = staticResourcesFolder.replace("/", File.separator);
47

    
48
        for(String path : swaggerResourcesPaths){
49
            String resourcePath = swagger2Endpoint + path;
50
            logger.info("fetching swagger-resources file from " + resourcePath);
51
            String response =  httpGetJson(resourcePath, null);
52
            try {
53
                File targetFile = new File(staticResourcesFolder + path + StaticSwaggerApiDoc.JSON);
54
                new File(targetFile.getParent()).mkdirs();
55
                FileUtils.write(targetFile, response, Charset.defaultCharset());
56
                logger.info(response.length() + " characters of swagger-resources written to " + targetFile.getAbsolutePath());
57
            } catch (IOException e) {
58
                logger.error(e);
59
            }
60
        }
61
    }
62

    
63
    @Test
64
    public void fetchSwaggerGroups(){
65

    
66
        String swagger2Endpoint= "/v2/api-docs";
67
        String staticApiDocFolder = "./target/classes/"+ StaticSwaggerApiDoc.SWAGGER_STATIC + "/api-docs/";
68

    
69
        staticApiDocFolder = staticApiDocFolder.replace("/", File.separator);
70

    
71
        for(SwaggerGroupsConfig group : SwaggerGroupsConfig.values()) {
72
            logger.info(group.groupName());
73
            String response =  httpGetJson(swagger2Endpoint, "group=" + group.groupName());
74
            response = response.replaceAll(",\"host\":\"([^\"]*)", ",\"host\":\"" + StaticSwaggerApiDoc.HOST);
75
            response = response.replaceAll(",\"basePath\":\"([^\"]*)", ",\"basePath\":\"" + StaticSwaggerApiDoc.BASE_PATH);
76
            try {
77
                FileUtils.write(new File(staticApiDocFolder + group.name() + StaticSwaggerApiDoc.JSON), response, Charset.defaultCharset());
78
            } catch (IOException e) {
79
                // TODO Auto-generated catch block
80
                e.printStackTrace();
81
            }
82
        }
83

    
84
//        File pwd = new File("pom.xml");
85
//        System.err.println(pwd.getAbsolutePath());
86
    }
87
}
(3-3/4)