刘光辉
8 小时以前 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
package jnpf.limsController;
 
import jnpf.lims.api.ElnSourceApi;
import jnpf.lims.api.TestWorkItemSnapshotApi;
import org.junit.Test;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
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 LimsElnSourceApiPathNamingTest {
 
    @Test
    public void allLimsElnSourceApiPathsUseJavaCamelCaseSegments() {
        for (Class<?> type : new Class<?>[]{LimsElnSourceController.class,
                LimsElnSourceActorController.class, LimsTestWorkItemSnapshotController.class}) {
            RequestMapping mapping = type.getAnnotation(RequestMapping.class);
            assertPaths(type.getName(), mapping == null ? null : mapping.value());
            assertMethodMappings(type);
        }
        for (Class<?> type : new Class<?>[]{ElnSourceApi.class, TestWorkItemSnapshotApi.class}) {
            FeignClient feign = type.getAnnotation(FeignClient.class);
            assertPaths(type.getName(), new String[]{feign.path()});
            assertMethodMappings(type);
        }
    }
 
    private static void assertMethodMappings(Class<?> type) {
        for (Method method : type.getDeclaredMethods()) {
            String owner = type.getName() + "#" + method.getName();
            GetMapping get = method.getAnnotation(GetMapping.class);
            PostMapping post = method.getAnnotation(PostMapping.class);
            PutMapping put = method.getAnnotation(PutMapping.class);
            assertPaths(owner, get == null ? null : get.value());
            assertPaths(owner, post == null ? null : post.value());
            assertPaths(owner, put == null ? null : put.value());
        }
    }
 
    private static void assertPaths(String owner, String[] paths) {
        if (paths == null) {
            return;
        }
        for (String path : paths) {
            assertFalse(owner + " contains a kebab-case API path: " + path, path.contains("-"));
        }
    }
}