mirror of https://github.com/apache/jclouds.git
Issue 695: Added JAXB test for Location
This commit is contained in:
parent
d8ef0a6d4f
commit
bf034c7b7a
|
@ -0,0 +1,111 @@
|
|||
/**
|
||||
* Licensed to jclouds, Inc. (jclouds) under one or more
|
||||
* contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. jclouds licenses this file
|
||||
* to you 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 org.jclouds.tmrk.enterprisecloud.xml;
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.collect.ImmutableSet;
|
||||
import com.google.inject.AbstractModule;
|
||||
import com.google.inject.Module;
|
||||
import com.google.inject.Provides;
|
||||
import org.jclouds.crypto.Crypto;
|
||||
import org.jclouds.date.internal.SimpleDateFormatDateService;
|
||||
import org.jclouds.http.HttpRequest;
|
||||
import org.jclouds.http.HttpResponse;
|
||||
import org.jclouds.http.functions.ParseSax;
|
||||
import org.jclouds.http.functions.ParseXMLWithJAXB;
|
||||
import org.jclouds.logging.config.NullLoggingModule;
|
||||
import org.jclouds.rest.AuthorizationException;
|
||||
import org.jclouds.rest.BaseRestClientTest;
|
||||
import org.jclouds.rest.RestContextSpec;
|
||||
import org.jclouds.rest.internal.RestAnnotationProcessor;
|
||||
import org.jclouds.tmrk.enterprisecloud.domain.Location;
|
||||
import org.jclouds.tmrk.enterprisecloud.domain.NamedResource;
|
||||
import org.jclouds.tmrk.enterprisecloud.domain.Task;
|
||||
import org.jclouds.tmrk.enterprisecloud.domain.Tasks;
|
||||
import org.jclouds.tmrk.enterprisecloud.features.LocationAsyncClient;
|
||||
import org.jclouds.tmrk.enterprisecloud.features.TaskAsyncClient;
|
||||
import org.testng.annotations.BeforeClass;
|
||||
import org.testng.annotations.BeforeMethod;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import javax.inject.Named;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URI;
|
||||
import java.util.Set;
|
||||
|
||||
import static org.jclouds.io.Payloads.newInputStreamPayload;
|
||||
import static org.jclouds.rest.RestContextFactory.contextSpec;
|
||||
import static org.jclouds.rest.RestContextFactory.createContextBuilder;
|
||||
import static org.testng.Assert.assertEquals;
|
||||
import static org.testng.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Tests behavior of JAXB parsing for Location
|
||||
*
|
||||
* @author Jason King
|
||||
*/
|
||||
@Test(groups = "unit", testName = "LocationJAXBParsingTest")
|
||||
public class LocationJAXBParsingTest extends BaseRestClientTest {
|
||||
|
||||
private Location expected;
|
||||
|
||||
@BeforeMethod
|
||||
public void setUp() {
|
||||
expected = Location.builder().href(URI.create("/cloudapi/ecloud/locations/1")).name("Terremark - Richardson").type("application/vnd.tmrk.cloud.location")
|
||||
.friendlyName("Terremark - Richardson").locode("DAC").iso3166("US-TX").build();
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
void setupFactory() {
|
||||
RestContextSpec<String, Integer> contextSpec = contextSpec("test", "http://localhost:9999", "1", "", "userfoo",
|
||||
"credentialFoo", String.class, Integer.class,
|
||||
ImmutableSet.<Module> of(new MockModule(), new NullLoggingModule(), new AbstractModule() {
|
||||
|
||||
@Override
|
||||
protected void configure() {}
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
@Provides
|
||||
@Named("exception")
|
||||
Set<String> exception() {
|
||||
throw new AuthorizationException();
|
||||
}
|
||||
|
||||
}));
|
||||
|
||||
injector = createContextBuilder(contextSpec).buildInjector();
|
||||
parserFactory = injector.getInstance(ParseSax.Factory.class);
|
||||
crypto = injector.getInstance(Crypto.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseLocationWithJAXB() throws Exception {
|
||||
Method method = LocationAsyncClient.class.getMethod("getLocationById",URI.class);
|
||||
HttpRequest request = factory(LocationAsyncClient.class).createRequest(method, new URI("/1"));
|
||||
assertResponseParserClassEquals(method, request, ParseXMLWithJAXB.class);
|
||||
|
||||
Function<HttpResponse, Location> parser = (Function<HttpResponse, Location>) RestAnnotationProcessor
|
||||
.createResponseParser(parserFactory, injector, method, request);
|
||||
|
||||
InputStream is = getClass().getResourceAsStream("/location.xml");
|
||||
Location location = parser.apply(new HttpResponse(200, "ok", newInputStreamPayload(is)));
|
||||
assertEquals(location, expected);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
<Location href="/cloudapi/ecloud/locations/1" name="Terremark - Richardson"
|
||||
type="application/vnd.tmrk.cloud.location"
|
||||
xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<FriendlyName>Terremark - Richardson</FriendlyName>
|
||||
<Locode>DAC</Locode>
|
||||
<ISO3166>US-TX</ISO3166>
|
||||
</Location>
|
Loading…
Reference in New Issue