1010 *******************************************************************************/
1111
1212package com .redhat .fabric8analytics .lsp .eclipse .core .tests ;
13-
14- import static org .hamcrest .CoreMatchers .containsString ;
1513import static org .hamcrest .CoreMatchers .is ;
14+ import static org .hamcrest .CoreMatchers .startsWith ;
1615import static org .junit .Assert .assertThat ;
1716import static org .mockito .Matchers .any ;
1817import static org .mockito .Mockito .atLeastOnce ;
2322import java .io .ByteArrayInputStream ;
2423import java .io .IOException ;
2524
26- import org .apache .commons .io .IOUtils ;
2725import org .apache .http .HttpEntity ;
2826import org .apache .http .HttpStatus ;
2927import org .apache .http .StatusLine ;
3028import org .apache .http .client .ClientProtocolException ;
3129import org .apache .http .client .methods .CloseableHttpResponse ;
32- import org .apache .http .client .methods .HttpPost ;
30+ import org .apache .http .client .methods .HttpGet ;
3331import org .apache .http .impl .client .CloseableHttpClient ;
3432import org .eclipse .core .runtime .CoreException ;
3533import org .junit .Before ;
@@ -50,6 +48,8 @@ public class ThreeScaleAPIProviderTest {
5048
5149 private static final String TOKEN = "mytoken" ;
5250
51+ private static final String URL = "www.myurl.com" ;
52+
5353 private static final String JSON_ENDPOINTS = "{\" prod\" :" + PROD + ", \" stage\" :" + STAGE +"}" ;
5454
5555 private static final String JSON = "{\" endpoints\" :" + JSON_ENDPOINTS + ", \" user_key\" : " + USER_KEY +"}" ;
@@ -80,32 +80,29 @@ public void checkNullToken() {
8080 }
8181
8282 @ Test
83- public void register3Scale_createPost () throws UnsupportedOperationException , IOException , ThreeScaleAPIException {
84- when (httpClient .execute (any (HttpPost .class ))).thenReturn (httpResponse );
83+ public void register3Scale_createGet () throws UnsupportedOperationException , IOException , ThreeScaleAPIException {
84+ when (httpClient .execute (any (HttpGet .class ))).thenReturn (httpResponse );
8585 when (httpResponse .getStatusLine ()).thenReturn (statusLine );
8686 when (httpResponse .getEntity ()).thenReturn (httpEntity );
8787 when (httpEntity .getContent ()).thenReturn (new ByteArrayInputStream (JSON .getBytes ()));
8888 when (statusLine .getStatusCode ()).thenReturn (HttpStatus .SC_OK );
8989
90- final ArgumentCaptor <HttpPost > argumentCaptor = ArgumentCaptor .forClass (HttpPost .class );
90+ final ArgumentCaptor <HttpGet > argumentCaptor = ArgumentCaptor .forClass (HttpGet .class );
9191
9292 provider .register3Scale ();
9393 verify (httpClient ).execute (argumentCaptor .capture ());
94- HttpPost httpPost = argumentCaptor .getValue ();
95- String entityString = IOUtils .toString (httpPost .getEntity ().getContent ());
96-
97-
98- assertThat (entityString , containsString ("auth_token" ));
99- assertThat (entityString , containsString (TOKEN ));
100- assertThat (entityString , containsString ("service_id" ));
101- assertThat (entityString , containsString (ThreeScaleAPIProvider .SERVICE_ID ));
94+ HttpGet httpGet = argumentCaptor .getValue ();
95+ assertThat (httpGet .getURI ().toString (), startsWith (String .format (ThreeScaleAPIProvider .THREE_SCALE_URL ,ThreeScaleAPIProvider .SERVICE_ID )));
96+ assertThat (httpGet .getHeaders ("Authorization" )[0 ].getValue (), is (TOKEN ));// might be withoput value
10297 }
10398
10499 @ Test
105100 public void register3Scale_responseOK () throws ClientProtocolException , IOException , ThreeScaleAPIException {
106- when (httpClient .execute (any (HttpPost .class ))).thenReturn (httpResponse );
101+ when (httpClient .execute (any (HttpGet .class ))).thenReturn (httpResponse );
107102 when (httpResponse .getStatusLine ()).thenReturn (statusLine );
108103 when (httpResponse .getEntity ()).thenReturn (httpEntity );
104+
105+
109106 when (httpEntity .getContent ()).thenReturn (new ByteArrayInputStream (JSON .getBytes ()));
110107 when (statusLine .getStatusCode ()).thenReturn (HttpStatus .SC_OK );
111108
@@ -120,7 +117,7 @@ public void register3Scale_responseOK() throws ClientProtocolException, IOExcept
120117
121118 @ Test (expected =ThreeScaleAPIException .class )
122119 public void register3Scale_responseOther () throws ClientProtocolException , IOException , ThreeScaleAPIException {
123- when (httpClient .execute (any (HttpPost .class ))).thenReturn (httpResponse );
120+ when (httpClient .execute (any (HttpGet .class ))).thenReturn (httpResponse );
124121 when (httpResponse .getStatusLine ()).thenReturn (statusLine );
125122 when (statusLine .getStatusCode ()).thenReturn (HttpStatus .SC_FORBIDDEN );
126123
@@ -131,7 +128,7 @@ public void register3Scale_responseOther() throws ClientProtocolException, IOExc
131128
132129 @ Test (expected =ThreeScaleAPIException .class )
133130 public void register3Scale_clientException () throws ClientProtocolException , IOException , ThreeScaleAPIException {
134- when (httpClient .execute (any (HttpPost .class ))).thenThrow (IOException .class );
131+ when (httpClient .execute (any (HttpGet .class ))).thenThrow (IOException .class );
135132 when (httpResponse .getStatusLine ()).thenReturn (statusLine );
136133 when (statusLine .getStatusCode ()).thenReturn (HttpStatus .SC_FORBIDDEN );
137134
0 commit comments