private String obtainAccessToken(String username, String password) throws Exception { final MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); params.add("grant_type", "password"); params.add("client_id", CLIENT_ID); params.add("username", username); params.add("password", password); // @formatter:off ResultActions result = mockMvc.perform(post("/oauth/token") .params(params) .with(httpBasic(CLIENT_ID, CLIENT_SECRET)) .accept(CONTENT_TYPE)) .andExpect(status().isOk()) .andExpect(content().contentType(CONTENT_TYPE)); // @formatter:on String resultString = result.andReturn().getResponse().getContentAsString(); JacksonJsonParser jsonParser = new JacksonJsonParser(); return jsonParser.parseMap(resultString).get("access_token").toString(); }
/** * Calls the remote service using the provided request, applies error handling and * converts the response into a {@link Map}. The entire request and response are * executed and handled in a hot observable. * * @param serviceId the id of the service for which the request is made * @param request the request which has to be executed using RxNetty * @return an {@link Observable} emitting the JSON response as a Map with String keys * and Object values. */ public Observable<Map<String, Object>> retrieveJsonFromRequest(String serviceId, HttpClientRequest<ByteBuf> request) { RxClient.ServerInfo serverInfo = getServerInfoFromRequestOrClient(request, rxClient); return rxClient.submit(serverInfo, request) .publish().autoConnect() .doOnError(el -> errorHandler.handleNodeError(serviceId, format("Error retrieving node(s) for url {0} with headers {1}: {2}", request.getUri(), request.getHeaders().entries(), el), el)) .filter(r -> { if (r.getStatus().code() < 400) { return true; } else { errorHandler.handleNodeWarning(serviceId, "Exception " + r.getStatus() + " for url " + request.getUri() + " with headers " + r.getHeaders().entries()); return false; } }) .flatMap(AbstractHttpContentHolder::getContent) .map(data -> data.toString(Charset.defaultCharset())) .map(response -> { JacksonJsonParser jsonParser = new JacksonJsonParser(); return jsonParser.parseMap(response); }) .doOnNext(r -> logger.info("Json retrieved from call: {}", r)) .onErrorResumeNext(Observable.empty()); }
public List<Object> getAndCheckProjectReleases(String projectId, String expectedProjectName) throws Exception { MvcResult result = mockMvc.perform( MockMvcRequestBuilders.get("/project_metadata/" + projectId + "?callback=a_function_name")).andReturn(); String content = result.getResponse().getContentAsString(); String functionNameRegex = "^([^(]*)\\((.*)\\);$"; Matcher matcher = Pattern.compile(functionNameRegex).matcher(content); if (matcher.find()) { assertThat(matcher.group(1), equalTo("/**/a_function_name")); Map<String, Object> projectMetadata = new JacksonJsonParser().parseMap(matcher.group(2)); assertThat((String) projectMetadata.get("name"), equalTo(expectedProjectName)); @SuppressWarnings("unchecked") List<Object> list = (List<Object>) projectMetadata.get("projectReleases"); return list; } else { fail(String.format("no match found: %s", content)); return null; } }