Java Code Examples for play.libs.Json#parse()

The following examples show how to use play.libs.Json#parse() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonFileDAO.java    From samantha with MIT License 6 votes vote down vote up
public ObjectNode getNextEntity() {
    if (line == null) {
        try {
            line = reader.readLine();
        } catch (IOException e) {
            logger.error("{}", e.getMessage());
            throw new BadRequestException(e);
        }
    }
    ObjectNode entity = Json.newObject();
    JsonNode data = Json.parse(line);
    line = null;
    IOUtilities.parseEntityFromJsonNode(data, entity);
    cnt++;
    if (cnt % 1000000 == 0) {
        logger.info("Read {} entities.", cnt);
    }
    return entity;
}
 
Example 2
Source File: LocationControllerTest.java    From sunbird-lms-service with MIT License 6 votes vote down vote up
@Test
public void testSearchLocation() {

  Map<String, Object> requestMap = new HashMap<>();
  Map<String, Object> requestBody = new HashMap<>();
  Map<String, Object> filters = new HashMap<>();
  filters.put(JsonKey.NAME, LOCATION_NAME);
  filters.put(JsonKey.CODE, LOCATION_CODE);
  filters.put(GeoLocationJsonKey.LOCATION_TYPE, LOCATION_TYPE);
  requestBody.put(JsonKey.FILTERS, filters);
  requestMap.put(JsonKey.REQUEST, requestBody);
  String data = TestUtil.mapToJson(requestMap);
  JsonNode json = Json.parse(data);
  RequestBuilder req =
      new RequestBuilder().bodyJson(json).uri(SEARCH_LOCATION_URL).method("POST");
  //req.headers(headerMap);
  Result result = Helpers.route(application,req);
  assertEquals(200, result.status());
}
 
Example 3
Source File: ClientControllerTest.java    From sunbird-lms-service with MIT License 6 votes vote down vote up
@Test
public void testregisterClient() {
  PowerMockito.mockStatic(RequestInterceptor.class);
  when(RequestInterceptor.verifyRequestData(Mockito.anyObject()))
      .thenReturn("{userId} uuiuhcf784508 8y8c79-fhh");
  Map<String, Object> requestMap = new HashMap<>();
  Map<String, Object> innerMap = new HashMap<>();
  innerMap.put("clientName", "ap");
  requestMap.put(JsonKey.REQUEST, innerMap);
  String data = mapToJson(requestMap);

  JsonNode json = Json.parse(data);
  RequestBuilder req =
      new RequestBuilder().bodyJson(json).uri("/v1/client/register").method("POST");
  Result result = Helpers.route(application, req);
  assertEquals(200, result.status());
}
 
Example 4
Source File: DbOperationControllerTest.java    From sunbird-lms-service with MIT License 6 votes vote down vote up
@Test
public void testread() {
  PowerMockito.mockStatic(RequestInterceptor.class);
  when(RequestInterceptor.verifyRequestData(Mockito.anyObject()))
      .thenReturn("{userId} uuiuhcf784508 8y8c79-fhh");
  Map<String, Object> requestMap = new HashMap<>();
  Map<String, Object> innerMap = new HashMap<>();
  innerMap.put(ENTITY_NAME, entityName);
  // innerMap.put(INDEXED , true);
  // innerMap.put(JsonKey.USER_ID , "usergt78y4ry8");
  innerMap.put(JsonKey.ID, "ggudy8d8ydyy8ddy9");
  /*Map<String , Object> payLoad = new HashMap<>();
  payLoad.put(JsonKey.USER_ID , "usergt78y4ry8");
  payLoad.put(JsonKey.ID , "ggudy8d8ydyy8ddy9");
  innerMap.put(PAYLOAD , payLoad);*/
  requestMap.put(JsonKey.REQUEST, innerMap);
  String data = mapToJson(requestMap);

  JsonNode json = Json.parse(data);
  RequestBuilder req = new RequestBuilder().bodyJson(json).uri("/v1/object/read").method("POST");
  /*//req.headers(headerMap);*/
  Result result = Helpers.route(application,req);
  assertEquals(200, result.status());
}
 
