[JCLOUDS-43] add TransactionApi and tests to ultradns-ws provider

This commit is contained in:
adriancole 2013-05-15 10:16:43 -07:00
parent 7923009eb5
commit b021d743fd
16 changed files with 408 additions and 0 deletions

View File

@ -36,6 +36,7 @@ import org.jclouds.ultradns.ws.features.ResourceRecordApi;
import org.jclouds.ultradns.ws.features.RoundRobinPoolApi;
import org.jclouds.ultradns.ws.features.TaskApi;
import org.jclouds.ultradns.ws.features.TrafficControllerPoolApi;
import org.jclouds.ultradns.ws.features.TransactionApi;
import org.jclouds.ultradns.ws.features.ZoneApi;
import org.jclouds.ultradns.ws.filters.SOAPWrapWithPasswordAuth;
import org.jclouds.ultradns.ws.xml.AccountHandler;
@ -127,4 +128,10 @@ public interface UltraDNSWSApi extends Closeable {
*/
@Delegate
TaskApi getTaskApi();
/**
* Provides access to Transaction features.
*/
@Delegate
TransactionApi getTransactionApi();
}

View File

@ -18,6 +18,8 @@
*/
package org.jclouds.ultradns.ws;
import org.jclouds.rest.InsufficientResourcesException;
/**
* Exceptions likely to be encountered when using {@link UltraDNSWSApi}
*
@ -46,4 +48,15 @@ public interface UltraDNSWSExceptions {
super(message, cause);
}
}
/**
* Error 9010: Ultra API only allows 3 concurrent transactions per user
*/
public static class TooManyTransactionsException extends InsufficientResourcesException {
private static final long serialVersionUID = 1L;
public TooManyTransactionsException(String message, Throwable cause) {
super(message, cause);
}
}
}

View File

@ -0,0 +1,85 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF 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.ultradns.ws.features;
import javax.inject.Named;
import javax.ws.rs.POST;
import org.jclouds.Fallbacks.VoidOnNotFoundOr404;
import org.jclouds.rest.ResourceNotFoundException;
import org.jclouds.rest.annotations.Fallback;
import org.jclouds.rest.annotations.Payload;
import org.jclouds.rest.annotations.PayloadParam;
import org.jclouds.rest.annotations.RequestFilters;
import org.jclouds.rest.annotations.VirtualHost;
import org.jclouds.rest.annotations.XMLResponseParser;
import org.jclouds.ultradns.ws.UltraDNSWSExceptions.TooManyTransactionsException;
import org.jclouds.ultradns.ws.filters.SOAPWrapWithPasswordAuth;
import org.jclouds.ultradns.ws.xml.ElementTextHandler;
/**
*
* @see <a href="https://ultra-api.ultradns.com:8443/UltraDNS_WS/v01?wsdl" />
* @see <a href="https://www.ultradns.net/api/NUS_API_XML_SOAP.pdf" />
* @author Adrian Cole
*/
@RequestFilters(SOAPWrapWithPasswordAuth.class)
@VirtualHost
public interface TransactionApi {
/**
* Starts a transaction, if possible. Note that 3 simultaneous ones are
* allowed per account, and they have a 1 hr timeout. All write commands will
* use this transaction until delete is called.
*
* @return id of the transaction created
* @throws TooManyTransactionsException
* if the maximum concurrent exception limit was hit.
*/
@Named("startTransaction")
@POST
@XMLResponseParser(ElementTextHandler.TransactionID.class)
@Payload("<v01:startTransaction/>")
String start() throws TooManyTransactionsException;
/**
* This request commits all of a transactions requests and writes them to
* the Neustar Ultra Services
*
* @param transactionID
* transaction id to commit.
* @throws ResourceNotFoundException
* if the transaction has already been committed or never existed.
*/
@Named("commitTransaction")
@POST
@Payload("<v01:commitTransaction><transactionID>{transactionID}</transactionID></v01:commitTransaction>")
void commit(@PayloadParam("transactionID") String transactionID) throws ResourceNotFoundException;
/**
* This request rolls back any changes included in a transaction. This will
* not error, if the transaction has timed out or does not exist.
*
* @param transactionID
* transaction id to rollback.
*/
@Named("rollbackTransaction")
@POST
@Payload("<v01:rollbackTransaction><transactionID>{transactionID}</transactionID></v01:rollbackTransaction>")
@Fallback(VoidOnNotFoundOr404.class)
void rollback(@PayloadParam("transactionID") String transactionID);
}

