ToneAnalyzer extends BaseService { public ServiceCall<ToneAnalysis> tone(ToneOptions toneOptions) { com.ibm.cloud.sdk.core.util.Validator.notNull(toneOptions, "toneOptions cannot be null"); String[] pathSegments = {"v3/tone"}; RequestBuilder builder = RequestBuilder.post(RequestBuilder.constructHttpUrl(getServiceUrl(), pathSegments)); builder.query("version", versionDate); Map<String, String> sdkHeaders = SdkCommon.getSdkHeaders("tone_analyzer", "v3", "tone"); for (Entry<String, String> header : sdkHeaders.entrySet()) { builder.header(header.getKey(), header.getValue()); } builder.header("Accept", "application/json"); if (toneOptions.contentType() != null) { builder.header("Content-Type", toneOptions.contentType()); } if (toneOptions.contentLanguage() != null) { builder.header("Content-Language", toneOptions.contentLanguage()); } if (toneOptions.acceptLanguage() != null) { builder.header("Accept-Language", toneOptions.acceptLanguage()); } if (toneOptions.sentences() != null) { builder.query("sentences", String.valueOf(toneOptions.sentences())); } if (toneOptions.tones() != null) { builder.query("tones", RequestUtils.join(toneOptions.tones(), ",")); } builder.bodyContent( toneOptions.contentType(), toneOptions.toneInput(), null, toneOptions.body()); ResponseConverter<ToneAnalysis> responseConverter = ResponseConverterUtils.getValue( new com.google.gson.reflect.TypeToken<ToneAnalysis>() {}.getType()); return createServiceCall(builder.build(), responseConverter); } ToneAnalyzer(String versionDate);  ToneAnalyzer(String versionDate, Authenticator authenticator);  ToneAnalyzer(String versionDate, String serviceName);  ToneAnalyzer(String versionDate, String serviceName, Authenticator authenticator); ServiceCall<ToneAnalysis> tone(ToneOptions toneOptions); ServiceCall<UtteranceAnalyses> toneChat(ToneChatOptions toneChatOptions);  }
@Test public void testReadme() throws InterruptedException, IOException { ToneAnalyzer service = new ToneAnalyzer(VERSION_DATE, new NoAuthAuthenticator()); service.setServiceUrl(getMockWebServerUrl()); ToneAnalysis mockResponse = loadFixture(FIXTURE, ToneAnalysis.class); server.enqueue(jsonResponse(mockResponse)); String text = "I know the times are difficult! Our sales have been " + "disappointing for the past three quarters for our data analytics " + "product suite. We have a competitive data analytics product " + "suite in the industry. But we need to do our job selling it! " + "We need to acknowledge and fix our sales challenges. " + "We can’t blame the economy for our lack of execution! " + "We are missing critical sales opportunities. " + "Our product is in no way inferior to the competitor products. " + "Our clients are hungry for analytical tools to improve their " + "business outcomes. Economy has nothing to do with it."; ToneOptions toneOptions = new ToneOptions.Builder().html(text).build(); ToneAnalysis tone = service.tone(toneOptions).execute().getResult(); System.out.println(tone); }
@Test(expected = IllegalArgumentException.class) public void testtoneWithNull() { service.tone(null); }
@Test public void testtones() throws InterruptedException, IOException { String text = "I know the times are difficult! Our sales have been " + "disappointing for the past three quarters for our data analytics " + "product suite. We have a competitive data analytics product " + "suite in the industry. But we need to do our job selling it! "; ToneAnalysis mockResponse = loadFixture(FIXTURE, ToneAnalysis.class); server.enqueue(jsonResponse(mockResponse)); server.enqueue(jsonResponse(mockResponse)); server.enqueue(jsonResponse(mockResponse)); ToneOptions toneOptions = new ToneOptions.Builder().html(text).build(); ToneAnalysis serviceResponse = service.tone(toneOptions).execute().getResult(); RecordedRequest request = server.takeRequest(); String path = StringUtils.join(TONE_PATH, "?", VERSION_DATE, "=", VERSION_DATE_VALUE); assertEquals(path, request.getPath()); assertEquals(serviceResponse, mockResponse); assertEquals(HttpMediaType.APPLICATION_JSON, request.getHeader(HttpHeaders.ACCEPT)); serviceResponse = service.tone(new ToneOptions.Builder().html(text).build()).execute().getResult(); request = server.takeRequest(); assertEquals(path, request.getPath()); assertTrue(request.getHeader(HttpHeaders.CONTENT_TYPE).startsWith(HttpMediaType.TEXT_HTML)); ToneOptions toneOptions1 = new ToneOptions.Builder() .html(text) .addTone(ToneOptions.Tone.EMOTION) .addTone(ToneOptions.Tone.LANGUAGE) .addTone(ToneOptions.Tone.SOCIAL) .build(); serviceResponse = service.tone(toneOptions1).execute().getResult(); request = server.takeRequest(); path = path + "&tones=" + RequestUtils.encode("emotion,language,social"); assertEquals(path, request.getPath()); }