Example 5
Source File: DbOperationControllerTest.java    From sunbird-lms-service with MIT License 6 votes vote down vote up
@Test
public void testCreateWithWrongEntityName() {
  PowerMockito.mockStatic(RequestInterceptor.class);
  when(RequestInterceptor.verifyRequestData(Mockito.anyObject()))
      .thenReturn("{userId} uuiuhcf784508 8y8c79-fhh");
  Map<String, Object> requestMap = new HashMap<>();
  Map<String, Object> innerMap = new HashMap<>();
  innerMap.put(ENTITY_NAME, entityName + "-wrong");
  innerMap.put(INDEXED, true);
  Map<String, Object> payLoad = new HashMap<>();
  payLoad.put(JsonKey.USER_ID, "usergt78y4ry8");
  payLoad.put(JsonKey.ID, "ggudy8d8ydyy8ddy9");
  innerMap.put(PAYLOAD, payLoad);
  requestMap.put(JsonKey.REQUEST, innerMap);
  String data = mapToJson(requestMap);

  JsonNode json = Json.parse(data);
  RequestBuilder req =
      new RequestBuilder().bodyJson(json).uri("/v1/object/create").method("POST");
  /*//req.headers(headerMap);*/
  Result result = Helpers.route(application,req);
  assertEquals(200, result.status());
}
 
Example 6
Source File: BadgeAssertionControllerTest.java    From sunbird-lms-service with MIT License 6 votes vote down vote up
@Test
public void revokeAssertionFailureTest() {
  PowerMockito.mockStatic(RequestInterceptor.class);
  when(RequestInterceptor.verifyRequestData(Mockito.anyObject()))
      .thenReturn("{userId} uuiuhcf784508 8y8c79-fhh");
  Map<String, Object> requestMap = new HashMap<>();
  Map<String, Object> innerMap = new HashMap<>();
  innerMap.put(BadgingJsonKey.ASSERTION_ID, "assertionId");
  innerMap.put(BadgingJsonKey.RECIPIENT_ID, "userIdorcontentId");
  innerMap.put(BadgingJsonKey.RECIPIENT_TYPE, "user");
  requestMap.put(JsonKey.REQUEST, innerMap);
  String data = mapToJson(requestMap);
  JsonNode json = Json.parse(data);
  RequestBuilder req =
      new RequestBuilder()
          .bodyJson(json)
          .uri("/v1/issuer/badge/assertion/delete")
          .method("DELETE");
  Result result = Helpers.route(application, req);
  assertEquals(400, result.status());
}
 
Example 7
Source File: DbOperationControllerTest.java    From sunbird-lms-service with MIT License 6 votes vote down vote up
@Test
public void testupdate() {
  PowerMockito.mockStatic(RequestInterceptor.class);
  when(RequestInterceptor.verifyRequestData(Mockito.anyObject()))
      .thenReturn("{userId} uuiuhcf784508 8y8c79-fhh");
  Map<String, Object> requestMap = new HashMap<>();
  Map<String, Object> innerMap = new HashMap<>();
  innerMap.put(ENTITY_NAME, entityName);
  innerMap.put(INDEXED, true);
  Map<String, Object> payLoad = new HashMap<>();
  payLoad.put(JsonKey.USER_ID, "usergt78y4ry85464");
  payLoad.put(JsonKey.ID, "ggudy8d8ydyy8ddy9");
  innerMap.put(PAYLOAD, payLoad);
  requestMap.put(JsonKey.REQUEST, innerMap);
  String data = mapToJson(requestMap);

  JsonNode json = Json.parse(data);
  RequestBuilder req =
      new RequestBuilder().bodyJson(json).uri("/v1/object/update").method("POST");
  /*//req.headers(headerMap);*/
  Result result = Helpers.route(application,req);
  assertEquals(200, result.status());
}
 
Example 8
Source File: BadgeAssertionControllerTest.java    From sunbird-lms-service with MIT License 6 votes vote down vote up
@Test
public void createAssertionWithInvalidData() {
  PowerMockito.mockStatic(RequestInterceptor.class);
  when(RequestInterceptor.verifyRequestData(Mockito.anyObject()))
      .thenReturn("{userId} uuiuhcf784508 8y8c79-fhh");
  Map<String, Object> requestMap = new HashMap<>();
  Map<String, Object> innerMap = new HashMap<>();
  innerMap.put(BadgingJsonKey.BADGE_CLASS_ID, "badgeid");
  innerMap.put(BadgingJsonKey.RECIPIENT_EMAIL, "[email protected]");
  innerMap.put(BadgingJsonKey.RECIPIENT_ID, "userIdorcontentId");
  innerMap.put(BadgingJsonKey.RECIPIENT_TYPE, "user");
  requestMap.put(JsonKey.REQUEST, innerMap);
  String data = mapToJson(requestMap);

  JsonNode json = Json.parse(data);
  RequestBuilder req =
      new RequestBuilder().bodyJson(json).uri("/v1/issuer/badge/assertion/create").method("POST");
  Result result = Helpers.route(application, req);
  assertEquals(400, result.status());
}
 