View File

@ -33,6 +33,7 @@ import org.jclouds.rest.ResourceNotFoundException;
import org.jclouds.ultradns.ws.UltraDNSWSError;
import org.jclouds.ultradns.ws.UltraDNSWSExceptions.DirectionalGroupOverlapException;
import org.jclouds.ultradns.ws.UltraDNSWSExceptions.ResourceAlreadyExistsException;
import org.jclouds.ultradns.ws.UltraDNSWSExceptions.TooManyTransactionsException;
import org.jclouds.ultradns.ws.UltraDNSWSResponseException;
import org.jclouds.ultradns.ws.xml.UltraWSExceptionHandler;
@ -79,6 +80,10 @@ public class UltraDNSWSErrorHandler implements HttpErrorHandler {
*/
static final class ErrorCodes {
static final int UNKNOWN = 0;
/**
* No transaction with Id Y found for the user Y
*/
static final int TX_NOT_FOUND = 1602;
/**
* Zone does not exist in the system.
*/
@ -131,6 +136,10 @@ public class UltraDNSWSErrorHandler implements HttpErrorHandler {
* Geolocation/Source IP overlap(s) found
*/
static final int DIRECTIONALPOOL_OVERLAP = 7021;
/**
* Ultra API only allows 3 concurrent transactions per user
*/
static final int EXCEEDED_TX_LIMIT = 9010;
}
private Exception refineException(UltraDNSWSResponseException exception) {
@ -141,6 +150,7 @@ public class UltraDNSWSErrorHandler implements HttpErrorHandler {
return exception;
if (exception.getError().getDescription().get().indexOf("Cannot find") == -1)
return exception;
case ErrorCodes.TX_NOT_FOUND:
case ErrorCodes.ZONE_NOT_FOUND:
case ErrorCodes.RESOURCE_RECORD_NOT_FOUND:
case ErrorCodes.ACCOUNT_NOT_FOUND:
@ -157,6 +167,8 @@ public class UltraDNSWSErrorHandler implements HttpErrorHandler {
return new ResourceAlreadyExistsException(message, exception);
case ErrorCodes.DIRECTIONALPOOL_OVERLAP:
return new DirectionalGroupOverlapException(message, exception);
case ErrorCodes.EXCEEDED_TX_LIMIT:
return new TooManyTransactionsException(message, exception);
}
return exception;
}

View File

@ -66,6 +66,12 @@ public abstract class ElementTextHandler extends ParseSax.HandlerForGeneratedReq
}
}
public static class TransactionID extends ElementTextHandler {
public TransactionID() {
super("transactionId");
}
}
private final String textElement;
private StringBuilder currentText = new StringBuilder();

View File

@ -0,0 +1,99 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF 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.ultradns.ws.features;
import static com.google.common.net.HttpHeaders.HOST;
import static javax.ws.rs.HttpMethod.POST;
import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
import static javax.ws.rs.core.Response.Status.OK;
import static org.testng.Assert.assertEquals;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.rest.ResourceNotFoundException;
import org.jclouds.ultradns.ws.UltraDNSWSApi;
import org.jclouds.ultradns.ws.UltraDNSWSExceptions.TooManyTransactionsException;
import org.jclouds.ultradns.ws.internal.BaseUltraDNSWSApiExpectTest;
import org.testng.annotations.Test;
/**
* @author Adrian Cole
*/
@Test(groups = "unit", testName = "TransactionApiExpectTest")
public class TransactionApiExpectTest extends BaseUltraDNSWSApiExpectTest {
HttpRequest start = HttpRequest.builder().method(POST)
.endpoint("https://ultra-api.ultradns.com:8443/UltraDNS_WS/v01")
.addHeader(HOST, "ultra-api.ultradns.com:8443")
.payload(payloadFromResourceWithContentType("/start_tx.xml", "application/xml")).build();
HttpResponse startResponse = HttpResponse.builder().statusCode(OK.getStatusCode())
.payload(payloadFromResourceWithContentType("/tx_started.xml", "application/xml")).build();
public void testStartWhenResponseIs2xx() {
UltraDNSWSApi success = requestSendsResponse(start, startResponse);
assertEquals(success.getTransactionApi().start().toString(), "jclouds-37562");
}
HttpResponse tooManyResponse = HttpResponse.builder().message("Server Error").statusCode(INTERNAL_SERVER_ERROR.getStatusCode())
.payload(payloadFromResource("/tx_toomany.xml")).build();
@Test(expectedExceptions = TooManyTransactionsException.class, expectedExceptionsMessageRegExp = "Ultra API only allows 3 concurrent transactions per user")
public void testStartWhenResponseError9010() {
UltraDNSWSApi tooMany = requestSendsResponse(start, tooManyResponse);
tooMany.getTransactionApi().start();
}
HttpRequest commit = HttpRequest.builder().method(POST)
.endpoint("https://ultra-api.ultradns.com:8443/UltraDNS_WS/v01")
.addHeader(HOST, "ultra-api.ultradns.com:8443")
.payload(payloadFromResourceWithContentType("/commit_tx.xml", "application/xml")).build();
HttpResponse commitResponse = HttpResponse.builder().statusCode(OK.getStatusCode())
.payload(payloadFromResourceWithContentType("/tx_committed.xml", "application/xml")).build();
public void testCommitWhenResponseIs2xx() {
UltraDNSWSApi success = requestSendsResponse(commit, commitResponse);
success.getTransactionApi().commit("jclouds-37562");
}
HttpResponse txDoesntExist = HttpResponse.builder().message("Server Error").statusCode(INTERNAL_SERVER_ERROR.getStatusCode())
.payload(payloadFromResource("/tx_doesnt_exist.xml")).build();
@Test(expectedExceptions = ResourceNotFoundException.class, expectedExceptionsMessageRegExp = "No transaction with Id AAAAAAAAAAAAAAAA found for the user .*")
public void testCommitWhenResponseError1602() {
UltraDNSWSApi notFound = requestSendsResponse(commit, txDoesntExist);
notFound.getTransactionApi().commit("jclouds-37562");
}
HttpRequest rollback = HttpRequest.builder().method(POST)
.endpoint("https://ultra-api.ultradns.com:8443/UltraDNS_WS/v01")
.addHeader(HOST, "ultra-api.ultradns.com:8443")
.payload(payloadFromResourceWithContentType("/rollback_tx.xml", "application/xml")).build();
HttpResponse rollbackResponse = HttpResponse.builder().statusCode(OK.getStatusCode())
.payload(payloadFromResourceWithContentType("/tx_rolledback.xml", "application/xml")).build();
public void testRollbackWhenResponseIs2xx() {
UltraDNSWSApi success = requestSendsResponse(rollback, rollbackResponse);
success.getTransactionApi().rollback("jclouds-37562");
}
public void testRollbackWhenResponseError1602IsOK() {
UltraDNSWSApi notFound = requestSendsResponse(rollback, txDoesntExist);
notFound.getTransactionApi().rollback("jclouds-37562");
}
}

View File

@ -0,0 +1,78 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF 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.ultradns.ws.features;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import java.util.List;
import org.jclouds.rest.ResourceNotFoundException;
import org.jclouds.ultradns.ws.UltraDNSWSExceptions.TooManyTransactionsException;
import org.jclouds.ultradns.ws.internal.BaseUltraDNSWSApiLiveTest;
import org.testng.annotations.Test;
import com.google.common.collect.Lists;
/**
* @author Adrian Cole
*/
@Test(groups = "live", singleThreaded = true, testName = "TransactionApiLiveTest")
public class TransactionApiLiveTest extends BaseUltraDNSWSApiLiveTest {
@Test
public void commitTransaction() {
String tx = api().start();
assertNotNull(tx);
api().commit(tx);
}
@Test
public void rollbackTransaction() {
String tx = api().start();
assertNotNull(tx);
api().rollback(tx);
}
@Test(expectedExceptions = TooManyTransactionsException.class, expectedExceptionsMessageRegExp = "Ultra API only allows 3 concurrent transactions per user")
public void only3Transactions() {
List<String> txIds = Lists.newArrayList();
try {
while (true)
txIds.add(api().start());
} finally {
for (String tx : txIds)
api().rollback(tx);
assertEquals(txIds.size(), 3);
}
}
@Test(expectedExceptions = ResourceNotFoundException.class, expectedExceptionsMessageRegExp = "No transaction with Id AAAAAAAAAAAAAAAA found for the user .*")
public void commitTransactionWhenNotFound() {
api().commit("AAAAAAAAAAAAAAAA");
}
@Test
public void testRollbackTransactionWhenNotFound() {
api().rollback("AAAAAAAAAAAAAAAA");
}
protected TransactionApi api() {
return api.getTransactionApi();
}
}

View File

@ -35,6 +35,7 @@ import org.jclouds.io.Payload;
import org.jclouds.rest.ResourceNotFoundException;
import org.jclouds.ultradns.ws.UltraDNSWSExceptions.DirectionalGroupOverlapException;
import org.jclouds.ultradns.ws.UltraDNSWSExceptions.ResourceAlreadyExistsException;
import org.jclouds.ultradns.ws.UltraDNSWSExceptions.TooManyTransactionsException;
import org.jclouds.ultradns.ws.UltraDNSWSResponseException;
import org.testng.annotations.Test;
@ -364,6 +365,54 @@ public class UltraDNSWSErrorHandlerTest {
assertEquals(exception.getError().getCode(), 2705);
}
@Test
public void testCode1602SetsResourceNotFoundException() throws IOException {
HttpRequest request = HttpRequest.builder().method(POST)
.endpoint("https://ultra-api.ultradns.com:8443/UltraDNS_WS/v01")
.addHeader(HOST, "ultra-api.ultradns.com:8443")
.payload(payloadFromResource("/delete_directionalrecord.xml")).build();
HttpCommand command = new HttpCommand(request);
HttpResponse response = HttpResponse.builder()
.message(INTERNAL_SERVER_ERROR.getReasonPhrase())
.statusCode(INTERNAL_SERVER_ERROR.getStatusCode())
.payload(payloadFromResource("/tx_doesnt_exist.xml")).build();
function.handleError(command, response);
assertEquals(command.getException().getClass(), ResourceNotFoundException.class);
assertEquals(command.getException().getMessage(), "No transaction with Id AAAAAAAAAAAAAAAA found for the user jclouds");
UltraDNSWSResponseException exception = UltraDNSWSResponseException.class.cast(command.getException().getCause());
assertEquals(exception.getMessage(), "Error 1602: No transaction with Id AAAAAAAAAAAAAAAA found for the user jclouds");
assertEquals(exception.getError().getDescription().get(), "No transaction with Id AAAAAAAAAAAAAAAA found for the user jclouds");
assertEquals(exception.getError().getCode(), 1602);
}
@Test
public void testCode9010SetsTooManyTransactionsException() throws IOException {
HttpRequest request = HttpRequest.builder().method(POST)
.endpoint("https://ultra-api.ultradns.com:8443/UltraDNS_WS/v01")
.addHeader(HOST, "ultra-api.ultradns.com:8443")
.payload(payloadFromResource("/create_directionalrecord_newgroup.xml")).build();
HttpCommand command = new HttpCommand(request);
HttpResponse response = HttpResponse.builder()
.message(INTERNAL_SERVER_ERROR.getReasonPhrase())
.statusCode(INTERNAL_SERVER_ERROR.getStatusCode())
.payload(payloadFromResource("/tx_toomany.xml")).build();
function.handleError(command, response);
assertEquals(command.getException().getClass(), TooManyTransactionsException.class);
assertEquals(command.getException().getMessage(), "Ultra API only allows 3 concurrent transactions per user");
UltraDNSWSResponseException exception = UltraDNSWSResponseException.class.cast(command.getException().getCause());
assertEquals(exception.getMessage(), "Error 9010: Ultra API only allows 3 concurrent transactions per user");
assertEquals(exception.getError().getDescription().get(), "Ultra API only allows 3 concurrent transactions per user");
assertEquals(exception.getError().getCode(), 9010);
}
@Test
public void testCode7021SetsDirectionalGroupOverlapException() throws IOException {
HttpRequest request = HttpRequest.builder().method(POST)

View File

@ -0,0 +1 @@
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v01="http://webservice.api.ultra.neustar.com/v01/"><soapenv:Header><wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><wsse:UsernameToken><wsse:Username>identity</wsse:Username><wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">credential</wsse:Password></wsse:UsernameToken></wsse:Security></soapenv:Header><soapenv:Body><v01:commitTransaction><transactionID>jclouds-37562</transactionID></v01:commitTransaction></soapenv:Body></soapenv:Envelope>

View File

@ -0,0 +1 @@
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v01="http://webservice.api.ultra.neustar.com/v01/"><soapenv:Header><wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><wsse:UsernameToken><wsse:Username>identity</wsse:Username><wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">credential</wsse:Password></wsse:UsernameToken></wsse:Security></soapenv:Header><soapenv:Body><v01:rollbackTransaction><transactionID>jclouds-37562</transactionID></v01:rollbackTransaction></soapenv:Body></soapenv:Envelope>

View File

@ -0,0 +1 @@
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v01="http://webservice.api.ultra.neustar.com/v01/"><soapenv:Header><wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><wsse:UsernameToken><wsse:Username>identity</wsse:Username><wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">credential</wsse:Password></wsse:UsernameToken></wsse:Security></soapenv:Header><soapenv:Body><v01:startTransaction/></soapenv:Body></soapenv:Envelope>

View File

@ -0,0 +1,8 @@
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns1:commitTransactionResponse
xmlns:ns1="http://webservice.api.ultra.neustar.com/v01/">
<result xmlns:ns2="http://schema.ultraservice.neustar.com/v01/">Successful</result>
</ns1:commitTransactionResponse>
</soap:Body>
</soap:Envelope>

View File

@ -0,0 +1,16 @@
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring>Fault occurred while processing.</faultstring>
<detail>
<ns1:UltraWSException xmlns:ns1="http://webservice.api.ultra.neustar.com/v01/">
<errorCode xmlns:ns2="http://schema.ultraservice.neustar.com/v01/"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="xs:int">1602</errorCode>
<errorDescription xmlns:ns2="http://schema.ultraservice.neustar.com/v01/">No transaction with Id AAAAAAAAAAAAAAAA found for the user jclouds</errorDescription>
</ns1:UltraWSException>
</detail>
</soap:Fault>
</soap:Body>
</soap:Envelope>

View File

@ -0,0 +1,8 @@
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns1:rollbackTransactionResponse
xmlns:ns1="http://webservice.api.ultra.neustar.com/v01/">
<result xmlns:ns2="http://schema.ultraservice.neustar.com/v01/">Successful</result>
</ns1:rollbackTransactionResponse>
</soap:Body>
</soap:Envelope>

View File

@ -0,0 +1,8 @@
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns1:startTransactionResponse
xmlns:ns1="http://webservice.api.ultra.neustar.com/v01/">
<transactionId xmlns:ns2="http://schema.ultraservice.neustar.com/v01/">jclouds-37562</transactionId>
</ns1:startTransactionResponse>
</soap:Body>
</soap:Envelope>

View File

@ -0,0 +1,16 @@
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring>Fault occurred while processing.</faultstring>
<detail>
<ns1:UltraWSException xmlns:ns1="http://webservice.api.ultra.neustar.com/v01/">
<errorCode xmlns:ns2="http://schema.ultraservice.neustar.com/v01/"
xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="xs:int">9010</errorCode>
<errorDescription xmlns:ns2="http://schema.ultraservice.neustar.com/v01/">Ultra API only allows 3 concurrent transactions per user</errorDescription>
</ns1:UltraWSException>
</detail>
</soap:Fault>
</soap:Body>
</soap:Envelope>