diff --git a/src/main/java/com/memetix/mst/MicrosoftTranslatorAPI.java b/src/main/java/com/memetix/mst/MicrosoftTranslatorAPI.java index b719fd9..3cf493c 100644 --- a/src/main/java/com/memetix/mst/MicrosoftTranslatorAPI.java +++ b/src/main/java/com/memetix/mst/MicrosoftTranslatorAPI.java @@ -35,24 +35,24 @@ * Makes the generic Microsoft Translator API calls. Different service classes then * extend this to make the specific service calls. * - * Uses the AJAX Interface V2 - see: http://msdn.microsoft.com/en-us/library/ff512404.aspx + * see: https://www.microsoft.com/en-us/translator/getstarted.aspx * * @author Jonathan Griggs */ public abstract class MicrosoftTranslatorAPI { //Encoding type protected static final String ENCODING = "UTF-8"; - + + + private static String MicrosoftCognitiveServicesAuthTokenUri = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken"; + protected static String apiKey; - private static String DatamarketAccessUri = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13"; private static String referrer; - private static String clientId; - private static String clientSecret; private static String token; private static long tokenExpiration = 0; private static String contentType = "text/plain"; - protected static final String PARAM_APP_ID = "appId=", + protected static final String PARAM_TO_LANG = "&to=", PARAM_FROM_LANG = "&from=", PARAM_TEXT_SINGLE = "&text=", @@ -64,46 +64,25 @@ public abstract class MicrosoftTranslatorAPI { /** * Sets the API key. - * - * Note: Should ONLY be used with API Keys generated prior to March 31, 2012. All new applications should obtain a ClientId and Client Secret by following - * the guide at: http://msdn.microsoft.com/en-us/library/hh454950.aspx + * + * Note: DataMarket and Data Services are being retired and will stop accepting new orders after 12/31/2016. Existing subscriptions will be retired and cancelled starting 3/31/2017. + * the guide at: https://www.microsoft.com/en-us/translator/getstarted.aspx + * * @param pKey The API key. */ public static void setKey(final String pKey) { apiKey = pKey; } - - /** - * Sets the API key. - * - * Note: Should ONLY be used with API Keys generated prior to March 31, 2012. All new applications should obtain a ClientId and Client Secret by following - * the guide at: http://msdn.microsoft.com/en-us/library/hh454950.aspx - * @param pKey The API key. - */ - public static void setContentType(final String pKey) { - contentType = pKey; - } - - /** - * Sets the Client ID. - * All new applications should obtain a ClientId and Client Secret by following - * the guide at: http://msdn.microsoft.com/en-us/library/hh454950.aspx - * @param pKey The Client Id. - */ - public static void setClientId(final String pClientId) { - clientId = pClientId; - } - + /** - * Sets the Client Secret. - * All new applications should obtain a ClientId and Client Secret by following - * the guide at: http://msdn.microsoft.com/en-us/library/hh454950.aspx - * @param pKey The Client Secret. + * Sets the content type. + * + * @param type The content type. */ - public static void setClientSecret(final String pClientSecret) { - clientSecret = pClientSecret; + public static void setContentType(final String type) { + contentType = type; } - + /** * Sets the Http Referrer. * @param pReferrer The HTTP client referrer. @@ -111,42 +90,39 @@ public static void setClientSecret(final String pClientSecret) { public static void setHttpReferrer(final String pReferrer) { referrer = pReferrer; } + /** * Gets the OAuth access token. - * @param clientId The Client key. - * @param clientSecret The Client Secret + * @param key The Client key. */ - public static String getToken(final String clientId, final String clientSecret) throws Exception { - final String params = "grant_type=client_credentials&scope=http://api.microsofttranslator.com" - + "&client_id=" + URLEncoder.encode(clientId,ENCODING) - + "&client_secret=" + URLEncoder.encode(clientSecret,ENCODING) ; + public static String getToken(final String key) throws Exception { + //final String params = "Subscription-Key=" + URLEncoder.encode(key,ENCODING); - final URL url = new URL(DatamarketAccessUri); - final HttpURLConnection uc = (HttpURLConnection) url.openConnection(); - if(referrer!=null) - uc.setRequestProperty("referer", referrer); - uc.setRequestProperty("Content-Type","application/x-www-form-urlencoded; charset=" + ENCODING); - uc.setRequestProperty("Accept-Charset",ENCODING); - uc.setRequestMethod("POST"); - uc.setDoOutput(true); + final URL url = new URL(MicrosoftCognitiveServicesAuthTokenUri); + final HttpURLConnection uc = (HttpURLConnection) url.openConnection(); + uc.setRequestProperty("Ocp-Apim-Subscription-Key", key); + uc.setRequestProperty("Content-Type","application/json"); + uc.setRequestProperty("Accept","application/jwt"); + uc.setRequestMethod("POST"); + uc.setDoOutput(true); - OutputStreamWriter wr = new OutputStreamWriter(uc.getOutputStream()); - wr.write(params); - wr.flush(); + OutputStreamWriter wr = new OutputStreamWriter(uc.getOutputStream()); + //wr.write(params); + wr.flush(); - try { - final int responseCode = uc.getResponseCode(); - final String result = inputStreamToString(uc.getInputStream()); - if(responseCode!=200) { - throw new Exception("Error from Microsoft Translator API: " + result); - } - return result; - } finally { - if(uc!=null) { - uc.disconnect(); - } - } - } + try { + final int responseCode = uc.getResponseCode(); + final String result = inputStreamToString(uc.getInputStream()); + if(responseCode!=200) { + throw new Exception("Error from Microsoft Translator API: " + result); + } + return result; + } finally { + if(uc!=null) { + uc.disconnect(); + } + } + } /** * Forms an HTTP request, sends it using GET method and returns the result of the request as a String. @@ -156,11 +132,12 @@ public static String getToken(final String clientId, final String clientSecret) * @throws Exception on error. */ private static String retrieveResponse(final URL url) throws Exception { - if(clientId!=null&&clientSecret!=null&&System.currentTimeMillis()>tokenExpiration) { - String tokenJson = getToken(clientId,clientSecret); - Integer expiresIn = Integer.parseInt((String)((JSONObject)JSONValue.parse(tokenJson)).get("expires_in")); - tokenExpiration = System.currentTimeMillis()+((expiresIn*1000)-1); - token = "Bearer " + (String)((JSONObject)JSONValue.parse(tokenJson)).get("access_token"); + + if(apiKey!=null&&System.currentTimeMillis()>tokenExpiration) { + String authToken = getToken(apiKey); + // set expiration time to 8 minutes + tokenExpiration = System.currentTimeMillis()+(8*60*1000); + token = "Bearer " + authToken; } final HttpURLConnection uc = (HttpURLConnection) url.openConnection(); if(referrer!=null) @@ -196,7 +173,7 @@ private static String retrieveResponse(final URL url) throws Exception { */ protected static String retrieveString(final URL url) throws Exception { try { - final String response = retrieveResponse(url); + final String response = retrieveResponse(url); return jsonToString(response); } catch (Exception ex) { throw new Exception("[microsoft-translator-api] Error retrieving translation : " + ex.getMessage(), ex); @@ -317,10 +294,8 @@ private static String inputStreamToString(final InputStream inputStream) throws //Check if ready to make request, if not, throw a RuntimeException protected static void validateServiceState() throws Exception { - if(apiKey!=null&&apiKey.length()<16) { - throw new RuntimeException("INVALID_API_KEY - Please set the API Key with your Bing Developer's Key"); - } else if (apiKey==null&&(clientId==null||clientSecret==null)) { - throw new RuntimeException("Must provide a Windows Azure Marketplace Client Id and Client Secret - Please see http://msdn.microsoft.com/en-us/library/hh454950.aspx for further documentation"); + if(apiKey!=null&&apiKey.length()<32) { + throw new RuntimeException("INVALID_API_KEY - Please set the API Key with your Azure Cognitive Services Developer's Key - Please see https://www.microsoft.com/en-us/translator/getstarted.aspx"); } } diff --git a/src/main/java/com/memetix/mst/detect/Detect.java b/src/main/java/com/memetix/mst/detect/Detect.java index 3fcfdc5..b210372 100644 --- a/src/main/java/com/memetix/mst/detect/Detect.java +++ b/src/main/java/com/memetix/mst/detect/Detect.java @@ -48,8 +48,7 @@ public final class Detect extends MicrosoftTranslatorAPI { public static Language execute(final String text) throws Exception { //Run the basic service validations first validateServiceState(text); - final URL url = new URL(SERVICE_URL - +(apiKey != null ? PARAM_APP_ID + URLEncoder.encode(apiKey,ENCODING) : "") + final URL url = new URL(SERVICE_URL +PARAM_TEXT_SINGLE+URLEncoder.encode(text, ENCODING)); final String response = retrieveString(url); @@ -67,8 +66,7 @@ public static String[] execute(final String[] texts) throws Exception { //Run the basic service validations first validateServiceState(texts); final String textArr = buildStringArrayParam(texts); - final URL url = new URL(ARRAY_SERVICE_URL - +(apiKey != null ? PARAM_APP_ID + URLEncoder.encode(apiKey,ENCODING) : "") + final URL url = new URL(ARRAY_SERVICE_URL +PARAM_TEXT_ARRAY+URLEncoder.encode(textArr, ENCODING)); final String[] response = retrieveStringArr(url); return response; diff --git a/src/main/java/com/memetix/mst/language/Language.java b/src/main/java/com/memetix/mst/language/Language.java index 498fbab..dd27a59 100644 --- a/src/main/java/com/memetix/mst/language/Language.java +++ b/src/main/java/com/memetix/mst/language/Language.java @@ -114,13 +114,7 @@ public static void setKey(String pKey) { LanguageService.setKey(pKey); } - public static void setClientId(String pId) { - LanguageService.setClientId(pId); - } - public static void setClientSecret(String pSecret) { - LanguageService.setClientSecret(pSecret); - } - + /** * getName() * @@ -221,7 +215,6 @@ public static String[] execute(final Language[] targets, final Language locale) final String targetString = buildStringArrayParam(Language.values()); final URL url = new URL(SERVICE_URL - +(apiKey != null ? PARAM_APP_ID + URLEncoder.encode(apiKey,ENCODING) : "") +PARAM_LOCALE+URLEncoder.encode(locale.toString(), ENCODING) +PARAM_LANGUAGE_CODES + URLEncoder.encode(targetString, ENCODING)); localizedNames = retrieveStringArr(url); @@ -245,7 +238,7 @@ public static String[] execute() throws Exception { validateServiceState(); String[] codes = new String[0]; - final URL url = new URL(SERVICE_URL +(apiKey != null ? PARAM_APP_ID + URLEncoder.encode(apiKey,ENCODING) : "")); + final URL url = new URL(SERVICE_URL); codes = retrieveStringArr(url); return codes; } diff --git a/src/main/java/com/memetix/mst/language/SpokenDialect.java b/src/main/java/com/memetix/mst/language/SpokenDialect.java index 55c1513..bd310fe 100644 --- a/src/main/java/com/memetix/mst/language/SpokenDialect.java +++ b/src/main/java/com/memetix/mst/language/SpokenDialect.java @@ -98,14 +98,7 @@ public static void setKey(String pKey) { SpokenDialectService.setKey(pKey); } - public static void setClientId(String pId) { - SpokenDialectService.setClientId(pId); - } - - public static void setClientSecret(String pSecret) { - SpokenDialectService.setClientSecret(pSecret); - } - + /** * getName() * @@ -172,7 +165,6 @@ public static String[] execute(final SpokenDialect[] targets, final Language loc final String targetString = buildStringArrayParam(SpokenDialect.values()); final URL url = new URL(SERVICE_URL - +(apiKey != null ? PARAM_APP_ID + URLEncoder.encode(apiKey,ENCODING) : "") +PARAM_LOCALE+URLEncoder.encode(locale.toString(), ENCODING) +PARAM_LANGUAGE_CODES + URLEncoder.encode(targetString, ENCODING)); localizedNames = retrieveStringArr(url); diff --git a/src/main/java/com/memetix/mst/sentence/BreakSentences.java b/src/main/java/com/memetix/mst/sentence/BreakSentences.java index 7e5dc48..617357d 100644 --- a/src/main/java/com/memetix/mst/sentence/BreakSentences.java +++ b/src/main/java/com/memetix/mst/sentence/BreakSentences.java @@ -53,7 +53,6 @@ public static Integer[] execute(final String text, final Language fromLang) thro //Run the basic service validations first validateServiceState(text,fromLang); final URL url = new URL(SERVICE_URL - +(apiKey != null ? PARAM_APP_ID + URLEncoder.encode(apiKey,ENCODING) : "") +PARAM_SENTENCES_LANGUAGE+URLEncoder.encode(fromLang.toString(), ENCODING) +PARAM_TEXT_SINGLE+URLEncoder.encode(text, ENCODING)); diff --git a/src/main/java/com/memetix/mst/speak/Speak.java b/src/main/java/com/memetix/mst/speak/Speak.java index 43d936a..93b8a19 100644 --- a/src/main/java/com/memetix/mst/speak/Speak.java +++ b/src/main/java/com/memetix/mst/speak/Speak.java @@ -50,7 +50,6 @@ public static String execute(final String text, final SpokenDialect language) th //Run the basic service validations first validateServiceState(text); final URL url = new URL(SERVICE_URL - +(apiKey != null ? PARAM_APP_ID + URLEncoder.encode(apiKey,ENCODING) : "") +PARAM_SPOKEN_LANGUAGE+URLEncoder.encode(language.toString(),ENCODING) +PARAM_TEXT_SINGLE+URLEncoder.encode(text, ENCODING)); final String response = retrieveString(url); diff --git a/src/main/java/com/memetix/mst/translate/Translate.java b/src/main/java/com/memetix/mst/translate/Translate.java index 9503f3d..bf2d1ff 100644 --- a/src/main/java/com/memetix/mst/translate/Translate.java +++ b/src/main/java/com/memetix/mst/translate/Translate.java @@ -52,13 +52,12 @@ public static String execute(final String text, final Language from, final Langu //Run the basic service validations first validateServiceState(text); final String params = - (apiKey != null ? PARAM_APP_ID + URLEncoder.encode(apiKey,ENCODING) : "") - + PARAM_FROM_LANG + URLEncoder.encode(from.toString(),ENCODING) + PARAM_FROM_LANG + URLEncoder.encode(from.toString(),ENCODING) + PARAM_TO_LANG + URLEncoder.encode(to.toString(),ENCODING) + PARAM_TEXT_SINGLE + URLEncoder.encode(text,ENCODING); final URL url = new URL(SERVICE_URL + params); - final String response = retrieveString(url); + final String response = retrieveString(url); return response; } @@ -92,8 +91,7 @@ public static String[] execute(final String[] texts, final Language from, final //Run the basic service validations first validateServiceState(texts); final String params = - (apiKey != null ? PARAM_APP_ID + URLEncoder.encode(apiKey,ENCODING) : "") - + PARAM_FROM_LANG + URLEncoder.encode(from.toString(),ENCODING) + PARAM_FROM_LANG + URLEncoder.encode(from.toString(),ENCODING) + PARAM_TO_LANG + URLEncoder.encode(to.toString(),ENCODING) + PARAM_TEXT_ARRAY + URLEncoder.encode(buildStringArrayParam(texts),ENCODING); @@ -112,7 +110,6 @@ public static String[] execute(final String[] texts, final Language from, final * execute(texts[],fromLang,toLang) * * @param texts The Strings Array to translate. - * @param from The language code to translate from. * @param to The language code to translate to. * @return The translated Strings Array[]. * @throws Exception on error. diff --git a/src/test/java/com/memetix/mst/detect/DetectTest.java b/src/test/java/com/memetix/mst/detect/DetectTest.java index d364da7..846f8e0 100644 --- a/src/test/java/com/memetix/mst/detect/DetectTest.java +++ b/src/test/java/com/memetix/mst/detect/DetectTest.java @@ -43,20 +43,10 @@ public void setUp() throws Exception { p = new Properties(); URL url = ClassLoader.getSystemResource("META-INF/config.properties"); p.load(url.openStream()); - String apiKey = p.getProperty("microsoft.translator.api.key"); + String apiKey = p.getProperty("microsoft.azure.subscription.key"); if(System.getProperty("test.api.key")!=null) { apiKey = System.getProperty("test.api.key").split(",")[0]; } - String clientId = p.getProperty("microsoft.translator.api.clientId"); - if(System.getProperty("test.api.key")!=null) { - clientId = System.getProperty("test.api.key").split(",")[1]; - } - String clientSecret = p.getProperty("microsoft.translator.api.clientSecret"); - if(System.getProperty("test.api.key")!=null) { - clientSecret = System.getProperty("test.api.key").split(",")[2]; - } - Detect.setClientId(clientId); - Detect.setClientSecret(clientSecret); Detect.setKey(apiKey); } @@ -102,37 +92,33 @@ public void testDetectArraySingle() throws Exception { @Test public void testDetect_WrongKey() throws Exception { Detect.setKey("wrong_key"); - Detect.setClientId(null); exception.expect(RuntimeException.class); - exception.expectMessage("INVALID_API_KEY - Please set the API Key with your Bing Developer's Key"); + exception.expectMessage("INVALID_API_KEY - Please set the API Key with your Azure Subscription Key"); Detect.execute("전 세계 여러분 안녕하세요"); } @Test public void testDetect_NoKey() throws Exception { Detect.setKey(null); - Detect.setClientId(null); exception.expect(RuntimeException.class); - exception.expectMessage("Must provide a Windows Azure Marketplace Client Id and Client Secret - Please see http://msdn.microsoft.com/en-us/library/hh454950.aspx for further documentation"); + exception.expectMessage("Must provide a Windows Azure Subscription Key - Please see https://www.microsoft.com/en-us/translator/getstarted.aspx for further documentation"); Detect.execute("전 세계 여러분 안녕하세요"); } @Test public void testDetectArray_NoKey() throws Exception { Detect.setKey(null); - Detect.setClientId(null); String[] texts = {"Hello world!"}; exception.expect(RuntimeException.class); - exception.expectMessage("Must provide a Windows Azure Marketplace Client Id and Client Secret - Please see http://msdn.microsoft.com/en-us/library/hh454950.aspx for further documentation"); + exception.expectMessage("Must provide a Windows Azure Subscription Key - Please see https://www.microsoft.com/en-us/translator/getstarted.aspx for further documentation"); Detect.execute(texts); } @Test public void testDetectArray_WrongKey() throws Exception { Detect.setKey("wrong_key"); - Detect.setClientId(null); String[] texts = {"Hello world!"}; exception.expect(RuntimeException.class); - exception.expectMessage("INVALID_API_KEY - Please set the API Key with your Bing Developer's Key"); + exception.expectMessage("INVALID_API_KEY - Please set the API Key with your Azure Subscription Key"); Detect.execute(texts); } diff --git a/src/test/java/com/memetix/mst/language/LanguageTest.java b/src/test/java/com/memetix/mst/language/LanguageTest.java index ee42e9b..9087243 100644 --- a/src/test/java/com/memetix/mst/language/LanguageTest.java +++ b/src/test/java/com/memetix/mst/language/LanguageTest.java @@ -47,20 +47,10 @@ public void setUp() throws Exception { p = new Properties(); URL url = ClassLoader.getSystemResource("META-INF/config.properties"); p.load(url.openStream()); - String apiKey = p.getProperty("microsoft.translator.api.key"); + String apiKey = p.getProperty("microsoft.azure.subscription.key"); if(System.getProperty("test.api.key")!=null) { apiKey = System.getProperty("test.api.key").split(",")[0]; } - String clientId = p.getProperty("microsoft.translator.api.clientId"); - if(System.getProperty("test.api.key")!=null) { - clientId = System.getProperty("test.api.key").split(",")[1]; - } - String clientSecret = p.getProperty("microsoft.translator.api.clientSecret"); - if(System.getProperty("test.api.key")!=null) { - clientSecret = System.getProperty("test.api.key").split(",")[2]; - } - Language.setClientId(clientId); - Language.setClientSecret(clientSecret); Language.setKey(apiKey); } @@ -103,11 +93,10 @@ public void testFromString_ClientIdOnly() { @Test public void testGetLanguage_NoKey() throws Exception { Language.setKey(null); - Language.setClientId(null); Language locale = Language.PERSIAN; exception.expect(RuntimeException.class); - exception.expectMessage("Must provide a Windows Azure Marketplace Client Id and Client Secret - Please see http://msdn.microsoft.com/en-us/library/hh454950.aspx for further documentation"); + exception.expectMessage("Must provide a Windows Azure Subscription Key - Please see https://www.microsoft.com/en-us/translator/getstarted.aspx for further documentation"); Language.FRENCH.getName(locale); } @@ -117,7 +106,7 @@ public void testGetLanguage_WrongKey() throws Exception { Language locale = Language.PERSIAN; exception.expect(RuntimeException.class); - exception.expectMessage("INVALID_API_KEY - Please set the API Key with your Bing Developer's Key"); + exception.expectMessage("INVALID_API_KEY - Please set the API Key with your Azure Subscription Key"); Language.FRENCH.getName(locale); } diff --git a/src/test/java/com/memetix/mst/language/SpokenDialectTest.java b/src/test/java/com/memetix/mst/language/SpokenDialectTest.java index ab57982..89a4043 100644 --- a/src/test/java/com/memetix/mst/language/SpokenDialectTest.java +++ b/src/test/java/com/memetix/mst/language/SpokenDialectTest.java @@ -45,21 +45,11 @@ public void setUp() throws Exception { p = new Properties(); URL url = ClassLoader.getSystemResource("META-INF/config.properties"); p.load(url.openStream()); - String apiKey = p.getProperty("microsoft.translator.api.key"); + String apiKey = p.getProperty("microsoft.azure.subscription.key"); if(System.getProperty("test.api.key")!=null) { apiKey = System.getProperty("test.api.key").split(",")[0]; } - String clientId = p.getProperty("microsoft.translator.api.clientId"); - if(System.getProperty("test.api.key")!=null) { - clientId = System.getProperty("test.api.key").split(",")[1]; - } - String clientSecret = p.getProperty("microsoft.translator.api.clientSecret"); - if(System.getProperty("test.api.key")!=null) { - clientSecret = System.getProperty("test.api.key").split(",")[2]; - } SpokenDialect.setKey(apiKey); - SpokenDialect.setClientId(clientId); - SpokenDialect.setClientSecret(clientSecret); } @After @@ -71,11 +61,10 @@ public void tearDown() throws Exception { public void testGetSpokenDialect_NoKey() throws Exception { SpokenDialect.flushNameCache(); SpokenDialect.setKey(null); - SpokenDialect.setClientId(null); Language locale = Language.ENGLISH; exception.expect(RuntimeException.class); - exception.expectMessage("Must provide a Windows Azure Marketplace Client Id and Client Secret - Please see http://msdn.microsoft.com/en-us/library/hh454950.aspx for further documentation"); + exception.expectMessage("Must provide a Windows Azure Subscription Key - Please see https://www.microsoft.com/en-us/translator/getstarted.aspx for further documentation"); SpokenDialect.FRENCH_CANADA.getName(locale); } @@ -86,7 +75,7 @@ public void testGetSpokenDialect_WrongKey() throws Exception { Language locale = Language.ENGLISH; exception.expect(RuntimeException.class); - exception.expectMessage("INVALID_API_KEY - Please set the API Key with your Bing Developer's Key"); + exception.expectMessage("INVALID_API_KEY - Please set the API Key with your Azure Subscription Key"); SpokenDialect.FRENCH_CANADA.getName(locale); } diff --git a/src/test/java/com/memetix/mst/sentence/BreakSentencesTest.java b/src/test/java/com/memetix/mst/sentence/BreakSentencesTest.java index 9616d27..5835356 100644 --- a/src/test/java/com/memetix/mst/sentence/BreakSentencesTest.java +++ b/src/test/java/com/memetix/mst/sentence/BreakSentencesTest.java @@ -42,21 +42,12 @@ public void setUp() throws Exception { p = new Properties(); URL url = ClassLoader.getSystemResource("META-INF/config.properties"); p.load(url.openStream()); - String apiKey = p.getProperty("microsoft.translator.api.key"); + String apiKey = p.getProperty("microsoft.azure.subscription.key"); if(System.getProperty("test.api.key")!=null) { apiKey = System.getProperty("test.api.key").split(",")[0]; } - String clientId = p.getProperty("microsoft.translator.api.clientId"); - if(System.getProperty("test.api.key")!=null) { - clientId = System.getProperty("test.api.key").split(",")[1]; - } - String clientSecret = p.getProperty("microsoft.translator.api.clientSecret"); - if(System.getProperty("test.api.key")!=null) { - clientSecret = System.getProperty("test.api.key").split(",")[2]; - } BreakSentences.setKey(apiKey); - BreakSentences.setClientSecret(clientSecret); - BreakSentences.setClientId(clientId); + } @After @@ -82,18 +73,17 @@ public void testBreakSentences_AutoDetect() throws Exception { @Test public void testBreakSentences_NoKey() throws Exception { BreakSentences.setKey(null); - BreakSentences.setClientId(null); exception.expect(RuntimeException.class); - exception.expectMessage("Must provide a Windows Azure Marketplace Client Id and Client Secret - Please see http://msdn.microsoft.com/en-us/library/hh454950.aspx for further documentation"); - BreakSentences.execute("This is a sentence. That is a sentence. There are hopefully 3 sentences detected.",Language.ENGLISH); + exception.expectMessage("Must provide a Windows Azure Subscription Key - Please see https://www.microsoft.com/en-us/translator/getstarted.aspx for further documentation"); + BreakSentences.execute("This is a sentence. That is a sentence. There are hopefully 3 sentences detected.",Language.ENGLISH); + } + @Test + public void testBreakSentences_WrongKey() throws Exception { + BreakSentences.setKey("wrong"); + exception.expect(RuntimeException.class); + exception.expectMessage("INVALID_API_KEY - Please set the API Key with your Azure Subscription Key"); + BreakSentences.execute("This is a sentence. That is a sentence. There are hopefully 3 sentences detected.",Language.ENGLISH); } - @Test - public void testBreakSentences_WrongKey() throws Exception { - BreakSentences.setKey("wrong"); - exception.expect(RuntimeException.class); - exception.expectMessage("INVALID_API_KEY - Please set the API Key with your Bing Developer's Key"); - BreakSentences.execute("This is a sentence. That is a sentence. There are hopefully 3 sentences detected.",Language.ENGLISH); - } @Test public void testBreakSentencesEnglish_Large() throws Exception { diff --git a/src/test/java/com/memetix/mst/speak/SpeakTest.java b/src/test/java/com/memetix/mst/speak/SpeakTest.java index e863459..5e2c415 100644 --- a/src/test/java/com/memetix/mst/speak/SpeakTest.java +++ b/src/test/java/com/memetix/mst/speak/SpeakTest.java @@ -44,21 +44,12 @@ public void setUp() throws Exception { p = new Properties(); URL url = ClassLoader.getSystemResource("META-INF/config.properties"); p.load(url.openStream()); - String apiKey = p.getProperty("microsoft.translator.api.key"); + String apiKey = p.getProperty("microsoft.azure.subscription.key"); if(System.getProperty("test.api.key")!=null) { apiKey = System.getProperty("test.api.key").split(",")[0]; } - String clientId = p.getProperty("microsoft.translator.api.clientId"); - if(System.getProperty("test.api.key")!=null) { - clientId = System.getProperty("test.api.key").split(",")[1]; - } - String clientSecret = p.getProperty("microsoft.translator.api.clientSecret"); - if(System.getProperty("test.api.key")!=null) { - clientSecret = System.getProperty("test.api.key").split(",")[2]; - } Speak.setKey(apiKey); - Speak.setClientSecret(clientSecret); - Speak.setClientId(clientId); + } @After @@ -72,9 +63,8 @@ public void tearDown() throws Exception { @Test public void testGetSpeakUrl_NoKey() throws Exception { Speak.setKey(null); - Speak.setClientId(null); exception.expect(RuntimeException.class); - exception.expectMessage("Must provide a Windows Azure Marketplace Client Id and Client Secret - Please see http://msdn.microsoft.com/en-us/library/hh454950.aspx for further documentation"); + exception.expectMessage("Must provide a Windows Azure Subscription Key - Please see https://www.microsoft.com/en-us/translator/getstarted.aspx for further documentation"); String text = "Hello World!"; SpokenDialect language = SpokenDialect.ENGLISH_INDIA; Speak.execute(text, language); diff --git a/src/test/java/com/memetix/mst/token/GetTokenTest.java b/src/test/java/com/memetix/mst/token/GetTokenTest.java new file mode 100644 index 0000000..9f30ec1 --- /dev/null +++ b/src/test/java/com/memetix/mst/token/GetTokenTest.java @@ -0,0 +1,60 @@ +/* + * microsoft-translator-java-api + * + * Copyright 2012 Jonathan Griggs . + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.memetix.mst.token; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.net.URL; +import java.util.Properties; + +import com.memetix.mst.MicrosoftTranslatorAPI; +import com.memetix.mst.detect.Detect; +import com.memetix.mst.language.Language; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +/** + * Unit Tests for the Auth Token + * @author Thomas Lehoux + */ +public class GetTokenTest { + Properties p; + + + @Rule + public ExpectedException exception = ExpectedException.none(); + + + @Test + public void testGetToken() throws Exception { + + p = new Properties(); + URL url = ClassLoader.getSystemResource("META-INF/config.properties"); + p.load(url.openStream()); + String apiKey = p.getProperty("microsoft.azure.subscription.key"); + if(System.getProperty("test.api.key")!=null) { + apiKey = System.getProperty("test.api.key").split(",")[0]; + } + assertNotNull(MicrosoftTranslatorAPI.getToken(apiKey)); + } + +} diff --git a/src/test/java/com/memetix/mst/translate/TranslateTest.java b/src/test/java/com/memetix/mst/translate/TranslateTest.java index a52ad5c..9291ece 100644 --- a/src/test/java/com/memetix/mst/translate/TranslateTest.java +++ b/src/test/java/com/memetix/mst/translate/TranslateTest.java @@ -46,29 +46,18 @@ public void setUp() throws Exception { p = new Properties(); URL url = ClassLoader.getSystemResource("META-INF/config.properties"); p.load(url.openStream()); - String apiKey = p.getProperty("microsoft.translator.api.key"); + String apiKey = p.getProperty("microsoft.azure.subscription.key"); if(System.getProperty("test.api.key")!=null) { apiKey = System.getProperty("test.api.key").split(",")[0]; } - String clientId = p.getProperty("microsoft.translator.api.clientId"); - if(System.getProperty("test.api.key")!=null) { - clientId = System.getProperty("test.api.key").split(",")[1]; - } - String clientSecret = p.getProperty("microsoft.translator.api.clientSecret"); - if(System.getProperty("test.api.key")!=null) { - clientSecret = System.getProperty("test.api.key").split(",")[2]; - } Translate.setKey(apiKey); - Translate.setClientSecret(clientSecret); - Translate.setClientId(clientId); + } @After public void tearDown() throws Exception { Translate.setKey(null); Translate.setContentType("text/plain"); - Translate.setClientId(null); - Translate.setClientSecret(null); Translate.setHttpReferrer(null); } @@ -162,26 +151,20 @@ public void testTranslate_EnglisthToHebrew_Unicode() throws Exception { @Test public void testTranslate_NoKey() throws Exception { Translate.setKey(null); - Translate.setClientId(null); - Translate.setClientSecret(null); exception.expect(RuntimeException.class); - exception.expectMessage("Must provide a Windows Azure Marketplace Client Id and Client Secret - Please see http://msdn.microsoft.com/en-us/library/hh454950.aspx for further documentation"); + exception.expectMessage("Must provide a Windows Azure Subscription Key - Please see https://www.microsoft.com/en-us/translator/getstarted.aspx for further documentation"); Translate.execute("ハローワールド", Language.AUTO_DETECT, Language.ENGLISH); } @Test public void testTranslate_WrongKey() throws Exception { - Translate.setKey("lessthan16"); - Translate.setClientId(null); - Translate.setClientSecret(null); + Translate.setKey("lessthan32"); exception.expect(RuntimeException.class); - exception.expectMessage("INVALID_API_KEY - Please set the API Key with your Bing Developer's Key"); + exception.expectMessage("INVALID_API_KEY - Please set the API Key with your Azure Subscription Key"); Translate.execute("ハローワールド", Language.AUTO_DETECT, Language.ENGLISH); } @Test public void testTranslate_SetKeyNoClientIdAndSecret() throws Exception { - Translate.setClientId(null); - Translate.setClientSecret(null); String translate = Translate.execute("ハローワールド", Language.AUTO_DETECT, Language.ENGLISH); assertNotNull(translate); } diff --git a/src/test/resources/META-INF/config.properties b/src/test/resources/META-INF/config.properties index 35d2ac8..909ddcb 100644 --- a/src/test/resources/META-INF/config.properties +++ b/src/test/resources/META-INF/config.properties @@ -1,2 +1,2 @@ -microsoft.translator.api.key=INSERT_API_KEY +microsoft.azure.subscription.key=YOUR_API_KEY \ No newline at end of file