diff --git a/README.md b/README.md index b6b5dfa..f0fd3eb 100644 --- a/README.md +++ b/README.md @@ -11,9 +11,7 @@ is my goal to mimic the code structure, naming conventions, functionality, and u ## Requires * Java 1.5 or greater -* A Windows Azure Marketplace Client ID and Client Secret - [Documentation](http://msdn.microsoft.com/en-us/library/hh454950.aspx) - -_Please note: If you signed up for a Bing Developer Key after March 31, 2012, you will not be able to use your App Id with this API. Please visit the aforementioned documentation link_ +* A Microsoft Translator Text Translation Subscription Key - [Documentation](http://docs.microsofttranslator.com/text-translate.html) Quickstart =========== @@ -25,9 +23,8 @@ Download the latest [JAR with Dependencies](https://microsoft-translator-java-ap public class Main { public static void main(String[] args) throws Exception { - // Set your Windows Azure Marketplace client info - See http://msdn.microsoft.com/en-us/library/hh454950.aspx - Translate.setClientId(/* Enter your Windows Azure Client Id here */); - Translate.setClientSecret(/* Enter your Windows Azure Client Secret here */); + // Set your Microsoft Translator Text Translation Subscription Key - See http://docs.microsofttranslator.com/text-translate.html + Translate.setSubscriptionKey(/* Microsoft Translator Text Translation Subscription Key here */); String translatedText = Translate.execute("Bonjour le monde", Language.FRENCH, Language.ENGLISH); diff --git a/src/main/java/com/memetix/mst/MicrosoftTranslatorAPI.java b/src/main/java/com/memetix/mst/MicrosoftTranslatorAPI.java index b719fd9..cb5fde4 100644 --- a/src/main/java/com/memetix/mst/MicrosoftTranslatorAPI.java +++ b/src/main/java/com/memetix/mst/MicrosoftTranslatorAPI.java @@ -20,10 +20,8 @@ import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; -import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; -import java.net.URLEncoder; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.JSONValue; @@ -35,7 +33,7 @@ * 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 + * Uses the AJAX Interface V2 - see: http://docs.microsofttranslator.com/text-translate.html * * @author Jonathan Griggs */ @@ -43,17 +41,14 @@ public abstract class MicrosoftTranslatorAPI { //Encoding type protected static final String ENCODING = "UTF-8"; - protected static String apiKey; - private static String DatamarketAccessUri = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13"; + private static String TokenServiceUri = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken"; private static String referrer; - private static String clientId; - private static String clientSecret; + private static String subscriptionKey; private static String token; private static long tokenExpiration = 0; private static String contentType = "text/plain"; - protected static final String PARAM_APP_ID = "appId=", - PARAM_TO_LANG = "&to=", + protected static final String PARAM_TO_LANG = "&to=", PARAM_FROM_LANG = "&from=", PARAM_TEXT_SINGLE = "&text=", PARAM_TEXT_ARRAY = "&texts=", @@ -61,47 +56,24 @@ public abstract class MicrosoftTranslatorAPI { PARAM_SENTENCES_LANGUAGE = "&language=", PARAM_LOCALE = "&locale=", PARAM_LANGUAGE_CODES = "&languageCodes="; + private static final long TOKEN_DURATION = 10 * 60 * 1000; /** - * 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 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. + * Sets the Http Content Type. + * @param pContentType The HTTP content type. */ - public static void setContentType(final String pKey) { - contentType = pKey; + public static void setContentType(final String pContentType) { + contentType = pContentType; } /** - * 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. + * Sets the Subscription Key. + * All new applications should obtain a Subscription Key by following the + * guide at: http://docs.microsofttranslator.com/text-translate.html + * @param pSubscriptionKey The Subscription Key. */ - 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. - */ - public static void setClientSecret(final String pClientSecret) { - clientSecret = pClientSecret; + public static void setSubscriptionKey(final String pSubscriptionKey) { + subscriptionKey = pSubscriptionKey; } /** @@ -113,27 +85,19 @@ public static void setHttpReferrer(final String pReferrer) { } /** * Gets the OAuth access token. - * @param clientId The Client key. - * @param clientSecret The Client Secret + * @param subscriptionKey The Subscription 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) ; - - final URL url = new URL(DatamarketAccessUri); + public static String getToken(final String subscriptionKey) throws Exception { + final URL url = new URL(TokenServiceUri); 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.setRequestProperty("Ocp-Apim-Subscription-Key", subscriptionKey); uc.setRequestMethod("POST"); + uc.setFixedLengthStreamingMode(0); uc.setDoOutput(true); - OutputStreamWriter wr = new OutputStreamWriter(uc.getOutputStream()); - wr.write(params); - wr.flush(); - try { final int responseCode = uc.getResponseCode(); final String result = inputStreamToString(uc.getInputStream()); @@ -156,11 +120,9 @@ 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(subscriptionKey!=null&&System.currentTimeMillis()>tokenExpiration) { + token = "Bearer " + getToken(subscriptionKey); + tokenExpiration = System.currentTimeMillis()+TOKEN_DURATION-1; } final HttpURLConnection uc = (HttpURLConnection) url.openConnection(); if(referrer!=null) @@ -305,7 +267,10 @@ private static String inputStreamToString(final InputStream inputStream) throws while (null != (string = reader.readLine())) { // Need to strip the Unicode Zero-width Non-breaking Space. For some reason, the Microsoft AJAX // services prepend this to every response - outputBuilder.append(string.replaceAll("\uFEFF", "")); + string = string.replaceAll("\uFEFF", ""); + // Need to replace the Non-breaking Space into simple space + string = string.replace('\u00A0', ' '); + outputBuilder.append(string); } } } catch (Exception ex) { @@ -317,10 +282,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 (subscriptionKey==null) { + throw new RuntimeException("Must provide a Microsoft Translator Text Translation Subscription Key - Please see http://docs.microsofttranslator.com/text-translate.html for further documentation"); } } diff --git a/src/main/java/com/memetix/mst/detect/Detect.java b/src/main/java/com/memetix/mst/detect/Detect.java index 3fcfdc5..18dc8bf 100644 --- a/src/main/java/com/memetix/mst/detect/Detect.java +++ b/src/main/java/com/memetix/mst/detect/Detect.java @@ -49,7 +49,6 @@ 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) : "") +PARAM_TEXT_SINGLE+URLEncoder.encode(text, ENCODING)); final String response = retrieveString(url); @@ -68,7 +67,6 @@ public static String[] execute(final String[] texts) throws Exception { validateServiceState(texts); final String textArr = buildStringArrayParam(texts); final URL url = new URL(ARRAY_SERVICE_URL - +(apiKey != null ? PARAM_APP_ID + URLEncoder.encode(apiKey,ENCODING) : "") +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..29ef45c 100644 --- a/src/main/java/com/memetix/mst/language/Language.java +++ b/src/main/java/com/memetix/mst/language/Language.java @@ -110,15 +110,8 @@ public String toString() { return language; } - 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); + public static void setSubscriptionKey(String pSubscriptionKey) { + LanguageService.setSubscriptionKey(pSubscriptionKey); } /** @@ -221,7 +214,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 +237,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..42a73db 100644 --- a/src/main/java/com/memetix/mst/language/SpokenDialect.java +++ b/src/main/java/com/memetix/mst/language/SpokenDialect.java @@ -94,16 +94,8 @@ public String toString() { return language; } - 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); + public static void setSubscriptionKey(String pSubscriptionKey) { + SpokenDialectService.setSubscriptionKey(pSubscriptionKey); } /** @@ -172,7 +164,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..8a83e4a 100644 --- a/src/main/java/com/memetix/mst/translate/Translate.java +++ b/src/main/java/com/memetix/mst/translate/Translate.java @@ -52,8 +52,7 @@ 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); @@ -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); diff --git a/src/test/java/com/memetix/mst/detect/DetectTest.java b/src/test/java/com/memetix/mst/detect/DetectTest.java index d364da7..0486f15 100644 --- a/src/test/java/com/memetix/mst/detect/DetectTest.java +++ b/src/test/java/com/memetix/mst/detect/DetectTest.java @@ -43,21 +43,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"); - if(System.getProperty("test.api.key")!=null) { - apiKey = System.getProperty("test.api.key").split(",")[0]; + String subscriptionKey = p.getProperty("microsoft.translator.api.subscriptionKey"); + if(System.getProperty("test.subscription.key")!=null) { + subscriptionKey = System.getProperty("test.subscription.key"); } - 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); + Detect.setSubscriptionKey(subscriptionKey); } @After @@ -74,11 +64,6 @@ public void testDetectFrench() throws Exception { assertEquals(Language.FRENCH,Detect.execute("Salut tout le monde")); } - @Test - public void testDetectFrench_ClientIdOnly() throws Exception { - Language.setKey(null); - assertEquals(Language.FRENCH,Detect.execute("Salut tout le monde")); - } @Test public void testDetectKorean() throws Exception { assertEquals(Language.KOREAN,Detect.execute("전 세계 여러분 안녕하세요")); @@ -98,44 +83,22 @@ public void testDetectArraySingle() throws Exception { assertEquals(Language.ENGLISH.toString(),detections[0]); } - - @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"); - Detect.execute("전 세계 여러분 안녕하세요"); - } - @Test public void testDetect_NoKey() throws Exception { - Detect.setKey(null); - Detect.setClientId(null); + Detect.setSubscriptionKey(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 Microsoft Translator Text Translation Subscription Key - Please see http://docs.microsofttranslator.com/text-translate.html for further documentation"); Detect.execute("전 세계 여러분 안녕하세요"); } @Test public void testDetectArray_NoKey() throws Exception { - Detect.setKey(null); - Detect.setClientId(null); + Detect.setSubscriptionKey(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 Microsoft Translator Text Translation Subscription Key - Please see http://docs.microsofttranslator.com/text-translate.html 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"); - Detect.execute(texts); - } - @Test public void testDetectEnglish_Large() throws Exception { assertEquals(Language.ENGLISH,Detect.execute("Figures from the Office for National Statistics (ONS) show that between December and April, " diff --git a/src/test/java/com/memetix/mst/language/LanguageTest.java b/src/test/java/com/memetix/mst/language/LanguageTest.java index ee42e9b..f9f195e 100644 --- a/src/test/java/com/memetix/mst/language/LanguageTest.java +++ b/src/test/java/com/memetix/mst/language/LanguageTest.java @@ -47,21 +47,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"); - if(System.getProperty("test.api.key")!=null) { - apiKey = System.getProperty("test.api.key").split(",")[0]; + String subscriptionKey = p.getProperty("microsoft.translator.api.subscriptionKey"); + if(System.getProperty("test.subscription.key")!=null) { + subscriptionKey = System.getProperty("test.subscription.key"); } - 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); + Language.setSubscriptionKey(subscriptionKey); } @After @@ -92,35 +82,16 @@ public void testFromString() { assertEquals(expResult, result); } - @Test - public void testFromString_ClientIdOnly() { - String pLanguage = "en"; - Language.setKey(null); - Language expResult = Language.ENGLISH; - Language result = Language.fromString(pLanguage); - assertEquals(expResult, result); - } @Test public void testGetLanguage_NoKey() throws Exception { - Language.setKey(null); - Language.setClientId(null); + Language.setSubscriptionKey(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 Microsoft Translator Text Translation Subscription Key - Please see http://docs.microsofttranslator.com/text-translate.html for further documentation"); Language.FRENCH.getName(locale); } - @Test - public void testGetLanguage_WrongKey() throws Exception { - Language.setKey("wrong_key"); - Language locale = Language.PERSIAN; - - exception.expect(RuntimeException.class); - exception.expectMessage("INVALID_API_KEY - Please set the API Key with your Bing Developer's Key"); - Language.FRENCH.getName(locale); - } - /** * Test of toString method, of class Language. */ diff --git a/src/test/java/com/memetix/mst/language/SpokenDialectTest.java b/src/test/java/com/memetix/mst/language/SpokenDialectTest.java index ab57982..42b7a23 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"); - if(System.getProperty("test.api.key")!=null) { - apiKey = System.getProperty("test.api.key").split(",")[0]; + String subscriptionKey = p.getProperty("microsoft.translator.api.subscriptionKey"); + if(System.getProperty("test.subscription.key")!=null) { + subscriptionKey = System.getProperty("test.subscription.key"); } - 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); + SpokenDialect.setSubscriptionKey(subscriptionKey); } @After @@ -70,26 +60,14 @@ public void tearDown() throws Exception { @Test public void testGetSpokenDialect_NoKey() throws Exception { SpokenDialect.flushNameCache(); - SpokenDialect.setKey(null); - SpokenDialect.setClientId(null); + SpokenDialect.setSubscriptionKey(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 Microsoft Translator Text Translation Subscription Key - Please see http://docs.microsofttranslator.com/text-translate.html for further documentation"); SpokenDialect.FRENCH_CANADA.getName(locale); } - @Test - public void testGetSpokenDialect_WrongKey() throws Exception { - SpokenDialect.flushNameCache(); - SpokenDialect.setKey("wrong"); - Language locale = Language.ENGLISH; - - exception.expect(RuntimeException.class); - exception.expectMessage("INVALID_API_KEY - Please set the API Key with your Bing Developer's Key"); - SpokenDialect.FRENCH_CANADA.getName(locale); - } - /** * Test of valueOf method, of class Language. */ diff --git a/src/test/java/com/memetix/mst/sentence/BreakSentencesTest.java b/src/test/java/com/memetix/mst/sentence/BreakSentencesTest.java index 9616d27..2ceedb5 100644 --- a/src/test/java/com/memetix/mst/sentence/BreakSentencesTest.java +++ b/src/test/java/com/memetix/mst/sentence/BreakSentencesTest.java @@ -42,21 +42,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"); - if(System.getProperty("test.api.key")!=null) { - apiKey = System.getProperty("test.api.key").split(",")[0]; + String subscriptionKey = p.getProperty("microsoft.translator.api.subscriptionKey"); + if(System.getProperty("test.subscription.key")!=null) { + subscriptionKey = System.getProperty("test.subscription.key"); } - 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); + BreakSentences.setSubscriptionKey(subscriptionKey); } @After @@ -81,19 +71,11 @@ public void testBreakSentences_AutoDetect() throws Exception { @Test public void testBreakSentences_NoKey() throws Exception { - BreakSentences.setKey(null); - BreakSentences.setClientId(null); + BreakSentences.setSubscriptionKey(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 Microsoft Translator Text Translation Subscription Key - Please see http://docs.microsofttranslator.com/text-translate.html 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 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 { @@ -143,7 +125,9 @@ public void testBreakSentencesEnglish_Large() throws Exception { @Test public void testBreakSentencesEnglish_LargeNoKey() throws Exception { - BreakSentences.setKey(null); + BreakSentences.setSubscriptionKey(null); + exception.expect(RuntimeException.class); + exception.expectMessage("Must provide a Microsoft Translator Text Translation Subscription Key - Please see http://docs.microsofttranslator.com/text-translate.html for further documentation"); Integer[] results = BreakSentences.execute("Figures from the Office for National Statistics (ONS) show that between December and April, " + "the five-month period typically regarded as peak bonus season, those working in the financial " + "intermediation sector received bonuses worth ¬¨¬£7.6bn. The figure is more than 40pc lower than last" diff --git a/src/test/java/com/memetix/mst/speak/SpeakTest.java b/src/test/java/com/memetix/mst/speak/SpeakTest.java index e863459..f1d4834 100644 --- a/src/test/java/com/memetix/mst/speak/SpeakTest.java +++ b/src/test/java/com/memetix/mst/speak/SpeakTest.java @@ -44,21 +44,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"); - if(System.getProperty("test.api.key")!=null) { - apiKey = System.getProperty("test.api.key").split(",")[0]; + String subscriptionKey = p.getProperty("microsoft.translator.api.subscriptionKey"); + if(System.getProperty("test.subscription.key")!=null) { + subscriptionKey = System.getProperty("test.subscription.key"); } - 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); + Speak.setSubscriptionKey(subscriptionKey); } @After @@ -71,10 +61,9 @@ public void tearDown() throws Exception { */ @Test public void testGetSpeakUrl_NoKey() throws Exception { - Speak.setKey(null); - Speak.setClientId(null); + Speak.setSubscriptionKey(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 Microsoft Translator Text Translation Subscription Key - Please see http://docs.microsofttranslator.com/text-translate.html for further documentation"); String text = "Hello World!"; SpokenDialect language = SpokenDialect.ENGLISH_INDIA; Speak.execute(text, language); @@ -92,16 +81,6 @@ public void testGetSpeakUrl() throws Exception { assertEquals(true, result.contains(expResult)); } - @Test - public void testGetSpeakUrl_NoAppId() throws Exception { - SpokenDialect.setKey(null); - String text = "Hello World!"; - SpokenDialect language = SpokenDialect.ENGLISH_INDIA; - String expResult = "http://api.microsofttranslator.com/V2/http.svc/Speak"; - String result = Speak.execute(text, language); - assertEquals(true, result.contains(expResult)); - } - /** * Test of execute method, of class Speak. */ diff --git a/src/test/java/com/memetix/mst/translate/TranslateTest.java b/src/test/java/com/memetix/mst/translate/TranslateTest.java index a52ad5c..8ff2944 100644 --- a/src/test/java/com/memetix/mst/translate/TranslateTest.java +++ b/src/test/java/com/memetix/mst/translate/TranslateTest.java @@ -46,29 +46,17 @@ 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"); - if(System.getProperty("test.api.key")!=null) { - apiKey = System.getProperty("test.api.key").split(",")[0]; + String subscriptionKey = p.getProperty("microsoft.translator.api.subscriptionKey"); + if(System.getProperty("test.subscription.key")!=null) { + subscriptionKey = System.getProperty("test.subscription.key"); } - 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); + Translate.setSubscriptionKey(subscriptionKey); } @After public void tearDown() throws Exception { - Translate.setKey(null); Translate.setContentType("text/plain"); - Translate.setClientId(null); - Translate.setClientSecret(null); + Translate.setSubscriptionKey(null); Translate.setHttpReferrer(null); } @@ -83,9 +71,11 @@ public void testTranslate_SetReferrer() throws Exception { } @Test - public void testTranslate_NoApiKey() throws Exception { + public void testTranslate_NoSubscriptionKey() throws Exception { Translate.setHttpReferrer("http://localhost:8080"); - Translate.setKey(null); + Translate.setSubscriptionKey(null); + exception.expect(RuntimeException.class); + exception.expectMessage("Must provide a Microsoft Translator Text Translation Subscription Key - Please see http://docs.microsofttranslator.com/text-translate.html for further documentation"); assertEquals("Salut",Translate.execute("Hello", Language.ENGLISH, Language.FRENCH)); } @Test @@ -161,39 +151,12 @@ 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"); - Translate.execute("ハローワールド", Language.AUTO_DETECT, Language.ENGLISH); - } - @Test - public void testTranslate_WrongKey() throws Exception { - Translate.setKey("lessthan16"); - Translate.setClientId(null); - Translate.setClientSecret(null); + Translate.setSubscriptionKey(null); exception.expect(RuntimeException.class); - exception.expectMessage("INVALID_API_KEY - Please set the API Key with your Bing Developer's Key"); + exception.expectMessage("Must provide a Microsoft Translator Text Translation Subscription Key - Please see http://docs.microsofttranslator.com/text-translate.html for further documentation"); 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); - } - /* - @Test - public void testTranslate_Exception() throws Exception { - exception.expect(Exception.class); - //exception.expectMessage("INVALID_API_KEY - Please set the API Key with your Bing Developer's Key"); - String result = Translate.execute("\"test\" red", Language.AUTO_DETECT, Language.ENGLISH); - System.out.println(result); - } - */ @Test public void testLarge() throws Exception { String largeText = "Figures from the Office for National Statistics (ONS) show that between December and April, " diff --git a/src/test/resources/META-INF/README.md b/src/test/resources/META-INF/README.md index ff3bc59..4b1a4a4 100644 --- a/src/test/resources/META-INF/README.md +++ b/src/test/resources/META-INF/README.md @@ -1,11 +1,11 @@ # config.properties -This file contains any properties required for Unit Testing, chiefly, your Microsoft Translator API Key. +This file contains any properties required for Unit Testing, chiefly, your Microsoft Translator Subscription Key. - microsoft.translator.api.key=INSERT_API_KEY + microsoft.translator.subscription.key=INSERT_SUBSCRIPTION_KEY -This API Key is _only_ used for Unit Testing. For normal usage, the API Key is set in code at runtime. +This Subscription Key is _only_ used for Unit Testing. For normal usage, the Subscription Key is set in code at runtime. -If you've forked this branch and wish to run the Unit Tests, please replace the placeholder value with _your_ API Key. You should then be careful not to check this file back into your branch. Ignore the changes locally by issuing the following command +If you've forked this branch and wish to run the Unit Tests, please replace the placeholder value with _your_ Subscription Key. You should then be careful not to check this file back into your branch. Ignore the changes locally by issuing the following command git update-index --assume-unchanged /config.properties \ No newline at end of file diff --git a/src/test/resources/META-INF/config.properties b/src/test/resources/META-INF/config.properties index 35d2ac8..a20dbb0 100644 --- a/src/test/resources/META-INF/config.properties +++ b/src/test/resources/META-INF/config.properties @@ -1,2 +1 @@ -microsoft.translator.api.key=INSERT_API_KEY - \ No newline at end of file +microsoft.translator.api.subscriptionKey=INSERT_SUBSCRIPTION_KEY \ No newline at end of file