Skip to content

Commit

Permalink
JBIDE-28815: Fix after TP update (#195)
Browse files Browse the repository at this point in the history
* JBIDE-28815: Fix after TP update

Signed-off-by: Stephane Bouchet <[email protected]>

* JBIDE-28815: Fix after TP update

Signed-off-by: Stephane Bouchet <[email protected]>

* JBIDE-28815: Fix after TP update

Signed-off-by: Stephane Bouchet <[email protected]>

Signed-off-by: Stephane Bouchet <[email protected]>
  • Loading branch information
sbouchet authored Dec 12, 2022
1 parent a330bba commit 2034acb
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ Export-Package: com.redhat.fabric8analytics.lsp.eclipse.core,
com.redhat.fabric8analytics.lsp.eclipse.core.job,
com.redhat.fabric8analytics.lsp.eclipse.core.data,
com.redhat.fabric8analytics.lsp.eclipse.core.internal;x-friends:=com.redhat.fabric8analytics.lsp.eclipse.ui
Import-Package: javax.annotation


2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.jboss.tools</groupId>
<artifactId>parent</artifactId>
<version>4.26.0.AM1-SNAPSHOT</version>
<version>4.26.0.Final-SNAPSHOT</version>
</parent>

<groupId>com.redhat.fabric8analytics.eclipse</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Require-Bundle: org.eclipse.reddeer.go;bundle-version="2.0.0",
org.junit;bundle-version="4.8.2",
com.redhat.fabric8analytics.lsp.eclipse.core;bundle-version="0.0.1",
org.mockito;bundle-version="1.9.5",
org.hamcrest.core;bundle-version="1.3.0",
org.hamcrest.library;bundle-version="1.3.0",
org.apache.httpcomponents.httpclient;bundle-version="4.5.2",
org.apache.httpcomponents.httpcore;bundle-version="4.4.6",
org.eclipse.core.resources;bundle-version="3.12.0",
org.apache.commons.io;bundle-version="2.2.0",
org.json
org.json,
org.hamcrest;bundle-version="2.2.0",
org.mockito.mockito-core;bundle-version="4.8.0"
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
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.equalTo;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -63,7 +63,7 @@ public class RecommenderAPIProviderTest {
private static final String USER_KEY = "myuserkey";

private static final String URL = "www.myurl.com";

private static final String JOB = "myjob";

private static final String JSON = "{\"id\":12345}";
Expand All @@ -77,7 +77,7 @@ public class RecommenderAPIProviderTest {
private HttpEntity httpEntity;

private StatusLine statusLine;

private Map<String, String> files;

@Before
Expand All @@ -86,19 +86,19 @@ public void setup() throws CoreException, IOException {
httpResponse = mock(CloseableHttpResponse.class);
httpEntity = mock(HttpEntity.class);
statusLine = mock(StatusLine.class);

ThreeScaleData scaleData = new ThreeScaleData(URL, URL, USER_KEY);
AnalyticsAuthData data = new AnalyticsAuthData(scaleData);

provider = new RecommenderAPIProviderWithCustomClient(data);

IProject project = createProject("myproject");
files = new HashMap<>();
File fileA = createFile(project, "firstfile", "aaa content");
File fileB = createFile(project, "secondfile", "bbb content");
files.put(fileA.getAbsolutePath(), new String(Files.readAllBytes(fileA.toPath())));
files.put(fileB.getAbsolutePath(), new String(Files.readAllBytes(fileB.toPath())));
}
}

@After
public void after() throws CoreException {
Expand All @@ -114,30 +114,32 @@ public void after() throws CoreException {
root.delete(true, null);
}

@Test(expected=IllegalArgumentException.class)
@Test(expected = IllegalArgumentException.class)
public void checkNullURL() {
ThreeScaleData scaleData = new ThreeScaleData(null, URL, USER_KEY);
AnalyticsAuthData data = new AnalyticsAuthData(scaleData);
new RecommenderAPIProvider(data);
}

@Test(expected=IllegalArgumentException.class)
@Test(expected = IllegalArgumentException.class)
public void checkNullUserKey() {
ThreeScaleData scaleData = new ThreeScaleData(URL, URL, null);
AnalyticsAuthData data = new AnalyticsAuthData(scaleData);
new RecommenderAPIProvider(data);
}

@Test(expected=IllegalArgumentException.class)
public void requestAnalyses_nullFiles() throws ClientProtocolException, IOException, RecommenderAPIException, CoreException {
@Test(expected = IllegalArgumentException.class)
public void requestAnalyses_nullFiles()
throws ClientProtocolException, IOException, RecommenderAPIException, CoreException {
provider.requestAnalyses(null, null);
}

@Test(expected=IllegalArgumentException.class)
public void requestAnalyses_emptyFiles() throws ClientProtocolException, IOException, RecommenderAPIException, CoreException {

@Test(expected = IllegalArgumentException.class)
public void requestAnalyses_emptyFiles()
throws ClientProtocolException, IOException, RecommenderAPIException, CoreException {
provider.requestAnalyses(Collections.emptyMap(), null);
}

@Test
public void requestAnalyses_createPost() throws ClientProtocolException, IOException, RecommenderAPIException {
when(httpClient.execute(any(HttpPost.class))).thenReturn(httpResponse);
Expand All @@ -149,7 +151,7 @@ public void requestAnalyses_createPost() throws ClientProtocolException, IOExcep
final ArgumentCaptor<HttpPost> argumentCaptor = ArgumentCaptor.forClass(HttpPost.class);

provider.requestAnalyses(files, null);

verify(httpClient).execute(argumentCaptor.capture());
HttpPost httpPost = argumentCaptor.getValue();
String filesString = IOUtils.toString(httpPost.getEntity().getContent());
Expand All @@ -173,10 +175,10 @@ public void requestAnalyses_responseOK() throws ClientProtocolException, IOExcep
String jobID = provider.requestAnalyses(files, null);
verify(httpClient, atLeastOnce()).close();

assertThat(jobID, is("12345"));
assertThat(jobID, equalTo("12345"));
}

@Test(expected=RecommenderAPIException.class)
@Test(expected = RecommenderAPIException.class)
public void requestAnalyses_responseOther() throws ClientProtocolException, IOException, RecommenderAPIException {
when(httpClient.execute(any(HttpPost.class))).thenReturn(httpResponse);
when(httpResponse.getStatusLine()).thenReturn(statusLine);
Expand All @@ -186,24 +188,24 @@ public void requestAnalyses_responseOther() throws ClientProtocolException, IOEx
verify(httpClient, atLeastOnce()).close();
}

@Test(expected=RecommenderAPIException.class)
@Test(expected = RecommenderAPIException.class)
public void requestAnalyses_clientException() throws ClientProtocolException, IOException, RecommenderAPIException {
when(httpClient.execute(any(HttpPost.class))).thenThrow(IOException.class);

provider.requestAnalyses(files, null);
verify(httpClient, atLeastOnce()).close();
}

@Test(expected=IllegalArgumentException.class)
@Test(expected = IllegalArgumentException.class)
public void analysesFinished_nullJob() throws ClientProtocolException, IOException, RecommenderAPIException {
provider.analysesFinished(null);
}
@Test(expected=IllegalArgumentException.class)

@Test(expected = IllegalArgumentException.class)
public void analysesFinished_emptyStringJob() throws ClientProtocolException, IOException, RecommenderAPIException {
provider.analysesFinished("");
}

@Test
public void analysesFinished_createGet() throws ClientProtocolException, IOException, RecommenderAPIException {
when(httpClient.execute(any(HttpGet.class))).thenReturn(httpResponse);
Expand All @@ -227,38 +229,40 @@ public void analysesFinished_responseOK() throws ClientProtocolException, IOExce
when(httpResponse.getStatusLine()).thenReturn(statusLine);
when(statusLine.getStatusCode()).thenReturn(HttpStatus.SC_OK);

assertThat(provider.analysesFinished(JOB), is(true));
assertThat(provider.analysesFinished(JOB), equalTo(true));
verify(httpClient, atLeastOnce()).close();
}

@Test
public void analysesFinished_responseAccepted() throws ClientProtocolException, IOException, RecommenderAPIException {
public void analysesFinished_responseAccepted()
throws ClientProtocolException, IOException, RecommenderAPIException {
when(httpClient.execute(any(HttpGet.class))).thenReturn(httpResponse);
when(httpResponse.getStatusLine()).thenReturn(statusLine);
when(statusLine.getStatusCode()).thenReturn(HttpStatus.SC_ACCEPTED);

assertThat(provider.analysesFinished(JOB), is(false));
assertThat(provider.analysesFinished(JOB), equalTo(false));
verify(httpClient, atLeastOnce()).close();
}

@Test(expected=RecommenderAPIException.class)
@Test(expected = RecommenderAPIException.class)
public void analysesFinished_responseOther() throws ClientProtocolException, IOException, RecommenderAPIException {
when(httpClient.execute(any(HttpGet.class))).thenReturn(httpResponse);
when(httpResponse.getStatusLine()).thenReturn(statusLine);
when(statusLine.getStatusCode()).thenReturn(HttpStatus.SC_FORBIDDEN);

provider.analysesFinished(JOB);

verify(httpClient, atLeastOnce()).close();
}

@SuppressWarnings("unchecked")
@Test(expected=RecommenderAPIException.class)
public void analysesFinished_clientException() throws ClientProtocolException, IOException, RecommenderAPIException {
@Test(expected = RecommenderAPIException.class)
public void analysesFinished_clientException()
throws ClientProtocolException, IOException, RecommenderAPIException {
when(httpClient.execute(any(HttpGet.class))).thenThrow(IOException.class);

provider.analysesFinished(JOB);

verify(httpClient, atLeastOnce()).close();
}

Expand All @@ -267,14 +271,14 @@ private class RecommenderAPIProviderWithCustomClient extends RecommenderAPIProvi
public RecommenderAPIProviderWithCustomClient(AnalyticsAuthData data) {
super(data);
}

@Override
protected CloseableHttpClient createClient() {
return httpClient;
}
}
private File createFile(IProject project, String name, String content) throws CoreException{

private File createFile(IProject project, String name, String content) throws CoreException {
IFile file = project.getFile(name);
file.create(new ByteArrayInputStream(content.getBytes()), IResource.NONE, null);
return file.getRawLocation().makeAbsolute().toFile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
*******************************************************************************/

package com.redhat.fabric8analytics.lsp.eclipse.core.tests;
import static org.hamcrest.CoreMatchers.is;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.startsWith;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
Expand All @@ -39,20 +40,20 @@
import com.redhat.fabric8analytics.lsp.eclipse.core.data.ThreeScaleData;

public class ThreeScaleAPIProviderTest {

private static final String PROD = "produrl";

private static final String STAGE = "stageurl";

private static final String USER_KEY = "myuserkey";

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 +"}";

private static final String JSON_ENDPOINTS = "{\"prod\":" + PROD + ", \"stage\":" + STAGE + "}";

private static final String JSON = "{\"endpoints\":" + JSON_ENDPOINTS + ", \"user_key\": " + USER_KEY + "}";

private ThreeScaleAPIProvider provider;

Expand All @@ -63,7 +64,7 @@ public class ThreeScaleAPIProviderTest {
private HttpEntity httpEntity;

private StatusLine statusLine;

@Before
public void setup() throws CoreException {
httpClient = mock(CloseableHttpClient.class);
Expand All @@ -73,7 +74,7 @@ public void setup() throws CoreException {

provider = new ThreeScaleAPIProviderWithCustomClient();
}

@Test
public void register3Scale_createGet() throws UnsupportedOperationException, IOException, ThreeScaleAPIException {
when(httpClient.execute(any(HttpGet.class))).thenReturn(httpResponse);
Expand All @@ -87,56 +88,56 @@ public void register3Scale_createGet() throws UnsupportedOperationException, IOE
provider.register3Scale();
verify(httpClient).execute(argumentCaptor.capture());
HttpGet httpGet = argumentCaptor.getValue();
assertThat(httpGet.getURI().toString(), startsWith(String.format(ThreeScaleAPIProvider.THREE_SCALE_URL,ThreeScaleAPIProvider.SERVICE_ID)));
assertThat(httpGet.getURI().toString(),
startsWith(String.format(ThreeScaleAPIProvider.THREE_SCALE_URL, ThreeScaleAPIProvider.SERVICE_ID)));
}

@Test
public void register3Scale_responseOK() throws ClientProtocolException, 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);

ThreeScaleData data = provider.register3Scale();

verify(httpClient, atLeastOnce()).close();
assertThat(data.getProd(), is(PROD));
assertThat(data.getStage(), is(STAGE));
assertThat(data.getUserKey(), is(USER_KEY));
assertThat(data.getProd(), equalTo(PROD));
assertThat(data.getStage(), equalTo(STAGE));
assertThat(data.getUserKey(), equalTo(USER_KEY));

}

@Test(expected=ThreeScaleAPIException.class)
@Test(expected = ThreeScaleAPIException.class)
public void register3Scale_responseOther() throws ClientProtocolException, IOException, ThreeScaleAPIException {
when(httpClient.execute(any(HttpGet.class))).thenReturn(httpResponse);
when(httpResponse.getStatusLine()).thenReturn(statusLine);
when(statusLine.getStatusCode()).thenReturn(HttpStatus.SC_FORBIDDEN);

provider.register3Scale();

verify(httpClient, atLeastOnce()).close();
}

@Test(expected=ThreeScaleAPIException.class)
@Test(expected = ThreeScaleAPIException.class)
public void register3Scale_clientException() throws ClientProtocolException, IOException, ThreeScaleAPIException {
when(httpClient.execute(any(HttpGet.class))).thenThrow(IOException.class);
when(httpResponse.getStatusLine()).thenReturn(statusLine);
when(statusLine.getStatusCode()).thenReturn(HttpStatus.SC_FORBIDDEN);

provider.register3Scale();

verify(httpClient, atLeastOnce()).close();
}

private class ThreeScaleAPIProviderWithCustomClient extends ThreeScaleAPIProvider {

public ThreeScaleAPIProviderWithCustomClient() {
super();
}

@Override
protected CloseableHttpClient createClient() {
return httpClient;
Expand Down
Loading

0 comments on commit 2034acb

Please sign in to comment.