diff --git a/src/main/java/br/com/autonomiccs/apacheCloudStack/client/ApacheCloudStackClient.java b/src/main/java/br/com/autonomiccs/apacheCloudStack/client/ApacheCloudStackClient.java index 9aa620c..b9d766d 100644 --- a/src/main/java/br/com/autonomiccs/apacheCloudStack/client/ApacheCloudStackClient.java +++ b/src/main/java/br/com/autonomiccs/apacheCloudStack/client/ApacheCloudStackClient.java @@ -85,15 +85,6 @@ * The client is a pair of URL and user credentials ({@link #apacheCloudStackUser}). */ public class ApacheCloudStackClient { - /** - * The suffix 'client' that is the endpoint to access Apache CloudStack. - */ - private final static String CLOUDSTACK_BASE_ENDPOINT_URL_SUFFIX = "client"; - /** - * The Apache CloudStack API endpoint - */ - private static final String APACHE_CLOUDSTACK_API_ENDPOINT = "/api"; - /** * This flag indicates if we are going to validate the server certificate in case of HTTPS connections. * The default value is 'true', meaning that we always validate the server HTTPS certificate. @@ -116,8 +107,8 @@ public class ApacheCloudStackClient { private Logger logger = LoggerFactory.getLogger(getClass()); /** - * The URL used to access Apache CloudStack API. - * Ex: https://cloud.domain.com/client + * The API URL used to access Apache CloudStack API. + * Ex: https://cloud.domain.com/client/api */ private String url; @@ -128,33 +119,11 @@ public class ApacheCloudStackClient { protected ApacheCloudStackUser apacheCloudStackUser; public ApacheCloudStackClient(String url, ApacheCloudStackUser apacheCloudStackUser) { - this.url = adjustUrlIfNeeded(url); + this.url = url; this.apacheCloudStackUser = apacheCloudStackUser; } - /** - * adds the suffix '{@value #CLOUDSTACK_BASE_ENDPOINT_URL_SUFFIX}' if it does have it. - * It uses the method {@link #appendUrlSuffix(String)} to execute the appending. - */ - protected String adjustUrlIfNeeded(String url) { - if (StringUtils.endsWith(url, "/client") || StringUtils.endsWith(url, "/client/")) { - return url; - } - return appendUrlSuffix(url); - } - - /** - * Appends the suffix '{@value #CLOUDSTACK_BASE_ENDPOINT_URL_SUFFIX}' at the end of the given URL. - * If it is needed, it will also add, a '/' before the suffix is appended to the URL. - */ - protected String appendUrlSuffix(String url) { - if (StringUtils.endsWith(url, "/")) { - return url + CLOUDSTACK_BASE_ENDPOINT_URL_SUFFIX; - } - return url + "/" + CLOUDSTACK_BASE_ENDPOINT_URL_SUFFIX; - } - /** * This method executes the given {@link ApacheCloudStackRequest}. * It will return the response as a plain {@link String}. @@ -321,7 +290,7 @@ protected void configureDomainForCookie(BasicClientCookie cookie) { * The content type configured for this request is 'application/x-www-form-urlencoded'. */ protected HttpPost createHttpPost() { - HttpPost httpPost = new HttpPost(url + APACHE_CLOUDSTACK_API_ENDPOINT); + HttpPost httpPost = new HttpPost(url); httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded"); return httpPost; } @@ -371,15 +340,14 @@ protected String getResponseAsString(CloseableHttpResponse response) throws IOEx * and then, if it needs, it creates the signature using the method {@link #createSignature(String)} and append it to the URL. */ protected String createApacheCloudStackApiUrlRequest(ApacheCloudStackRequest request, boolean shouldSignAppendSignature) { - StringBuilder urlRequest = new StringBuilder(url + APACHE_CLOUDSTACK_API_ENDPOINT); - urlRequest.append("?"); + StringBuilder urlRequest = new StringBuilder(url).append("?"); String queryString = createCommandString(request); urlRequest.append(queryString); if (shouldSignAppendSignature) { String signature = createSignature(queryString); - urlRequest.append("&signature=" + getUrlEncodedValue(signature)); + urlRequest.append("&signature=").append(getUrlEncodedValue(signature)); } return urlRequest.toString(); } diff --git a/src/test/java/br/com/autonomiccs/apacheCloudStack/client/ApacheCloudStackClientTest.java b/src/test/java/br/com/autonomiccs/apacheCloudStack/client/ApacheCloudStackClientTest.java index 1ba892d..fc72082 100644 --- a/src/test/java/br/com/autonomiccs/apacheCloudStack/client/ApacheCloudStackClientTest.java +++ b/src/test/java/br/com/autonomiccs/apacheCloudStack/client/ApacheCloudStackClientTest.java @@ -21,17 +21,9 @@ */ package br.com.autonomiccs.apacheCloudStack.client; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.net.MalformedURLException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.HashSet; -import java.util.List; -import java.util.Set; - +import br.com.autonomiccs.apacheCloudStack.client.beans.ApacheCloudStackUser; +import br.com.autonomiccs.apacheCloudStack.exceptions.ApacheCloudStackClientRequestRuntimeException; +import br.com.autonomiccs.apacheCloudStack.exceptions.ApacheCloudStackClientRuntimeException; import org.apache.http.Header; import org.apache.http.HeaderElement; import org.apache.http.HttpEntity; @@ -61,9 +53,16 @@ import org.mockito.Mockito; import org.mockito.runners.MockitoJUnitRunner; -import br.com.autonomiccs.apacheCloudStack.client.beans.ApacheCloudStackUser; -import br.com.autonomiccs.apacheCloudStack.exceptions.ApacheCloudStackClientRequestRuntimeException; -import br.com.autonomiccs.apacheCloudStack.exceptions.ApacheCloudStackClientRuntimeException; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.HashSet; +import java.util.List; +import java.util.Set; @RunWith(MockitoJUnitRunner.class) public class ApacheCloudStackClientTest { @@ -74,66 +73,13 @@ public class ApacheCloudStackClientTest { private ApacheCloudStackUser apacheCloudStackUser; private String cloudStackDomain = "cloud.domain.com"; - private String cloudStackUrl = "https://" + cloudStackDomain + "/client"; + private String cloudStackUrl = "https://" + cloudStackDomain + "/client/api"; @Before public void setup() { apacheCloudStackClient = Mockito.spy(new ApacheCloudStackClient(cloudStackUrl, apacheCloudStackUser)); } - @Test - public void adjustUrlIfNeededTestEndingWithSuffix() { - executeAndVerifyAdjustUrlIfNeededTest(cloudStackUrl, 0); - } - - @Test - public void adjustUrlIfNeededTestEndingWithoutSuffix() { - executeAndVerifyAdjustUrlIfNeededTest("https://cloud.domain.com", 1); - } - - private void executeAndVerifyAdjustUrlIfNeededTest(String givenCloudStackUrl, int appendCallTimes) { - String url = apacheCloudStackClient.adjustUrlIfNeeded(givenCloudStackUrl); - - Assert.assertEquals(cloudStackUrl, url); - Mockito.verify(apacheCloudStackClient, Mockito.times(appendCallTimes)).appendUrlSuffix(Mockito.anyString()); - } - - @Test - public void adjustUrlIfNeededTestUrlEndingWithoutSlashClient() { - String urlWithSuffix = apacheCloudStackClient.adjustUrlIfNeeded("https://cloud.domain.com/"); - Assert.assertEquals(cloudStackUrl, urlWithSuffix); - } - - @Test - public void adjustUrlIfNeededTestUrlEndingWithoutSlashAndClient() { - String urlWithSuffix = apacheCloudStackClient.adjustUrlIfNeeded("https://cloud.domain.com"); - Assert.assertEquals(cloudStackUrl, urlWithSuffix); - } - - @Test - public void adjustUrlIfNeededTestUrlEndingWithSlashClientWithoutLastSlash() { - String urlWithSuffix = apacheCloudStackClient.adjustUrlIfNeeded("https://cloud.domain.com/client"); - Assert.assertEquals(cloudStackUrl, urlWithSuffix); - } - - @Test - public void adjustUrlIfNeededTestUrlEndingWithSlashClientWithtLastSlash() { - String urlWithSuffix = apacheCloudStackClient.adjustUrlIfNeeded("https://cloud.domain.com/client/"); - Assert.assertEquals(cloudStackUrl + "/", urlWithSuffix); - } - - @Test - public void appendUrlSuffixTestEndingWithSlash(){ - String urlWithSuffix = apacheCloudStackClient.appendUrlSuffix("https://cloud.domain.com/"); - Assert.assertEquals(cloudStackUrl, urlWithSuffix); - } - - @Test - public void appendUrlSuffixTestEndingWithoutSlash() { - String urlWithSuffix = apacheCloudStackClient.appendUrlSuffix("https://cloud.domain.com"); - Assert.assertEquals(cloudStackUrl, urlWithSuffix); - } - @Test public void executeRequestTest() throws ClientProtocolException, IOException { configureMocksExecuteTestAndVerifyForMethodExecuteRequest(200); @@ -233,7 +179,7 @@ public void createApacheCloudStackApiUrlRequestTestWithSignature() { Mockito.doReturn(signatureValue).when(apacheCloudStackClient).createSignature(Mockito.eq(queryString)); String urlRequestReturned = apacheCloudStackClient.createApacheCloudStackApiUrlRequest(apacheCloudStackRequestMock, true); - Assert.assertEquals(cloudStackUrl + "/api?" + queryString + "&signature=" + signatureValue, urlRequestReturned); + Assert.assertEquals(cloudStackUrl + "?" + queryString + "&signature=" + signatureValue, urlRequestReturned); InOrder inOrder = Mockito.inOrder(apacheCloudStackClient); inOrder.verify(apacheCloudStackClient).createCommandString(Mockito.eq(apacheCloudStackRequestMock)); @@ -249,7 +195,7 @@ public void createApacheCloudStackApiUrlRequestTestWithoutSignature() { Mockito.doReturn(queryString).when(apacheCloudStackClient).createCommandString(Mockito.eq(apacheCloudStackRequestMock)); String urlRequestReturned = apacheCloudStackClient.createApacheCloudStackApiUrlRequest(apacheCloudStackRequestMock, false); - Assert.assertEquals(cloudStackUrl + "/api?" + queryString, urlRequestReturned); + Assert.assertEquals(cloudStackUrl + "?" + queryString, urlRequestReturned); InOrder inOrder = Mockito.inOrder(apacheCloudStackClient); inOrder.verify(apacheCloudStackClient).createCommandString(Mockito.eq(apacheCloudStackRequestMock)); @@ -544,7 +490,7 @@ public void configureDomainForCookieTest() { public void createHttpPostTest() throws MalformedURLException { HttpPost httpPost = apacheCloudStackClient.createHttpPost(); - Assert.assertEquals(cloudStackUrl + "/api", httpPost.getURI().toURL().toString()); + Assert.assertEquals(cloudStackUrl, httpPost.getURI().toURL().toString()); Assert.assertEquals("application/x-www-form-urlencoded", httpPost.getFirstHeader("Content-Type").getValue()); }