刘光辉
117 分钟以前 06f314545ac3514d7ce28939920866ae8188d31c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package jnpf.elnController;
 
import jnpf.eln.RecordFillApi;
import jnpf.eln.RecordFreezeApi;
import jnpf.eln.TemplateEditabilityApi;
import org.junit.Test;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
 
import java.lang.reflect.Method;
 
import static org.junit.Assert.assertFalse;
 
public class ElnApiPathNamingTest {
 
    private static final Class<?>[] CONTROLLERS = {
            ElnFieldCatalogController.class,
            ElnFormulaCatalogController.class,
            ElnRecordController.class,
            ElnRecordFillController.class,
            ElnRecordFillInternalController.class,
            ElnRecordFreezeInternalController.class,
            ElnRecordQueryController.class,
            ElnTemplateAnnotationController.class,
            ElnTemplateCatalogController.class,
            ElnTemplateEditabilityController.class,
            ElnTemplateFileController.class,
            PingController.class
    };
 
    private static final Class<?>[] FEIGN_APIS = {
            RecordFillApi.class,
            RecordFreezeApi.class,
            TemplateEditabilityApi.class
    };
 
    @Test
    public void allElnApiPathsUseJavaCamelCaseSegments() {
        for (Class<?> type : CONTROLLERS) {
            assertRequestMapping(type, type.getAnnotation(RequestMapping.class));
            assertMethodMappings(type);
        }
        for (Class<?> type : FEIGN_APIS) {
            FeignClient feignClient = type.getAnnotation(FeignClient.class);
            assertCamelCasePath(type.getName(), feignClient.path());
            assertMethodMappings(type);
        }
    }
 
    private static void assertMethodMappings(Class<?> type) {
        for (Method method : type.getDeclaredMethods()) {
            String owner = type.getName() + "#" + method.getName();
            assertRequestMapping(method, owner);
            assertPaths(owner, values(method.getAnnotation(GetMapping.class)));
            assertPaths(owner, values(method.getAnnotation(PostMapping.class)));
            assertPaths(owner, values(method.getAnnotation(PutMapping.class)));
            assertPaths(owner, values(method.getAnnotation(DeleteMapping.class)));
            assertPaths(owner, values(method.getAnnotation(PatchMapping.class)));
        }
    }
 
    private static void assertRequestMapping(Class<?> type, RequestMapping mapping) {
        if (mapping != null) {
            assertPaths(type.getName(), mapping.value());
        }
    }
 
    private static void assertRequestMapping(Method method, String owner) {
        RequestMapping mapping = method.getAnnotation(RequestMapping.class);
        if (mapping != null) {
            assertPaths(owner, mapping.value());
        }
    }
 
    private static void assertPaths(String owner, String[] paths) {
        if (paths == null) {
            return;
        }
        for (String path : paths) {
            assertCamelCasePath(owner, path);
        }
    }
 
    private static void assertCamelCasePath(String owner, String path) {
        assertFalse(owner + " contains a kebab-case API path: " + path, path.contains("-"));
    }
 
    private static String[] values(GetMapping mapping) {
        return mapping == null ? null : mapping.value();
    }
 
    private static String[] values(PostMapping mapping) {
        return mapping == null ? null : mapping.value();
    }
 
    private static String[] values(PutMapping mapping) {
        return mapping == null ? null : mapping.value();
    }
 
    private static String[] values(DeleteMapping mapping) {
        return mapping == null ? null : mapping.value();
    }
 
    private static String[] values(PatchMapping mapping) {
        return mapping == null ? null : mapping.value();
    }
}