Skip to content

Commit

Permalink
fix(pom): upgrade sdk-core (#1220)
Browse files Browse the repository at this point in the history
This upgrade is mainly to remove the vulnerable guava dependency
  • Loading branch information
apaparazzi0329 committed Aug 8, 2023
1 parent 0b5ec7e commit 5ce39c7
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
package com.ibm.watson.common;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ibm.cloud.sdk.core.http.HttpMediaType;
import com.ibm.cloud.sdk.core.util.GsonSingleton;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -81,4 +84,18 @@ protected static MockResponse jsonResponse(Object body) {
.addHeader(CONTENT_TYPE, HttpMediaType.APPLICATION_JSON)
.setBody(GSON.toJson(body));
}

/**
* Create a MockResponse with JSON content type and the object serialized to JSON as body.
* For HashMaps
*
* @param body the body
* @return the mock response
*/
protected static MockResponse hashmapToJsonResponse(Object body) {
Type typeObject = new TypeToken<HashMap>() {}.getType();
return new MockResponse()
.addHeader(CONTENT_TYPE, HttpMediaType.APPLICATION_JSON)
.setBody(GSON.toJson(body, typeObject));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.ibm.cloud.sdk.core.http.HttpMediaType;
import com.ibm.cloud.sdk.core.http.Response;
import com.ibm.cloud.sdk.core.security.Authenticator;
Expand All @@ -32,6 +30,7 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -47,12 +46,11 @@ public class LanguageTranslatorIT extends WatsonServiceTest {
private LanguageTranslator service;

private final Map<String, String> translations =
ImmutableMap.of(
"The IBM Watson team is awesome",
"El equipo de IBM Watson es impresionante",
"Welcome to the cognitive era",
"Bienvenido a la era cognitiva");
private final List<String> texts = ImmutableList.copyOf(translations.keySet());
new HashMap<String, String>() {{
put("The IBM Watson team is awesome", "El equipo de IBM Watson es impresionante");
put("Welcome to the cognitive era", "Bienvenido a la era cognitiva");
}};
private final List<String> texts = new ArrayList<>(translations.keySet());

/**
* Sets up the tests.
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<sdk-core-version>9.17.3</sdk-core-version>
<sdk-core-version>9.18.4</sdk-core-version>

<!-- >>> Replace this with the name of your SDK's github repository -->
<git-repository-name>java-sdk</git-repository-name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

import com.google.common.collect.ImmutableList;
import com.ibm.cloud.sdk.core.http.HttpMediaType;
import com.ibm.cloud.sdk.core.security.Authenticator;
import com.ibm.cloud.sdk.core.security.IamAuthenticator;
Expand All @@ -28,6 +27,8 @@
import com.ibm.watson.text_to_speech.v1.model.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -79,7 +80,7 @@ private List<Word> instantiateWords() {
.word("shocking")
.translation("<phoneme alphabet='ibm' ph='.1Sa.0kIG'></phoneme>")
.build();
return ImmutableList.of(word1, word2);
return Collections.unmodifiableList(Arrays.asList(word1, word2));
}

private List<Word> instantiateWordsJapanese() {
Expand All @@ -95,7 +96,7 @@ private List<Word> instantiateWordsJapanese() {
.translation("close the door")
.partOfSpeech(Word.PartOfSpeech.HOKA)
.build();
return ImmutableList.of(word1, word2);
return Collections.unmodifiableList(Arrays.asList(word1, word2));
}

private CustomModel createCustomModel() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.ibm.cloud.sdk.core.security.NoAuthAuthenticator;
import com.ibm.watson.common.WatsonServiceUnitTest;
import com.ibm.watson.text_to_speech.v1.model.*;
import java.util.List;
import java.util.*;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.RecordedRequest;
import org.junit.Before;
Expand Down Expand Up @@ -80,7 +78,7 @@ private static Word instantiateWord() {
}

private static List<Word> instantiateWords() {
return ImmutableList.of(instantiateWord());
return Collections.unmodifiableList(Arrays.asList(instantiateWord()));
}

/**
Expand Down Expand Up @@ -331,7 +329,9 @@ public void testDeleteVoiceModel() throws InterruptedException {
public void testListWords() throws InterruptedException {
final List<Word> expected = instantiateWords();

server.enqueue(jsonResponse(ImmutableMap.of(WORDS, expected)));
server.enqueue(hashmapToJsonResponse(new HashMap<String, List<Word>>() {{
put(WORDS, expected);
}}));
ListWordsOptions listOptions =
new ListWordsOptions.Builder().customizationId(CUSTOMIZATION_ID).build();
final Words result = service.listWords(listOptions).execute().getResult();
Expand All @@ -351,7 +351,11 @@ public void testListWords() throws InterruptedException {
public void testGetWord() throws InterruptedException {
final Word expected = instantiateWords().get(0);

server.enqueue(jsonResponse(ImmutableMap.of(TRANSLATION, expected.translation())));
Map<String, String> map = new HashMap<String, String>() {{
put(TRANSLATION, expected.translation());
}};

server.enqueue(hashmapToJsonResponse(map));
GetWordOptions getOptions =
new GetWordOptions.Builder()
.customizationId(CUSTOMIZATION_ID)
Expand Down

0 comments on commit 5ce39c7

Please sign in to comment.