@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")); }
protected Optional<URI> getEndpointFor(Invocation invocation) { URI endpoint = getEndpointInParametersOrNull(invocation, injector); if (endpoint == null) { Endpoint annotation; if (invocation.getInvokable().isAnnotationPresent(Endpoint.class)) { annotation = invocation.getInvokable().getAnnotation(Endpoint.class); } else if (invocation.getInvokable().getOwnerType().getRawType().isAnnotationPresent(Endpoint.class)) { annotation = invocation.getInvokable().getOwnerType().getRawType().getAnnotation(Endpoint.class); } else { logger.trace("no annotations on class or invocation.getInvoked(): %s", invocation.getInvokable()); return Optional.absent(); } endpoint = injector.getInstance(Key.get(uriSupplierLiteral, annotation.value())).get(); } URI provider = injector.getInstance(Key.get(uriSupplierLiteral, org.jclouds.location.Provider.class)).get(); return Optional.fromNullable(addHostIfMissing(endpoint, provider)); }
private Multimap<String, String> buildHeaders(Multimap<String, ?> tokenValues, Invocation invocation) { Multimap<String, String> headers = LinkedHashMultimap.create(); addHeaderIfAnnotationPresentOnMethod(headers, invocation, tokenValues); for (Parameter headerParam : parametersWithAnnotation(invocation.getInvokable(), HeaderParam.class)) { Annotation key = headerParam.getAnnotation(HeaderParam.class); String value = invocation.getArgs().get(headerParam.hashCode()).toString(); value = replaceTokens(value, tokenValues); headers.put(((HeaderParam) key).value(), value); } addProducesIfPresentOnTypeOrMethod(headers, invocation); addConsumesIfPresentOnTypeOrMethod(headers, invocation); return headers; }
private Multimap<String, Object> addFormParams(Multimap<String, ?> tokenValues, Invocation invocation) { Multimap<String, Object> formMap = LinkedListMultimap.create(); if (invocation.getInvokable().getOwnerType().getRawType().isAnnotationPresent(FormParams.class)) { FormParams form = invocation.getInvokable().getOwnerType().getRawType().getAnnotation(FormParams.class); addForm(formMap, form, tokenValues); } if (invocation.getInvokable().isAnnotationPresent(FormParams.class)) { FormParams form = invocation.getInvokable().getAnnotation(FormParams.class); addForm(formMap, form, tokenValues); } for (Entry<String, Object> form : getFormParamKeyValues(invocation).entries()) { formMap.put(form.getKey(), replaceTokens(form.getValue().toString(), tokenValues)); } return formMap; }
public void testGetTask() throws SecurityException, NoSuchMethodException, IOException { Invokable<?, ?> method = method(TaskAsyncClient.class, "getTask", URI.class); GeneratedHttpRequest request = processor.createRequest(method, ImmutableList.<Object> of(URI .create("https://vcenterprise.bluelock.com/api/v1.0/task/1"))); assertRequestLineEquals(request, "GET https://vcenterprise.bluelock.com/api/v1.0/task/1 HTTP/1.1"); assertNonPayloadHeadersEqual(request, "Accept: application/vnd.vmware.vcloud.task+xml\n"); assertPayloadEquals(request, null, null, false); assertResponseParserClassEquals(method, request, ParseSax.class); assertSaxResponseParserClassEquals(method, TaskHandler.class); assertFallbackClassEquals(method, NullOnNotFoundOr404.class); checkFilters(request); }
@Test(expectedExceptions = NullPointerException.class) public void testAddHostNullWithHost() throws Exception { assertNull(RestAnnotationProcessor.addHostIfMissing(null, null)); }
@Test public void testOneEndpointParam() throws SecurityException, NoSuchMethodException, ExecutionException { Invokable<?, ?> method = method(TestEndpointParams.class, "oneEndpointParam", String.class); URI uri = RestAnnotationProcessor.getEndpointInParametersOrNull( Invocation.create(method, ImmutableList.<Object> of("robot")), injector); assertEquals(uri, URI.create("robot")); }
public void testVersions() throws SecurityException, NoSuchMethodException, IOException { Invokable<?, ?> method = method(VCloudVersionsAsyncClient.class, "getSupportedVersions"); GeneratedHttpRequest request = processor.createRequest(method, ImmutableList.of()); assertEquals(request.getRequestLine(), "GET http://localhost:8080/versions HTTP/1.1"); assertNonPayloadHeadersEqual(request, ""); assertPayloadEquals(request, null, null, false); assertResponseParserClassEquals(method, request, ParseSax.class); assertSaxResponseParserClassEquals(method, SupportedVersionsHandler.class); assertFallbackClassEquals(method, null); checkFilters(request); }
private Multimap<String, Object> addFormParams(Multimap<String, ?> tokenValues, Invocation invocation) { Multimap<String, Object> formMap = LinkedListMultimap.create(); if (invocation.getInvokable().getOwnerType().getRawType().isAnnotationPresent(FormParams.class)) { FormParams form = invocation.getInvokable().getOwnerType().getRawType().getAnnotation(FormParams.class); addForm(formMap, form, tokenValues); } if (invocation.getInvokable().isAnnotationPresent(FormParams.class)) { FormParams form = invocation.getInvokable().getAnnotation(FormParams.class); addForm(formMap, form, tokenValues); } for (Entry<String, Object> form : getFormParamKeyValues(invocation).entries()) { formMap.put(form.getKey(), replaceTokens(form.getValue().toString(), tokenValues)); } return formMap; }
@Test public void testAddHostOriginalHasHost() throws Exception { URI original = new URI("http://hashost/foo"); URI result = RestAnnotationProcessor.addHostIfMissing(original, new URI("http://foo")); assertEquals(original, result); }
@Test public void testOneEndpointParam() throws SecurityException, NoSuchMethodException, ExecutionException { Invokable<?, ?> method = method(TestEndpointParams.class, "oneEndpointParam", String.class); URI uri = RestAnnotationProcessor.getEndpointInParametersOrNull( Invocation.create(method, ImmutableList.<Object> of("robot")), injector); assertEquals(uri, URI.create("robot")); }
public void testCreateVolumeWithSnapshot() throws SecurityException, NoSuchMethodException, IOException { Invokable<?, ?> method = method(VolumeAsyncClient.class, "createVolumeFromSnapshotInZone", String.class, String.class, String.class); GeneratedHttpRequest httpRequest = processor.createRequest(method, ImmutableList.<Object> of("jclouds-volume", 999L, 111l)); assertRequestLineEquals(httpRequest, createVolumeFromSnapshot.getRequestLine()); assertNonPayloadHeadersEqual(httpRequest, "Accept: application/json\n"); assertPayloadEquals(httpRequest, null, null, false); assertSaxResponseParserClassEquals(method, null); checkFilters(httpRequest); }
@Test public void testBuildOneHeader() throws SecurityException, NoSuchMethodException { Invokable<?, ?> method = method(TestHeader.class, "oneHeader", String.class); Multimap<String, String> headers = processor.apply(Invocation.create(method, ImmutableList.<Object> of("robot"))).getHeaders(); assertEquals(headers.size(), 1); assertEquals(headers.get("x-amz-copy-source"), ImmutableList.of("/robot")); }
private Multimap<String, String> buildHeaders(Multimap<String, ?> tokenValues, Invocation invocation) { Multimap<String, String> headers = LinkedHashMultimap.create(); addHeaderIfAnnotationPresentOnMethod(headers, invocation, tokenValues); for (Parameter headerParam : parametersWithAnnotation(invocation.getInvokable(), HeaderParam.class)) { Annotation key = headerParam.getAnnotation(HeaderParam.class); String value = invocation.getArgs().get(headerParam.hashCode()).toString(); value = replaceTokens(value, tokenValues); headers.put(((HeaderParam) key).value(), value); } addProducesIfPresentOnTypeOrMethod(headers, invocation); addConsumesIfPresentOnTypeOrMethod(headers, invocation); return headers; }
protected Optional<URI> getEndpointFor(Invocation invocation) { URI endpoint = getEndpointInParametersOrNull(invocation, injector); if (endpoint == null) { Endpoint annotation; if (invocation.getInvokable().isAnnotationPresent(Endpoint.class)) { annotation = invocation.getInvokable().getAnnotation(Endpoint.class); } else if (invocation.getInvokable().getOwnerType().getRawType().isAnnotationPresent(Endpoint.class)) { annotation = invocation.getInvokable().getOwnerType().getRawType().getAnnotation(Endpoint.class); } else { logger.trace("no annotations on class or invocation.getInvoked(): %s", invocation.getInvokable()); return Optional.absent(); } endpoint = injector.getInstance(Key.get(uriSupplierLiteral, annotation.value())).get(); } URI provider = injector.getInstance(Key.get(uriSupplierLiteral, org.jclouds.location.Provider.class)).get(); return Optional.fromNullable(addHostIfMissing(endpoint, provider)); }
private Multimap<String, Object> addFormParams(Multimap<String, ?> tokenValues, Invocation invocation) { Multimap<String, Object> formMap = LinkedListMultimap.create(); if (invocation.getInvokable().getOwnerType().getRawType().isAnnotationPresent(FormParams.class)) { FormParams form = invocation.getInvokable().getOwnerType().getRawType().getAnnotation(FormParams.class); addForm(formMap, form, tokenValues); } if (invocation.getInvokable().isAnnotationPresent(FormParams.class)) { FormParams form = invocation.getInvokable().getAnnotation(FormParams.class); addForm(formMap, form, tokenValues); } for (Entry<String, Object> form : getFormParamKeyValues(invocation).entries()) { formMap.put(form.getKey(), replaceTokens(form.getValue().toString(), tokenValues)); } return formMap; }
@Test(expectedExceptions = NullPointerException.class) public void testAddHostNullWithHost() throws Exception { assertNull(RestAnnotationProcessor.addHostIfMissing(null, null)); }
@Test(expectedExceptions = IllegalStateException.class) public void testTwoDifferentEndpointParams() throws SecurityException, NoSuchMethodException, ExecutionException { Invokable<?, ?> method = method(TestEndpointParams.class, "twoEndpointParams", String.class, String.class); RestAnnotationProcessor.getEndpointInParametersOrNull( Invocation.create(method, ImmutableList.<Object> of("robot", "egg")), injector); }
public void testAuthenticateStorage() throws SecurityException, NoSuchMethodException, IOException { Invokable<?, ?> method = method(OpenStackAuthAsyncClient.class, "authenticateStorage", String.class, String.class); GeneratedHttpRequest httpRequest = processor.createRequest(method, ImmutableList.<Object> of("foo", "bar")); assertRequestLineEquals(httpRequest, "GET http://localhost:8080/v1.0 HTTP/1.1"); assertNonPayloadHeadersEqual(httpRequest, "Accept: */*\nHost: localhost:8080\nX-Storage-Pass: bar\nX-Storage-User: foo\n"); assertPayloadEquals(httpRequest, null, null, false); assertResponseParserClassEquals(method, httpRequest, ParseAuthenticationResponseFromHeaders.class); assertSaxResponseParserClassEquals(method, null); assertFallbackClassEquals(method, MapHttp4xxCodesToExceptions.class); }
@Test public void testOneIntHeader() throws SecurityException, NoSuchMethodException, ExecutionException { Invokable<?, ?> method = method(TestHeaders.class, "oneIntHeader", int.class); Multimap<String, String> headers = processor.apply(Invocation.create(method, ImmutableList.<Object> of(1))).getHeaders(); assertEquals(headers.size(), 1); assertEquals(headers.get("header"), ImmutableList.of("1")); }