Example 9
Source File: SearchControllerTest.java    From sunbird-lms-service with MIT License 6 votes vote down vote up
@Test
public void testcompositeSearch() {
  PowerMockito.mockStatic(RequestInterceptor.class);
  when(RequestInterceptor.verifyRequestData(Mockito.anyObject()))
      .thenReturn("{userId} uuiuhcf784508 8y8c79-fhh");
  Map<String, Object> requestMap = new HashMap<>();
  Map<String, Object> innerMap = new HashMap<>();
  innerMap.put(JsonKey.ORG_NAME, "org123");
  innerMap.put(JsonKey.OBJECT_TYPE, JsonKey.ORGANISATION);
  requestMap.put(JsonKey.REQUEST, innerMap);
  String data = mapToJson(requestMap);

  JsonNode json = Json.parse(data);
  RequestBuilder req =
      new RequestBuilder().bodyJson(json).uri("/v1/search/compositesearch").method("POST");
  //req.headers(headerMap);
  Result result = Helpers.route(application,req);
  assertEquals(200, result.status());
}
 
Example 10
Source File: UserSkillControllerTest.java    From sunbird-lms-service with MIT License 6 votes vote down vote up
@Test
public void testGetSkill() {

  Map<String, Object> requestMap = new HashMap<>();
  Map<String, Object> innerMap = new HashMap<>();
  innerMap.put(JsonKey.USER_ID, "7687584");
  requestMap.put(JsonKey.REQUEST, innerMap);
  String data = mapToJson(requestMap);

  JsonNode json = Json.parse(data);
  Http.RequestBuilder req =
      new Http.RequestBuilder().bodyJson(json).uri("/v1/user/skill/read").method("POST");
  //req.headers(headerMap);
  Result result = Helpers.route(application,req);
  assertEquals(200, result.status());
}
 
Example 11
Source File: TnCControllerTest.java    From sunbird-lms-service with MIT License 5 votes vote down vote up
public Result performTest(String url, String method, Map map) {
  String data = mapToJson(map);
  Http.RequestBuilder req;
  if (StringUtils.isNotBlank(data)) {
    JsonNode json = Json.parse(data);
    req = new Http.RequestBuilder().bodyJson(json).uri(url).method(method);
  } else {
    req = new Http.RequestBuilder().uri(url).method(method);
  }
  //req.headers(new Http.Headers(headerMap));
  Result result = Helpers.route(application, req);
  return result;
}
 
Example 12
Source File: PostgresNodeVersionDao.java    From ground with Apache License 2.0 5 votes vote down vote up
@Override
public List<Long> retrieveAdjacentLineageEdgeVersion(long startId) throws GroundException {
  String sql = String.format(SqlConstants.SELECT_NODE_VERSION_ADJACENT_LINEAGE, startId);
  JsonNode json = Json.parse(PostgresUtils.executeQueryToJson(this.dbSource, sql));

  List<Long> result = new ArrayList<>();
  json.forEach(x -> result.add(x.get("id").asLong()));

  return result;
}
 
Example 13
Source File: ESQueryBasedRetriever.java    From samantha with MIT License 5 votes vote down vote up
public RetrievedResult retrieve(RequestContext requestContext) {
    final JsonNode requestBody = requestContext.getRequestBody();
    boolean setScroll = JsonHelpers.getOptionalBoolean(requestBody, setScrollKey, false);
    if (setScroll && scrollComplete) {
        return new RetrievedResult(new ArrayList<>(), 0);
    }
    JsonNode elasticSearchRequest;
    if (requestBody.has(elasticSearchReqKey)) {
        elasticSearchRequest = JsonHelpers.getRequiredJson(requestBody, elasticSearchReqKey);
    } else if (matchFields.length > 0 && requestBody.has(queryKey)) {
        String query = JsonHelpers.getRequiredString(requestBody, queryKey);
        elasticSearchRequest = Json.parse(QueryBuilders.multiMatchQuery(query, matchFields).toString());
    } else if (defaultMatchAll || setScroll) {
        elasticSearchRequest = Json.parse(QueryBuilders.matchAllQuery().toString());
    } else {
        return new RetrievedResult(new ArrayList<>(), 0);
    }
    Integer size = JsonHelpers.getOptionalInt(elasticSearchRequest, "size", null);
    Integer from = JsonHelpers.getOptionalInt(elasticSearchRequest, "from", null);
    SearchResponse searchResponse = elasticSearchService
            .search(elasticSearchIndex,
                    retrieveType,
                    elasticSearchRequest,
                    retrieveFields,
                    setScroll,
                    scrollId, size, from);
    RetrievedResult initialResult = ESRetrieverUtilities.parse(elasticSearchScoreName, requestContext,
            searchResponse, expanders, retrieveFields);
    if (setScroll) {
        scrollId = searchResponse.getScrollId();
        if (initialResult.getEntityList().size() == 0) {
            elasticSearchService.resetScroll(scrollId);
            scrollComplete = true;
        }
    }
    return initialResult;
}
 
