public void testMethodFromClassAndNoParams() { @SuppressWarnings("rawtypes") Invokable<Set, Object> methodInSuper = method(Set.class, "iterator"); assertEquals(methodInSuper.getOwnerType(), typeToken(Set.class)); }
public static void putInvokables(Class<?> sync, Class<?> async, Cache<Invokable<?, ?>, Invokable<?, ?>> cache) { for (Invokable<?, ?> invoked : methods(sync)) { Invokable<?, ?> delegatedMethod = method(async, invoked.getName(), getParameterTypes(invoked)); checkArgument(delegatedMethod.getExceptionTypes().equals(invoked.getExceptionTypes()) || isCloseable(delegatedMethod), "invoked %s has different typed exceptions than target %s", invoked, delegatedMethod); cache.put(invoked, delegatedMethod); } }
/** * return all methods present in the class as {@link Invokable}s. * * @param ownerType * corresponds to {@link Invokable#getOwnerType()} */ @SuppressWarnings("unchecked") public static <T> Collection<Invokable<T, Object>> methods(Class<T> ownerType) { return Collection.class.cast(get(methodsForTypeToken, typeToken(ownerType))); }
public void testReleaseAddress() throws SecurityException, NoSuchMethodException, IOException { Invokable<?, ?> method = method(ElasticIPAddressAsyncClient.class, "releaseAddressInRegion", String.class, String.class); GeneratedHttpRequest request = processor.createRequest(method, Lists.<Object> newArrayList(null, "127.0.0.1")); assertRequestLineEquals(request, "POST https://ec2.us-east-1.amazonaws.com/ HTTP/1.1"); assertNonPayloadHeadersEqual(request, "Host: ec2.us-east-1.amazonaws.com\n"); assertPayloadEquals(request, "Action=ReleaseAddress&PublicIp=127.0.0.1", "application/x-www-form-urlencoded", false); assertResponseParserClassEquals(method, request, ReleasePayloadAndReturn.class); assertSaxResponseParserClassEquals(method, null); assertFallbackClassEquals(method, null); checkFilters(request); }
@Test public void testBuildTwoHeader() throws SecurityException, NoSuchMethodException { Invokable<?, ?> method = method(TestHeader.class, "twoHeader", String.class); Multimap<String, String> headers = processor.apply(Invocation.create(method, ImmutableList.<Object> of("robot"))).getHeaders(); assertEquals(headers.size(), 2); assertEquals(headers.get("slash"), ImmutableList.of("/robot")); assertEquals(headers.get("hyphen"), ImmutableList.of("-robot")); }
@Test(expectedExceptions = NullPointerException.class, expectedExceptionsMessageRegExp = "param\\{robbie\\} for invocation TestQuery.foo3") public void testNiceNPEQueryParam() throws SecurityException, NoSuchMethodException, IOException { processor.apply(Invocation.create(method(TestQuery.class, "foo3", String.class), Lists.<Object> newArrayList((String) null))); }
@Test public void testSkipEncoding() throws SecurityException, NoSuchMethodException { Invokable<?, ?> method = method(TestEncoding.class, "twoPaths", String.class, String.class); GeneratedHttpRequest request = processor.apply(Invocation.create(method, ImmutableList.<Object> of("1", "localhost"))); assertEquals(request.getEndpoint().getPath(), "/1/localhost"); assertEquals(request.getMethod(), HttpMethod.GET); assertEquals(request.getHeaders().size(), 0); }
@Test public void testBuildOneClassQuery() throws SecurityException, NoSuchMethodException { Invokable<?, ?> method = method(TestClassQuery.class, "oneQuery", String.class); String query = processor.apply(Invocation.create(method, ImmutableList.<Object> of("robot"))) .getEndpoint().getQuery(); assertEquals(query, "x-amz-copy-source=/robot"); }
public void testNoNPEOnQueryParamWithNullable() throws SecurityException, NoSuchMethodException { GeneratedHttpRequest request = processor.apply(Invocation.create( method(TestQuery.class, "foo3Nullable", String.class), Lists.<Object> newArrayList((String) null))); assertEquals(request.getEndpoint().getHost(), "localhost"); assertEquals(request.getEndpoint().getPath(), "/"); assertEquals(request.getEndpoint().getQuery(), "x-ms-version=2009-07-17&foo=bar&fooble=baz"); assertEquals(request.getMethod(), "FOO"); }
@Test public void testBuildTwoForm() throws SecurityException, NoSuchMethodException { Invokable<?, ?> method = method(TestFormReplace.class, "twoForm", String.class); Object form = processor.apply(Invocation.create(method, ImmutableList.<Object> of("robot"))) .getPayload().getRawContent(); assertEquals(form, "slash=/robot&hyphen=-robot"); }
public void testCreatePostRequestNullNotOk1() throws SecurityException, NoSuchMethodException, IOException { Invokable<?, ?> method = method(TestPost.class, "postNonnull", String.class); try { GeneratedHttpRequest request = processor.apply(Invocation.create(method, Lists.<Object> newArrayList((String) null))); Assert.fail("call should have failed with illegal null parameter, not permitted " + request + " to be created"); } catch (NullPointerException e) { Assert.assertTrue(e.toString().indexOf("postNonnull parameter 1") >= 0, "Error message should have referred to 'parameter 1': " + e); } }
@Test public void testCorrect() throws SecurityException, NoSuchMethodException { Invokable<?, Object> testPayload = method(TestPayload.class, "testPayload", String.class); GeneratedHttpRequest request = GeneratedHttpRequest.builder() .invocation(Invocation.create(testPayload, ImmutableList.<Object> of("robot"))) .method("POST").endpoint("http://localhost").build(); GeneratedHttpRequest newRequest = binder().bindToRequest(request, ImmutableMap.<String, Object> of("fooble", "robot")); assertEquals(newRequest.getRequestLine(), request.getRequestLine()); assertEquals(newRequest.getPayload().getRawContent(), "name robot"); }
@BeforeClass void setUp() throws SecurityException, NoSuchMethodException { putBucket = GeneratedHttpRequest.builder() .method("PUT") .endpoint("https://adriancole-blobstore113.s3.amazonaws.com/") .invocation( Invocation.create(method(S3Client.class, "putBucketInRegion", String.class, String.class, PutBucketOptions[].class), Lists.<Object> newArrayList(null, "bucket"))).build(); }
public void testCreatePostRequestNullOk1() throws SecurityException, NoSuchMethodException, IOException { Invokable<?, ?> method = method(TestPost.class, "post", String.class); GeneratedHttpRequest request = processor.apply(Invocation.create(method, Lists.<Object> newArrayList((String) null))); assertRequestLineEquals(request, "POST http://localhost:9999 HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); assertPayloadEquals(request, null, "application/unknown", false); }
public void testGetLaunchPermissionForImage() throws SecurityException, NoSuchMethodException, IOException { Invokable<?, ?> method = method(AMIAsyncClient.class, "getLaunchPermissionForImageInRegion", String.class, String.class); GeneratedHttpRequest request = processor.createRequest(method, Lists.<Object> newArrayList(null, "imageId")); request = (GeneratedHttpRequest) request.getFilters().get(0).filter(request); assertRequestLineEquals(request, "POST https://ec2.us-east-1.amazonaws.com/ HTTP/1.1"); assertNonPayloadHeadersEqual(request, "Host: ec2.us-east-1.amazonaws.com\n"); assertPayloadEquals(request, getLaunchPermissionForImage.getPayload().getRawContent().toString(), "application/x-www-form-urlencoded", false); assertResponseParserClassEquals(method, request, ParseSax.class); assertSaxResponseParserClassEquals(method, PermissionHandler.class); assertFallbackClassEquals(method, null); checkFilters(request); }
public void oneTransformerWithContext() throws SecurityException, NoSuchMethodException { GeneratedHttpRequest request = GeneratedHttpRequest.builder() .method("GET") .endpoint("http://localhost") .invocation( Invocation.create(method(TestTransformers.class, "oneTransformerWithContext"), ImmutableList.of())).build(); Function<HttpResponse, ?> transformer = this.transformer.apply(request); assertEquals(transformer.getClass(), ReturnStringIf200Context.class); assertEquals(((ReturnStringIf200Context) transformer).request, request); }
public void testAnnotationConstructorFieldNamingStrategyCPAndNamed() throws Exception { AnnotationConstructorNamingStrategy strategy = new AnnotationConstructorNamingStrategy( ImmutableSet.of(ConstructorProperties.class), ImmutableSet.of(new ExtractNamed())); Invokable<SimpleTest, SimpleTest> constructor = strategy.getDeserializer(typeToken(SimpleTest.class)); assertNotNull(constructor); assertEquals(constructor.getParameters().size(), 4); assertEquals(strategy.translateName(constructor, 0), "aardvark"); assertEquals(strategy.translateName(constructor, 1), "bat"); assertEquals(strategy.translateName(constructor, 2), "coyote"); }
public void testPeanutButterDidntTurnIntoWine() { Wine wine = new Wine(); assertNotEquals(wine.getBackendType(), typeToken(PeanutButter.class)); try { wine.unwrap(typeToken(PeanutButter.class)); fail(); } catch (IllegalArgumentException e) { assertEquals(e.getMessage(), "backend type: org.jclouds.internal.BaseViewTest$Water not assignable from org.jclouds.internal.BaseViewTest$PeanutButter"); } }
public void testMethodFromClassAndParams() { @SuppressWarnings("rawtypes") Invokable<Set, Object> methodInSuper = method(Set.class, "equals", Object.class); assertEquals(methodInSuper.getOwnerType(), typeToken(Set.class)); assertEquals(methodInSuper.getParameters().get(0).getType().getRawType(), Object.class); }