예제 #1
0
  private RequestBuilder buildProfileRequest(ProfileOptions options) {
    Validate.notNull(options, "options cannot be null");
    Validate.isTrue(
        options.getText() != null || options.getContentItems() != null,
        "text, html or content items need to be specified");

    final String contentType = options.getContentType();

    final RequestBuilder request = RequestBuilder.post(PATH_PROFILE);

    if (options.getText() != null) {
      request.withBodyContent(options.getText(), contentType);
    } else {
      final Content content = new Content();
      content.setContentItems(options.getContentItems());
      String body = gson.toJson(content);
      request.withBodyContent(body, contentType);
    }

    if (options.getIncludeRaw() != null) request.withQuery(INCLUDE_RAW, options.getIncludeRaw());

    if (options.getLanguage() != null)
      request.withHeader(HttpHeaders.CONTENT_LANGUAGE, options.getLanguage());

    if (options.getAcceptLanguage() != null)
      request.withHeader(HttpHeaders.ACCEPT_LANGUAGE, options.getAcceptLanguage());

    return request;
  }
  /** Test get profile. */
  @Test
  public void testGetProfileWithContent() {

    ContentItem cItem = new ContentItem();
    cItem.setContent(englishText);

    Content content = new Content();
    content.addContentItem(cItem);

    Map<String, Object> params = new HashMap<String, Object>();
    params.put(PersonalityInsights.CONTENT, content);
    String contentJson = GsonSingleton.getGson().toJson(params.get(PersonalityInsights.CONTENT));

    Profile p = new Profile();
    p.setId("*UNKNOWN*");
    p.setSource("*UNKNOWN*");
    p.setWordCount(1339);
    p.setWordCountMessage(
        "There were 1,339 words in the input. We need a minimum of 3,500, preferably 6,000 or more, to compute a reliable estimate");
    p.setProcessedLanguage("en");

    Trait root = new Trait();
    root.setId("r");
    root.setName("root");

    Trait childA = new Trait();
    childA.setId("personality");
    childA.setName("Big 5");
    List<Trait> rootKids = new ArrayList<Trait>();
    rootKids.add(childA);
    root.setChildren(rootKids);

    Trait childA1 = new Trait();
    childA1.setId("Openness_parent");
    childA1.setName("Openness");
    childA1.setCategory("personality");
    childA1.setPercentage(0.8834414318445747);
    List<Trait> childAKids = new ArrayList<Trait>();
    childAKids.add(childA1);
    childA.setChildren(childAKids);

    Trait childA11 = new Trait();
    childA11.setId("Openness");
    childA11.setName("Openness");
    childA11.setCategory("personality");
    childA11.setPercentage(0.8834414318445747);
    childA11.setSamplingError(0.0577082256);
    List<Trait> childA1Kids = new ArrayList<Trait>();
    childA1Kids.add(childA11);
    childA1.setChildren(childA1Kids);

    Trait childA110 = new Trait();
    childA110.setId("Adventurousness");
    childA110.setName("Adventurousness");
    childA110.setCategory("personality");
    childA110.setPercentage(0.7395861152833635);
    childA110.setSamplingError(0.0494178204);

    Trait childA111 = new Trait();
    childA111.setId("Imagination");
    childA111.setName("Imagination");
    childA111.setCategory("personality");
    childA111.setPercentage(0.8226761749222228);
    childA111.setSamplingError(0.0616986395);

    Trait childA112 = new Trait();
    childA112.setId("Artistic interests");
    childA112.setName("Artistic interests");
    childA112.setCategory("personality");
    childA112.setPercentage(0.5127471735266416);
    childA112.setSamplingError(0.0464200574);

    Trait childA113 = new Trait();
    childA113.setId("Emotionality");
    childA113.setName("Emotionality");
    childA113.setCategory("personality");
    childA113.setPercentage(0.3995729122484725);
    childA113.setSamplingError(0.0464200574);

    List<Trait> childA11Kids = new ArrayList<Trait>();
    childA11Kids.add(childA110);
    childA11Kids.add(childA111);
    childA11Kids.add(childA112);
    childA11Kids.add(childA113);
    childA11.setChildren(childA11Kids);

    p.setTree(root);

    mockServer
        .when(
            request()
                .withMethod("POST")
                .withPath(GET_PROFILE_PATH)
                .withBody(contentJson.toString()))
        .respond(
            response()
                .withHeaders(new Header(HttpHeaders.Names.CONTENT_TYPE, MediaType.APPLICATION_JSON))
                .withBody(GsonSingleton.getGson().toJson(p)));

    Profile profile = service.getProfile(params);
    Assert.assertNotNull(profile);
    Assert.assertEquals(profile, p);
  }