Hi there! I am trying to test my implementation AccountServiceBean, so I created the test AccountServiceBeanTest in my module core.
I created private functions, but my test is failing java.lang.RuntimeException: Status Code: 401
at ca.theaidplans.muaipm.service.AccountServiceBeanTest.getAuthToken(AccountServiceBeanTest.java:215)
at ca.theaidplans.muaipm.service.AccountServiceBeanTest.setUp(AccountServiceBeanTest.java:55)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
String uri = URI_BASE + "/oauth/token";
CloseableHttpClient httpClient = HttpClients.createDefault();
String encoding = Base64.getEncoder().encodeToString((CLIENT_ID + ":" + Client_SECRET).getBytes());
HttpPost httpPost = new HttpPost(uri);
httpPost.setHeader("Authorization", "Basic" + encoding);
httpPost.setHeader("Accept-Language", "en");
List<NameValuePair> urlParameters = new ArrayList<>();
urlParameters.add(new BasicNameValuePair("grant_type", "password"));
urlParameters.add(new BasicNameValuePair("username", login));
urlParameters.add(new BasicNameValuePair("password", password));
httpPost.setEntity(new UrlEncodedFormEntity(urlParameters));
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
int statusCode = statusCode(response);
if (statusCode != SC_OK) {
throw new RuntimeException("Status Code: " + statusCode);
}
ReadContext ctx = parseResponse(response);
return ctx.read("$.access_token");
}
}
private CloseableHttpResponse sendGet(String url, String token) throws Exception {
URIBuilder uriBuilder = new URIBuilder(URI_BASE + url);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(uriBuilder.build());
httpGet.setHeader("Authorization", "Beaver " + token);
httpGet.setHeader("Accept-Language", "en");
return httpClient.execute(httpGet);
}
private CloseableHttpResponse sendPost(String url, String token, String body) throws Exception {
URIBuilder uriBuilder = new URIBuilder(URI_BASE + url);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(uriBuilder.build());
StringEntity stringEntity = new StringEntity(body);
httpPost.setEntity(stringEntity);
httpPost.setHeader("Authorization", "Beaver " + token);
return httpClient.execute(httpPost);
}
private CloseableHttpResponse sendDelete(String url, String token) throws Exception {
URIBuilder uriBuilder = new URIBuilder(URI_BASE + url);
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpDelete httpDelete = new HttpDelete(uriBuilder.build());
httpDelete.setHeader("Autorization", "Bearern " + token);
return httpClient.execute(httpDelete);
}
private ReadContext parseResponse(CloseableHttpResponse response) throws IOException {
HttpEntity entity = response.getEntity();
String s = EntityUtils.toString(entity);
return JsonPath.parse(s);
}
private int statusCode(CloseableHttpResponse response){
return response.getStatusLine().getStatusCode();
}```