Skip to content

Commit aadd5fc

Browse files
committed
added try catch for loading config file in tests, if fail tell user to add credentials.
1 parent 09b1572 commit aadd5fc

15 files changed

+478
-382
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Use this SDK to build Watson-powered applications in Unity.
88
* [Getting the Watson SDK and adding it to Unity](#getting-the-watson-sdk-and-adding-it-to-unity)
99
* [Installing the SDK source into your Unity project](#installing-the-sdk-source-into-your-unity-project)
1010
* [Configuring your service credentials](#configuring-your-service-credentials)
11+
* [Authentication](#authentication)
1112
* [Watson Services](#watson-services)
1213
* [Authentication Tokens](#authentication-tokens)
1314
* [Documentation](#documentation)

Scripts/UnitTests/TestAlchemyLanguage.cs

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -109,33 +109,41 @@ public class TestAlchemyAPI : UnitTest
109109
public override IEnumerator RunTest()
110110
{
111111
LogSystem.InstallDefaultReactors();
112-
113-
VcapCredentials vcapCredentials = new VcapCredentials();
114-
fsData data = null;
115-
116-
// Get credentials from a credential file defined in environmental variables in the VCAP_SERVICES format.
117-
// See https://www.ibm.com/watson/developercloud/doc/common/getting-started-variables.html.
118-
var environmentalVariable = Environment.GetEnvironmentVariable("VCAP_SERVICES");
119-
var fileContent = File.ReadAllText(environmentalVariable);
120-
121-
// Add in a parent object because Unity does not like to deserialize root level collection types.
122-
fileContent = Utility.AddTopLevelObjectToJson(fileContent, "VCAP_SERVICES");
123-
124-
// Convert json to fsResult
125-
fsResult r = fsJsonParser.Parse(fileContent, out data);
126-
if (!r.Succeeded)
127-
throw new WatsonException(r.FormattedMessages);
128-
129-
// Convert fsResult to VcapCredentials
130-
object obj = vcapCredentials;
131-
r = _serializer.TryDeserialize(data, obj.GetType(), ref obj);
132-
if (!r.Succeeded)
133-
throw new WatsonException(r.FormattedMessages);
134-
135-
// Set credentials from imported credntials
136-
Credential credential = vcapCredentials.VCAP_SERVICES["alchemy_api"][TestCredentialIndex].Credentials;
137-
_apikey = credential.Apikey.ToString();
138-
_url = credential.Url.ToString();
112+
113+
try
114+
{
115+
VcapCredentials vcapCredentials = new VcapCredentials();
116+
fsData data = null;
117+
118+
// Get credentials from a credential file defined in environmental variables in the VCAP_SERVICES format.
119+
// See https://www.ibm.com/watson/developercloud/doc/common/getting-started-variables.html.
120+
var environmentalVariable = Environment.GetEnvironmentVariable("VCAP_SERVICES");
121+
122+
var fileContent = File.ReadAllText(environmentalVariable);
123+
124+
// Add in a parent object because Unity does not like to deserialize root level collection types.
125+
fileContent = Utility.AddTopLevelObjectToJson(fileContent, "VCAP_SERVICES");
126+
127+
// Convert json to fsResult
128+
fsResult r = fsJsonParser.Parse(fileContent, out data);
129+
if (!r.Succeeded)
130+
throw new WatsonException(r.FormattedMessages);
131+
132+
// Convert fsResult to VcapCredentials
133+
object obj = vcapCredentials;
134+
r = _serializer.TryDeserialize(data, obj.GetType(), ref obj);
135+
if (!r.Succeeded)
136+
throw new WatsonException(r.FormattedMessages);
137+
138+
// Set credentials from imported credntials
139+
Credential credential = vcapCredentials.VCAP_SERVICES["alchemy_api"][TestCredentialIndex].Credentials;
140+
_apikey = credential.Apikey.ToString();
141+
_url = credential.Url.ToString();
142+
}
143+
catch
144+
{
145+
Log.Debug("TestAlchemyLanguage", "Failed to get credentials from VCAP_SERVICES file. Please configure credentials to run this test. For more information, see: https://github.com/watson-developer-cloud/unity-sdk/#authentication");
146+
}
139147

140148
// Create credential and instantiate service
141149
Credentials credentials = new Credentials(_apikey, _url);

Scripts/UnitTests/TestConversation.cs

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -44,35 +44,41 @@ public class TestConversation : UnitTest
4444
public override IEnumerator RunTest()
4545
{
4646
LogSystem.InstallDefaultReactors();
47-
48-
VcapCredentials vcapCredentials = new VcapCredentials();
49-
fsData data = null;
50-
51-
// Get credentials from a credential file defined in environmental variables in the VCAP_SERVICES format.
52-
// See https://www.ibm.com/watson/developercloud/doc/common/getting-started-variables.html.
53-
var environmentalVariable = Environment.GetEnvironmentVariable("VCAP_SERVICES");
54-
var fileContent = File.ReadAllText(environmentalVariable);
55-
56-
// Add in a parent object because Unity does not like to deserialize root level collection types.
57-
fileContent = Utility.AddTopLevelObjectToJson(fileContent, "VCAP_SERVICES");
58-
59-
// Convert json to fsResult
60-
fsResult r = fsJsonParser.Parse(fileContent, out data);
61-
if (!r.Succeeded)
62-
throw new WatsonException(r.FormattedMessages);
63-
64-
// Convert fsResult to VcapCredentials
65-
object obj = vcapCredentials;
66-
r = _serializer.TryDeserialize(data, obj.GetType(), ref obj);
67-
if (!r.Succeeded)
68-
throw new WatsonException(r.FormattedMessages);
69-
70-
// Set credentials from imported credntials
71-
Credential credential = vcapCredentials.VCAP_SERVICES["conversation"][TestCredentialIndex].Credentials;
72-
_username = credential.Username.ToString();
73-
_password = credential.Password.ToString();
74-
_url = credential.Url.ToString();
75-
_workspaceId = credential.WorkspaceId.ToString();
47+
try
48+
{
49+
VcapCredentials vcapCredentials = new VcapCredentials();
50+
fsData data = null;
51+
52+
// Get credentials from a credential file defined in environmental variables in the VCAP_SERVICES format.
53+
// See https://www.ibm.com/watson/developercloud/doc/common/getting-started-variables.html.
54+
var environmentalVariable = Environment.GetEnvironmentVariable("VCAP_SERVICES");
55+
var fileContent = File.ReadAllText(environmentalVariable);
56+
57+
// Add in a parent object because Unity does not like to deserialize root level collection types.
58+
fileContent = Utility.AddTopLevelObjectToJson(fileContent, "VCAP_SERVICES");
59+
60+
// Convert json to fsResult
61+
fsResult r = fsJsonParser.Parse(fileContent, out data);
62+
if (!r.Succeeded)
63+
throw new WatsonException(r.FormattedMessages);
64+
65+
// Convert fsResult to VcapCredentials
66+
object obj = vcapCredentials;
67+
r = _serializer.TryDeserialize(data, obj.GetType(), ref obj);
68+
if (!r.Succeeded)
69+
throw new WatsonException(r.FormattedMessages);
70+
71+
// Set credentials from imported credntials
72+
Credential credential = vcapCredentials.VCAP_SERVICES["conversation"][TestCredentialIndex].Credentials;
73+
_username = credential.Username.ToString();
74+
_password = credential.Password.ToString();
75+
_url = credential.Url.ToString();
76+
_workspaceId = credential.WorkspaceId.ToString();
77+
}
78+
catch
79+
{
80+
Log.Debug("TestConversation", "Failed to get credentials from VCAP_SERVICES file. Please configure credentials to run this test. For more information, see: https://github.com/watson-developer-cloud/unity-sdk/#authentication");
81+
}
7682

7783
// Create credential and instantiate service
7884
Credentials credentials = new Credentials(_username, _password, _url);

Scripts/UnitTests/TestDiscovery.cs

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -74,33 +74,40 @@ public override IEnumerator RunTest()
7474
{
7575
LogSystem.InstallDefaultReactors();
7676

77-
VcapCredentials vcapCredentials = new VcapCredentials();
78-
fsData data = null;
79-
80-
// Get credentials from a credential file defined in environmental variables in the VCAP_SERVICES format.
81-
// See https://www.ibm.com/watson/developercloud/doc/common/getting-started-variables.html.
82-
var environmentalVariable = System.Environment.GetEnvironmentVariable("VCAP_SERVICES");
83-
var fileContent = File.ReadAllText(environmentalVariable);
84-
85-
// Add in a parent object because Unity does not like to deserialize root level collection types.
86-
fileContent = Utility.AddTopLevelObjectToJson(fileContent, "VCAP_SERVICES");
87-
88-
// Convert json to fsResult
89-
fsResult r = fsJsonParser.Parse(fileContent, out data);
90-
if (!r.Succeeded)
91-
throw new WatsonException(r.FormattedMessages);
92-
93-
// Convert fsResult to VcapCredentials
94-
object obj = vcapCredentials;
95-
r = _serializer.TryDeserialize(data, obj.GetType(), ref obj);
96-
if (!r.Succeeded)
97-
throw new WatsonException(r.FormattedMessages);
98-
99-
// Set credentials from imported credntials
100-
Credential credential = vcapCredentials.VCAP_SERVICES["discovery"][TestCredentialIndex].Credentials;
101-
_username = credential.Username.ToString();
102-
_password = credential.Password.ToString();
103-
_url = credential.Url.ToString();
77+
try
78+
{
79+
VcapCredentials vcapCredentials = new VcapCredentials();
80+
fsData data = null;
81+
82+
// Get credentials from a credential file defined in environmental variables in the VCAP_SERVICES format.
83+
// See https://www.ibm.com/watson/developercloud/doc/common/getting-started-variables.html.
84+
var environmentalVariable = System.Environment.GetEnvironmentVariable("VCAP_SERVICES");
85+
var fileContent = File.ReadAllText(environmentalVariable);
86+
87+
// Add in a parent object because Unity does not like to deserialize root level collection types.
88+
fileContent = Utility.AddTopLevelObjectToJson(fileContent, "VCAP_SERVICES");
89+
90+
// Convert json to fsResult
91+
fsResult r = fsJsonParser.Parse(fileContent, out data);
92+
if (!r.Succeeded)
93+
throw new WatsonException(r.FormattedMessages);
94+
95+
// Convert fsResult to VcapCredentials
96+
object obj = vcapCredentials;
97+
r = _serializer.TryDeserialize(data, obj.GetType(), ref obj);
98+
if (!r.Succeeded)
99+
throw new WatsonException(r.FormattedMessages);
100+
101+
// Set credentials from imported credntials
102+
Credential credential = vcapCredentials.VCAP_SERVICES["discovery"][TestCredentialIndex].Credentials;
103+
_username = credential.Username.ToString();
104+
_password = credential.Password.ToString();
105+
_url = credential.Url.ToString();
106+
}
107+
catch
108+
{
109+
Log.Debug("TestDiscovery", "Failed to get credentials from VCAP_SERVICES file. Please configure credentials to run this test. For more information, see: https://github.com/watson-developer-cloud/unity-sdk/#authentication");
110+
}
104111

105112
// Create credential and instantiate service
106113
Credentials credentials = new Credentials(_username, _password, _url);

Scripts/UnitTests/TestDocumentConversion.cs

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,33 +42,40 @@ public override IEnumerator RunTest()
4242
{
4343
LogSystem.InstallDefaultReactors();
4444

45-
VcapCredentials vcapCredentials = new VcapCredentials();
46-
fsData data = null;
47-
48-
// Get credentials from a credential file defined in environmental variables in the VCAP_SERVICES format.
49-
// See https://www.ibm.com/watson/developercloud/doc/common/getting-started-variables.html.
50-
var environmentalVariable = Environment.GetEnvironmentVariable("VCAP_SERVICES");
51-
var fileContent = File.ReadAllText(environmentalVariable);
52-
53-
// Add in a parent object because Unity does not like to deserialize root level collection types.
54-
fileContent = Utility.AddTopLevelObjectToJson(fileContent, "VCAP_SERVICES");
55-
56-
// Convert json to fsResult
57-
fsResult r = fsJsonParser.Parse(fileContent, out data);
58-
if (!r.Succeeded)
59-
throw new WatsonException(r.FormattedMessages);
60-
61-
// Convert fsResult to VcapCredentials
62-
object obj = vcapCredentials;
63-
r = _serializer.TryDeserialize(data, obj.GetType(), ref obj);
64-
if (!r.Succeeded)
65-
throw new WatsonException(r.FormattedMessages);
66-
67-
// Set credentials from imported credntials
68-
Credential credential = vcapCredentials.VCAP_SERVICES["document_conversion"][TestCredentialIndex].Credentials;
69-
_username = credential.Username.ToString();
70-
_password = credential.Password.ToString();
71-
_url = credential.Url.ToString();
45+
try
46+
{
47+
VcapCredentials vcapCredentials = new VcapCredentials();
48+
fsData data = null;
49+
50+
// Get credentials from a credential file defined in environmental variables in the VCAP_SERVICES format.
51+
// See https://www.ibm.com/watson/developercloud/doc/common/getting-started-variables.html.
52+
var environmentalVariable = Environment.GetEnvironmentVariable("VCAP_SERVICES");
53+
var fileContent = File.ReadAllText(environmentalVariable);
54+
55+
// Add in a parent object because Unity does not like to deserialize root level collection types.
56+
fileContent = Utility.AddTopLevelObjectToJson(fileContent, "VCAP_SERVICES");
57+
58+
// Convert json to fsResult
59+
fsResult r = fsJsonParser.Parse(fileContent, out data);
60+
if (!r.Succeeded)
61+
throw new WatsonException(r.FormattedMessages);
62+
63+
// Convert fsResult to VcapCredentials
64+
object obj = vcapCredentials;
65+
r = _serializer.TryDeserialize(data, obj.GetType(), ref obj);
66+
if (!r.Succeeded)
67+
throw new WatsonException(r.FormattedMessages);
68+
69+
// Set credentials from imported credntials
70+
Credential credential = vcapCredentials.VCAP_SERVICES["document_conversion"][TestCredentialIndex].Credentials;
71+
_username = credential.Username.ToString();
72+
_password = credential.Password.ToString();
73+
_url = credential.Url.ToString();
74+
}
75+
catch
76+
{
77+
Log.Debug("TestDocumentConversion", "Failed to get credentials from VCAP_SERVICES file. Please configure credentials to run this test. For more information, see: https://github.com/watson-developer-cloud/unity-sdk/#authentication");
78+
}
7279

7380
// Create credential and instantiate service
7481
Credentials credentials = new Credentials(_username, _password, _url);

Scripts/UnitTests/TestLanguageTranslator.cs

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -52,33 +52,40 @@ public override IEnumerator RunTest()
5252
{
5353
LogSystem.InstallDefaultReactors();
5454

55-
VcapCredentials vcapCredentials = new VcapCredentials();
56-
fsData data = null;
57-
58-
// Get credentials from a credential file defined in environmental variables in the VCAP_SERVICES format.
59-
// See https://www.ibm.com/watson/developercloud/doc/common/getting-started-variables.html.
60-
var environmentalVariable = Environment.GetEnvironmentVariable("VCAP_SERVICES");
61-
var fileContent = File.ReadAllText(environmentalVariable);
62-
63-
// Add in a parent object because Unity does not like to deserialize root level collection types.
64-
fileContent = Utility.AddTopLevelObjectToJson(fileContent, "VCAP_SERVICES");
65-
66-
// Convert json to fsResult
67-
fsResult r = fsJsonParser.Parse(fileContent, out data);
68-
if (!r.Succeeded)
69-
throw new WatsonException(r.FormattedMessages);
70-
71-
// Convert fsResult to VcapCredentials
72-
object obj = vcapCredentials;
73-
r = _serializer.TryDeserialize(data, obj.GetType(), ref obj);
74-
if (!r.Succeeded)
75-
throw new WatsonException(r.FormattedMessages);
76-
77-
// Set credentials from imported credntials
78-
Credential credential = vcapCredentials.VCAP_SERVICES["language_translator"][TestCredentialIndex].Credentials;
79-
_username = credential.Username.ToString();
80-
_password = credential.Password.ToString();
81-
_url = credential.Url.ToString();
55+
try
56+
{
57+
VcapCredentials vcapCredentials = new VcapCredentials();
58+
fsData data = null;
59+
60+
// Get credentials from a credential file defined in environmental variables in the VCAP_SERVICES format.
61+
// See https://www.ibm.com/watson/developercloud/doc/common/getting-started-variables.html.
62+
var environmentalVariable = Environment.GetEnvironmentVariable("VCAP_SERVICES");
63+
var fileContent = File.ReadAllText(environmentalVariable);
64+
65+
// Add in a parent object because Unity does not like to deserialize root level collection types.
66+
fileContent = Utility.AddTopLevelObjectToJson(fileContent, "VCAP_SERVICES");
67+
68+
// Convert json to fsResult
69+
fsResult r = fsJsonParser.Parse(fileContent, out data);
70+
if (!r.Succeeded)
71+
throw new WatsonException(r.FormattedMessages);
72+
73+
// Convert fsResult to VcapCredentials
74+
object obj = vcapCredentials;
75+
r = _serializer.TryDeserialize(data, obj.GetType(), ref obj);
76+
if (!r.Succeeded)
77+
throw new WatsonException(r.FormattedMessages);
78+
79+
// Set credentials from imported credntials
80+
Credential credential = vcapCredentials.VCAP_SERVICES["language_translator"][TestCredentialIndex].Credentials;
81+
_username = credential.Username.ToString();
82+
_password = credential.Password.ToString();
83+
_url = credential.Url.ToString();
84+
}
85+
catch
86+
{
87+
Log.Debug("TestLanguageTranslator", "Failed to get credentials from VCAP_SERVICES file. Please configure credentials to run this test. For more information, see: https://github.com/watson-developer-cloud/unity-sdk/#authentication");
88+
}
8289

8390
// Create credential and instantiate service
8491
Credentials credentials = new Credentials(_username, _password, _url);

0 commit comments

Comments
 (0)