Example 14
Source File: UserProfileControllerTest.java    From sunbird-lms-service with MIT License 5 votes vote down vote up
@Test
public void testProfileVisibilityFailure() {

  String data = getRequesteddata(userId);
  JsonNode json = Json.parse(data);
  RequestBuilder req =
      new RequestBuilder().bodyJson(json).uri("/v1/user/profile/visibility").method("POST");
  //req.headers(headerMap);
  Result result = Helpers.route(application,req);
  String response = Helpers.contentAsString(result);
  assertEquals(400, result.status());
}
 
Example 15
Source File: NotesControllerTest.java    From sunbird-lms-service with MIT License 5 votes vote down vote up
@Test
public void testUpdateNoteSuccess() {
  Map<String, Object> requestMap = new HashMap<>();
  requestMap.put(
      JsonKey.REQUEST, getCreateNoteRequest(USER_ID, COURSE_ID, CONTENT_ID, NOTE, TITLE));
  String data = mapToJson(requestMap);

  JsonNode json = Json.parse(data);
  RequestBuilder req =
      new RequestBuilder().bodyJson(json).uri("/v1/note/update/" + NOTE_ID).method("PATCH");
  //req.headers(headerMap);
  Result result = Helpers.route(application,req);
  assertEquals(200, result.status());
}
 
Example 16
Source File: UserDataEncryptionControllerTest.java    From sunbird-lms-service with MIT License 5 votes vote down vote up
public Result performTest(String url, String method, Map map) {
  String data = mapToJson(map);
  Http.RequestBuilder req;
  if (StringUtils.isNotBlank(data)) {
    JsonNode json = Json.parse(data);
    req = new Http.RequestBuilder().bodyJson(json).uri(url).method(method);
  } else {
    req = new Http.RequestBuilder().uri(url).method(method);
  }
  //req.headers(new Http.Headers(headerMap));
  Result result = Helpers.route(application, req);
  return result;
}
 
Example 17
Source File: SystemSettingsControllerTest.java    From sunbird-lms-service with MIT License 5 votes vote down vote up
private JsonNode createSetSystemSettingRequest(String id, String field, String value) {
  Map<String, Object> requestMap = new HashMap<>();
  Map<String, Object> innerMap = new HashMap<>();
  if (id != null) innerMap.put(JsonKey.ID, id);
  if (field != null) innerMap.put(JsonKey.FIELD, field);
  if (value != null) innerMap.put(JsonKey.VALUE, value);
  requestMap.put(JsonKey.REQUEST, innerMap);
  String data = mapToJson(requestMap);

  return Json.parse(data);
}
 
Example 18
Source File: NotesControllerTest.java    From sunbird-lms-service with MIT License 5 votes vote down vote up
@Test
@Ignore
public void testCreateNoteSuccess() {
  Map<String, Object> requestMap = new HashMap<>();
  requestMap.put(
      JsonKey.REQUEST, getCreateNoteRequest(USER_ID, COURSE_ID, CONTENT_ID, NOTE, TITLE));
  String data = mapToJson(requestMap);
  JsonNode json = Json.parse(data);
  RequestBuilder req = new RequestBuilder().bodyJson(json).uri("/v1/note/create").method("POST");
  //req.headers(headerMap);
  Result result = Helpers.route(application,req);
  assertEquals(200, result.status());
}
 
Example 19
Source File: Storage.java    From remote-monitoring-services-java with MIT License 5 votes vote down vote up
@Override
public CompletionStage<Object> getThemeAsync() {
    String data = toJson(Theme.Default);
    try {
        String serverData = client.getAsync(SolutionCollectionId, ThemeKey).toCompletableFuture().get().getData();
        if (serverData != null && StringUtils.isNotBlank(serverData)) {
            data = serverData;
        }
    } catch (Exception ex) {
    }
    ObjectNode themeOut = (ObjectNode) Json.parse(data);
    appendAzureMapsKey(themeOut);
    return CompletableFuture.supplyAsync(() -> fromJson(themeOut.toString(), Object.class));
}
 
Example 20
Source File: PubSubTest.java    From WAMPlay with MIT License 4 votes vote down vote up
private void subscribe(String topic, WAMPlayClient client) {
	JsonNode req = Json.parse("[5, \"" + topic + "\"]");
	WAMPlayServer.handleRequest(client, req);
}