Skip to content

Commit 05fca92

Browse files
committed
test: Add Language Translator V3 test using "apikey":<apikey> for authentication
1 parent a7c3914 commit 05fca92

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/**
2+
* Copyright 2017 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
using IBM.WatsonDeveloperCloud.LanguageTranslator.v3.Model;
19+
using IBM.WatsonDeveloperCloud.Util;
20+
using Microsoft.VisualStudio.TestTools.UnitTesting;
21+
using Newtonsoft.Json;
22+
using Newtonsoft.Json.Linq;
23+
using System;
24+
using System.Collections.Generic;
25+
using System.IO;
26+
using System.Threading.Tasks;
27+
28+
namespace IBM.WatsonDeveloperCloud.LanguageTranslator.v3.IntegrationTests
29+
{
30+
[TestClass]
31+
public class LTServiceIntTestBasicAuthIamApikey
32+
{
33+
private static string _username;
34+
private static string _password;
35+
private static string _endpoint;
36+
private static LanguageTranslatorService _service;
37+
private static string credentials = string.Empty;
38+
39+
private static string _baseModel = "en-fr";
40+
private static string _text = "I'm sorry, Dave. I'm afraid I can't do that.";
41+
private string _versionDate = "2018-05-01";
42+
43+
[TestInitialize]
44+
public void Setup()
45+
{
46+
#region Get Credentials
47+
if (string.IsNullOrEmpty(credentials))
48+
{
49+
var parentDirectory = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.Parent.Parent.FullName;
50+
string credentialsFilepath = parentDirectory + Path.DirectorySeparatorChar + "sdk-credentials" + Path.DirectorySeparatorChar + "credentials.json";
51+
if (File.Exists(credentialsFilepath))
52+
{
53+
try
54+
{
55+
credentials = File.ReadAllText(credentialsFilepath);
56+
credentials = Utility.AddTopLevelObjectToJson(credentials, "VCAP_SERVICES");
57+
}
58+
catch (Exception e)
59+
{
60+
throw new Exception(string.Format("Failed to load credentials: {0}", e.Message));
61+
}
62+
}
63+
else
64+
{
65+
Console.WriteLine("Credentials file does not exist.");
66+
}
67+
68+
VcapCredentials vcapCredentials = JsonConvert.DeserializeObject<VcapCredentials>(credentials);
69+
var vcapServices = JObject.Parse(credentials);
70+
71+
Credential credential = vcapCredentials.GetCredentialByname("language-translator-v3-sdk-rc-wdc")[0].Credentials;
72+
_endpoint = credential.Url;
73+
_username = "apikey";
74+
_password = credential.IamApikey;
75+
}
76+
#endregion
77+
78+
_service = new LanguageTranslatorService(_username, _password, _versionDate);
79+
_service.SetEndpoint(_endpoint);
80+
}
81+
82+
[TestMethod]
83+
public void GetIdentifiableLanguages_Sucess_IamAsBasicAuth()
84+
{
85+
var results = _service.ListIdentifiableLanguages();
86+
87+
Assert.IsNotNull(results);
88+
Assert.IsTrue(results.Languages.Count > 0);
89+
}
90+
91+
[TestMethod]
92+
public void Identify_Sucess_IamAsBasicAuth()
93+
{
94+
var results = _service.Identify(_text);
95+
96+
Assert.IsNotNull(results);
97+
Assert.IsTrue(results.Languages.Count > 0);
98+
}
99+
100+
[TestMethod]
101+
public void Translate_Sucess_IamAsBasicAuth()
102+
{
103+
var translateRequest = new TranslateRequest()
104+
{
105+
Text = new List<string>()
106+
{
107+
_text
108+
},
109+
ModelId = _baseModel
110+
};
111+
112+
var results = _service.Translate(translateRequest);
113+
114+
Assert.IsNotNull(results);
115+
Assert.IsTrue(results.Translations.Count > 0);
116+
}
117+
118+
[TestMethod]
119+
public void ListModels_Sucess_IamAsBasicAuth()
120+
{
121+
var results = _service.ListModels();
122+
123+
Assert.IsNotNull(results);
124+
Assert.IsTrue(results.Models.Count > 0);
125+
}
126+
}
127+
}

0 commit comments

Comments
 (0)