Skip to content

Commit

Permalink
Updated the URl for scale (#170)
Browse files Browse the repository at this point in the history
Updated the URl for 3scale
  • Loading branch information
geetikabatra authored and rawagner committed Feb 22, 2018
1 parent f753fb8 commit 947d025
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 +"}";
Expand Down Expand Up @@ -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<HttpPost> argumentCaptor = ArgumentCaptor.forClass(HttpPost.class);
final ArgumentCaptor<HttpGet> 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);

Expand All @@ -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);

Expand All @@ -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);

Expand Down

0 comments on commit 947d025

Please sign in to comment.