Java tutorial
/* * Copyright (c) 2014 Andrey Paslavsky. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.paslavsky.springrest; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.testng.Assert; import org.testng.annotations.DataProvider; import org.testng.annotations.NoInjection; import org.testng.annotations.Test; import java.lang.reflect.Method; import java.util.HashSet; import java.util.Map; import java.util.Set; /** * Tests for the {@link SpringAnnotationPreprocessor} * * @author Andrey Paslavsky * @version 1.0 */ public class SpringAnnotationPreprocessorTest { @DataProvider public Object[][] data() { return new Object[][] { rowData(TestClient1.class, "test1"), rowData(TestClient1.class, "test2"), rowData(TestClient1.class, "test3"), rowData(TestClient1.class, "test4"), rowData(TestClient1.class, "test5"), rowData(TestClient1.class, "test6"), rowData(TestClient1.class, "test7"), rowData(TestClient2.class, "test8"), rowData(TestClient2.class, "test9"), rowData(TestClient2.class, "test10"), rowData(TestClient2.class, "test11"), }; } private Object[] rowData(Class<?> clientClass, String methodName) { MetadataBuilder builder = new MetadataBuilder(); if ("test1".equals(methodName)) { builder.setAdditionalPath("/test1").setHttpMethod(HttpMethod.PUT).setRequestParameter(0); } else if ("test7".equals(methodName)) { builder.setAdditionalPath("/test7/{path}").setHttpMethod(HttpMethod.GET).addUriVarParameter("path", 0) .addQueryParameter("size", 1).setMethodReturnType(Map.class).setResponseClass(Map.class); } else if ("test2".equals(methodName)) { builder.setAdditionalPath("/test2").setHttpMethod(HttpMethod.GET).addRequestHeaderParameter("OAuth", 0) .setMethodReturnType(ResponseEntity.class).setResponseClass(Map.class); } else if ("test3".equals(methodName)) { builder.setAdditionalPath("/test3").setHttpMethod(HttpMethod.GET) .setMethodReturnType(ResponseEntity.class).setResponseClass(Map.class); } else if ("test4".equals(methodName)) { builder.setAdditionalPath("/test4").setHttpMethod(HttpMethod.GET) .setMethodReturnType(ResponseEntity.class).setResponseClass(byte[].class); } else if ("test5".equals(methodName)) { builder.setAdditionalPath("/test5").setHttpMethod(HttpMethod.GET) .setMethodReturnType(ResponseEntity.class).setResponseClass(byte[].class); } else if ("test6".equals(methodName)) { builder.setAdditionalPath("/test6").setHttpMethod(HttpMethod.GET).setMethodReturnType(Object.class) .setResponseClass(byte[].class); } else if ("test8".equals(methodName)) { builder.setCommonPath("/common").setAdditionalPath("/test8").setHttpMethod(HttpMethod.PUT); } else if ("test9".equals(methodName)) { builder.setCommonPath("/common").setAdditionalPath("/test9").setHttpMethod(HttpMethod.GET) .setMethodReturnType(String.class).setResponseClass(String.class); } else if ("test10".equals(methodName)) { builder.setCommonPath("/common").setHttpMethod(HttpMethod.GET).setMethodReturnType(String.class) .setResponseClass(String.class); } else if ("test11".equals(methodName)) { builder.setCommonPath("/common").setHttpMethod(HttpMethod.GET).setMethodReturnType(String.class) .setResponseClass(String.class); } return new Object[] { clientClass, getMethod(clientClass, methodName), builder.build() }; } private Method getMethod(Class<?> clazz, String methodName) { Set<Method> candidates = new HashSet<Method>(1); Method[] methods = clazz.getMethods(); for (Method method : methods) { if (methodName.equals(method.getName())) { candidates.add(method); } } if (candidates.size() == 1) { return candidates.iterator().next(); } else if (candidates.isEmpty()) { throw new IllegalStateException("Expected method not found: " + clazz + "." + methodName); } else { throw new IllegalStateException("No unique method found: " + clazz + "." + methodName); } } @Test(dataProvider = "data") public void testParse(@NoInjection Class clientClass, @NoInjection Method method, RestMethodMetadata expected) throws Exception { RestMethodMetadata metadata = new SpringAnnotationPreprocessor().parse(clientClass, method); Assert.assertEquals(metadata, expected); } }