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(); } }