From 947d025b6cd9cfe93b28c3c176fa600e04b10c18 Mon Sep 17 00:00:00 2001 From: Geetika Batra Date: Thu, 22 Feb 2018 19:59:23 +0530 Subject: [PATCH] Updated the URl for scale (#170) Updated the URl for 3scale --- .../eclipse/core/ThreeScaleAPIProvider.java | 24 ++++---------- .../core/tests/ThreeScaleAPIProviderTest.java | 33 +++++++++---------- 2 files changed, 22 insertions(+), 35 deletions(-) diff --git a/plugins/com.redhat.fabric8analytics.lsp.eclipse.core/src/com/redhat/fabric8analytics/lsp/eclipse/core/ThreeScaleAPIProvider.java b/plugins/com.redhat.fabric8analytics.lsp.eclipse.core/src/com/redhat/fabric8analytics/lsp/eclipse/core/ThreeScaleAPIProvider.java index 59b1a35..fb91720 100644 --- a/plugins/com.redhat.fabric8analytics.lsp.eclipse.core/src/com/redhat/fabric8analytics/lsp/eclipse/core/ThreeScaleAPIProvider.java +++ b/plugins/com.redhat.fabric8analytics.lsp.eclipse.core/src/com/redhat/fabric8analytics/lsp/eclipse/core/ThreeScaleAPIProvider.java @@ -15,12 +15,9 @@ import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.entity.StringEntity; +import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; -import org.apache.http.message.BasicHeader; -import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import org.json.JSONException; import org.json.JSONObject; @@ -33,9 +30,9 @@ */ public class ThreeScaleAPIProvider { - public static final String THREE_SCALE_URL = "https://3scale-connect.api.openshift.io/get-route"; + public static final String THREE_SCALE_URL = "https://f8a-connect-api-2445582058137.production.gw.apicast.io:443/get-endpoints?user_key=%s"; - public static final String SERVICE_ID = "2555417754949"; + public static final String SERVICE_ID = "ad467b765e5c8a8a5ca745a1f32b8487"; private String token; @@ -55,17 +52,10 @@ public ThreeScaleAPIProvider(String token) { public ThreeScaleData register3Scale() throws ThreeScaleAPIException { CloseableHttpClient client = createClient(); try { - JSONObject urlObject = new JSONObject(); - urlObject.put("auth_token", token); - urlObject.put("service_id" , SERVICE_ID); - - StringEntity se = new StringEntity(urlObject.toString()); - se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); - - HttpPost post = new HttpPost(THREE_SCALE_URL); - post.setEntity(se); - - HttpResponse response = client.execute(post); + String queryUrl = String.format(THREE_SCALE_URL, SERVICE_ID); + HttpGet get = new HttpGet(queryUrl); + get.addHeader("Authorization", token); + HttpResponse response = client.execute(get); int responseCode = response.getStatusLine().getStatusCode(); diff --git a/tests/com.redhat.fabric8analytics.lsp.eclipse.core.tests/src/com/redhat/fabric8analytics/lsp/eclipse/core/tests/ThreeScaleAPIProviderTest.java b/tests/com.redhat.fabric8analytics.lsp.eclipse.core.tests/src/com/redhat/fabric8analytics/lsp/eclipse/core/tests/ThreeScaleAPIProviderTest.java index a69bc62..8272bab 100644 --- a/tests/com.redhat.fabric8analytics.lsp.eclipse.core.tests/src/com/redhat/fabric8analytics/lsp/eclipse/core/tests/ThreeScaleAPIProviderTest.java +++ b/tests/com.redhat.fabric8analytics.lsp.eclipse.core.tests/src/com/redhat/fabric8analytics/lsp/eclipse/core/tests/ThreeScaleAPIProviderTest.java @@ -10,9 +10,8 @@ *******************************************************************************/ package com.redhat.fabric8analytics.lsp.eclipse.core.tests; - -import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.startsWith; import static org.junit.Assert.assertThat; import static org.mockito.Matchers.any; import static org.mockito.Mockito.atLeastOnce; @@ -23,13 +22,12 @@ import java.io.ByteArrayInputStream; import java.io.IOException; -import org.apache.commons.io.IOUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpStatus; import org.apache.http.StatusLine; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; -import org.apache.http.client.methods.HttpPost; +import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.eclipse.core.runtime.CoreException; import org.junit.Before; @@ -50,6 +48,8 @@ public class ThreeScaleAPIProviderTest { private static final String TOKEN = "mytoken"; + private static final String URL = "www.myurl.com"; + private static final String JSON_ENDPOINTS = "{\"prod\":" + PROD + ", \"stage\":"+ STAGE +"}"; private static final String JSON = "{\"endpoints\":" + JSON_ENDPOINTS + ", \"user_key\": "+ USER_KEY +"}"; @@ -80,32 +80,29 @@ public void checkNullToken() { } @Test - public void register3Scale_createPost() throws UnsupportedOperationException, IOException, ThreeScaleAPIException { - when(httpClient.execute(any(HttpPost.class))).thenReturn(httpResponse); + public void register3Scale_createGet() throws UnsupportedOperationException, IOException, ThreeScaleAPIException { + when(httpClient.execute(any(HttpGet.class))).thenReturn(httpResponse); when(httpResponse.getStatusLine()).thenReturn(statusLine); when(httpResponse.getEntity()).thenReturn(httpEntity); when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream(JSON.getBytes())); when(statusLine.getStatusCode()).thenReturn(HttpStatus.SC_OK); - final ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(HttpPost.class); + final ArgumentCaptor argumentCaptor = ArgumentCaptor.forClass(HttpGet.class); provider.register3Scale(); verify(httpClient).execute(argumentCaptor.capture()); - HttpPost httpPost = argumentCaptor.getValue(); - String entityString = IOUtils.toString(httpPost.getEntity().getContent()); - - - assertThat(entityString, containsString("auth_token")); - assertThat(entityString, containsString(TOKEN)); - assertThat(entityString, containsString("service_id")); - assertThat(entityString, containsString(ThreeScaleAPIProvider.SERVICE_ID)); + HttpGet httpGet = argumentCaptor.getValue(); + assertThat(httpGet.getURI().toString(), startsWith(String.format(ThreeScaleAPIProvider.THREE_SCALE_URL,ThreeScaleAPIProvider.SERVICE_ID))); + assertThat(httpGet.getHeaders("Authorization")[0].getValue(), is(TOKEN));// might be withoput value } @Test public void register3Scale_responseOK() throws ClientProtocolException, IOException, ThreeScaleAPIException { - when(httpClient.execute(any(HttpPost.class))).thenReturn(httpResponse); + when(httpClient.execute(any(HttpGet.class))).thenReturn(httpResponse); when(httpResponse.getStatusLine()).thenReturn(statusLine); when(httpResponse.getEntity()).thenReturn(httpEntity); + + when(httpEntity.getContent()).thenReturn(new ByteArrayInputStream(JSON.getBytes())); when(statusLine.getStatusCode()).thenReturn(HttpStatus.SC_OK); @@ -120,7 +117,7 @@ public void register3Scale_responseOK() throws ClientProtocolException, IOExcept @Test(expected=ThreeScaleAPIException.class) public void register3Scale_responseOther() throws ClientProtocolException, IOException, ThreeScaleAPIException { - when(httpClient.execute(any(HttpPost.class))).thenReturn(httpResponse); + when(httpClient.execute(any(HttpGet.class))).thenReturn(httpResponse); when(httpResponse.getStatusLine()).thenReturn(statusLine); when(statusLine.getStatusCode()).thenReturn(HttpStatus.SC_FORBIDDEN); @@ -131,7 +128,7 @@ public void register3Scale_responseOther() throws ClientProtocolException, IOExc @Test(expected=ThreeScaleAPIException.class) public void register3Scale_clientException() throws ClientProtocolException, IOException, ThreeScaleAPIException { - when(httpClient.execute(any(HttpPost.class))).thenThrow(IOException.class); + when(httpClient.execute(any(HttpGet.class))).thenThrow(IOException.class); when(httpResponse.getStatusLine()).thenReturn(statusLine); when(statusLine.getStatusCode()).thenReturn(HttpStatus.SC_FORBIDDEN);