Skip to content

Commit f90007c

Browse files
authored
Merge pull request #16 from watson-developer-cloud/fix-autoconfig
Fix autoconfig
2 parents c2113a8 + 182d5e6 commit f90007c

File tree

5 files changed

+228
-2
lines changed

5 files changed

+228
-2
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright © 2017 IBM Corp. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5+
* except in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the
10+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
11+
* either express or implied. See the License for the specific language governing permissions
12+
* and limitations under the License.
13+
*/
14+
15+
package com.ibm.watson.developer_cloud.spring.boot;
16+
17+
import org.springframework.boot.context.properties.ConfigurationProperties;
18+
19+
@ConfigurationProperties(prefix = WatsonAssistantV2ConfigurationProperties.PREFIX)
20+
public class WatsonAssistantV2ConfigurationProperties extends WatsonConfigurationProperties {
21+
22+
public static final String PREFIX = "watson.assistant-v2";
23+
}

src/main/java/com/ibm/watson/developer_cloud/spring/boot/WatsonAutoConfiguration.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838

3939
@Configuration
4040
@EnableConfigurationProperties({ WatsonAssistantConfigurationProperties.class,
41+
WatsonAssistantV2ConfigurationProperties.class,
4142
WatsonCompareComplyConfigurationProperties.class, WatsonDiscoveryConfigurationProperties.class,
43+
WatsonDiscoveryV2ConfigurationProperties.class,
4244
WatsonLanguageTranslatorConfigurationProperties.class, WatsonNaturalLanguageClassifierConfigurationProperties.class,
4345
WatsonNaturalLanguageUnderstandingConfigurationProperties.class,
4446
WatsonPersonalityInsightsConfigurationProperties.class, WatsonSpeechToTextConfigurationProperties.class,
@@ -73,7 +75,7 @@ private Authenticator configAuth(WatsonConfigurationProperties config, String se
7375
return ConfigBasedAuthenticatorFactory.getAuthenticator(serviceName);
7476
}
7577

76-
// Watson Assistant service
78+
// Watson Assistant service V1
7779

7880
@Autowired
7981
private WatsonAssistantConfigurationProperties assistantConfig;
@@ -88,6 +90,22 @@ public Assistant assistant() {
8890
return service;
8991
}
9092

93+
// Watson Assistant service V2
94+
95+
@Autowired
96+
private WatsonAssistantV2ConfigurationProperties assistantV2Config;
97+
98+
@Bean
99+
@ConditionalOnMissingBean
100+
@ConditionalOnWatsonServiceProperties(prefix = WatsonAssistantV2ConfigurationProperties.PREFIX)
101+
public com.ibm.watson.assistant.v2.Assistant assistantV2() {
102+
Authenticator authConfig = configAuth(assistantV2Config, "assistant_v2");
103+
com.ibm.watson.assistant.v2.Assistant service =
104+
new com.ibm.watson.assistant.v2.Assistant(assistantV2Config.getVersionDate(), authConfig);
105+
configUrl(service, assistantConfig);
106+
return service;
107+
}
108+
91109
// Watson Compare and Comply
92110

93111
@Autowired
@@ -103,7 +121,7 @@ public CompareComply compareComply() {
103121
return service;
104122
}
105123

106-
// Watson Discovery service
124+
// Watson Discovery service V1
107125

108126
@Autowired
109127
private WatsonDiscoveryConfigurationProperties discoveryConfig;
@@ -118,6 +136,22 @@ public Discovery discovery() {
118136
return service;
119137
}
120138

139+
// Watson Discovery service V2
140+
141+
@Autowired
142+
private WatsonDiscoveryV2ConfigurationProperties discoveryV2Config;
143+
144+
@Bean
145+
@ConditionalOnMissingBean
146+
@ConditionalOnWatsonServiceProperties(prefix = WatsonDiscoveryV2ConfigurationProperties.PREFIX)
147+
public com.ibm.watson.discovery.v2.Discovery discoveryV2() {
148+
Authenticator authConfig = configAuth(discoveryV2Config, "discovery_v2");
149+
com.ibm.watson.discovery.v2.Discovery service =
150+
new com.ibm.watson.discovery.v2.Discovery(discoveryV2Config.getVersionDate(), authConfig);
151+
configUrl(service, discoveryV2Config);
152+
return service;
153+
}
154+
121155
// Watson LanguageTranslator service
122156

123157
@Autowired
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright © 2017 IBM Corp. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5+
* except in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the
10+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
11+
* either express or implied. See the License for the specific language governing permissions
12+
* and limitations under the License.
13+
*/
14+
15+
package com.ibm.watson.developer_cloud.spring.boot;
16+
17+
import org.springframework.boot.context.properties.ConfigurationProperties;
18+
19+
@ConfigurationProperties(prefix = WatsonDiscoveryV2ConfigurationProperties.PREFIX)
20+
public class WatsonDiscoveryV2ConfigurationProperties extends WatsonConfigurationProperties {
21+
22+
public static final String PREFIX = "watson.discovery-v2";
23+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright © 2017 IBM Corp. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5+
* except in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the
10+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
11+
* either express or implied. See the License for the specific language governing permissions
12+
* and limitations under the License.
13+
*/
14+
15+
package com.ibm.watson.developer_cloud.spring.boot.test;
16+
17+
import com.ibm.cloud.sdk.core.security.Authenticator;
18+
import com.ibm.cloud.sdk.core.security.BasicAuthenticator;
19+
import com.ibm.watson.assistant.v2.Assistant;
20+
import com.ibm.watson.developer_cloud.spring.boot.WatsonAutoConfiguration;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.context.ApplicationContext;
25+
import org.springframework.test.context.ContextConfiguration;
26+
import org.springframework.test.context.TestPropertySource;
27+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
28+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
29+
30+
import java.lang.reflect.Field;
31+
32+
import static org.junit.Assert.assertEquals;
33+
import static org.junit.Assert.assertNotNull;
34+
35+
@RunWith(SpringJUnit4ClassRunner.class)
36+
@ContextConfiguration(classes = { WatsonAutoConfiguration.class }, loader = AnnotationConfigContextLoader.class)
37+
@TestPropertySource(properties = { "watson.assistant-v2.url=" + AssistantV2AutoConfigTest.url,
38+
"watson.assistant-v2.username=" + AssistantV2AutoConfigTest.username,
39+
"watson.assistant-v2.password=" + AssistantV2AutoConfigTest.password,
40+
"watson.assistant-v2.versionDate=" + AssistantV2AutoConfigTest.versionDate })
41+
public class AssistantV2AutoConfigTest {
42+
43+
static final String url = "https://api.us-south.assistant.watson.cloud.ibm.com";
44+
static final String username = "sam";
45+
static final String password = "secret";
46+
static final String versionDate = "2019-11-22";
47+
48+
@Autowired
49+
private ApplicationContext applicationContext;
50+
51+
@Test
52+
public void assistantBeanConfig() {
53+
Assistant assistant = (Assistant) applicationContext.getBean("assistantV2");
54+
55+
assertNotNull(assistant);
56+
assertEquals(url, assistant.getServiceUrl());
57+
58+
// Verify the credentials and versionDate -- the latter of which is stored in a private member variable
59+
try {
60+
assertEquals(Authenticator.AUTHTYPE_BASIC, assistant.getAuthenticator().authenticationType());
61+
BasicAuthenticator authenticator = (BasicAuthenticator) assistant.getAuthenticator();
62+
assertEquals(username, authenticator.getUsername());
63+
assertEquals(password, authenticator.getPassword());
64+
65+
Field versionField = Assistant.class.getDeclaredField("versionDate");
66+
versionField.setAccessible(true);
67+
assertEquals(versionDate, versionField.get(assistant));
68+
} catch (NoSuchFieldException | IllegalAccessException ex) {
69+
// This shouldn't happen
70+
assert false;
71+
}
72+
}
73+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright © 2017 IBM Corp. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5+
* except in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the
10+
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
11+
* either express or implied. See the License for the specific language governing permissions
12+
* and limitations under the License.
13+
*/
14+
15+
package com.ibm.watson.developer_cloud.spring.boot.test;
16+
17+
import com.ibm.cloud.sdk.core.security.Authenticator;
18+
import com.ibm.cloud.sdk.core.security.BasicAuthenticator;
19+
import com.ibm.watson.developer_cloud.spring.boot.WatsonAutoConfiguration;
20+
import com.ibm.watson.discovery.v2.Discovery;
21+
import org.junit.Test;
22+
import org.junit.runner.RunWith;
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.context.ApplicationContext;
25+
import org.springframework.test.context.ContextConfiguration;
26+
import org.springframework.test.context.TestPropertySource;
27+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
28+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
29+
30+
import java.lang.reflect.Field;
31+
32+
import static org.junit.Assert.assertEquals;
33+
import static org.junit.Assert.assertNotNull;
34+
35+
@RunWith(SpringJUnit4ClassRunner.class)
36+
@ContextConfiguration(classes = { WatsonAutoConfiguration.class }, loader = AnnotationConfigContextLoader.class)
37+
@TestPropertySource(properties = { "watson.discovery-v2.url=" + DiscoveryV2AutoConfigTest.url,
38+
"watson.discovery-v2.username=" + DiscoveryV2AutoConfigTest.username,
39+
"watson.discovery-v2.password=" + DiscoveryV2AutoConfigTest.password,
40+
"watson.discovery-v2.versionDate=" + DiscoveryV2AutoConfigTest.versionDate })
41+
public class DiscoveryV2AutoConfigTest {
42+
43+
static final String url = "https://api.us-south.discovery.watson.cloud.ibm.com";
44+
static final String username = "sam";
45+
static final String password = "secret";
46+
static final String versionDate = "2019-11-22";
47+
48+
@Autowired
49+
private ApplicationContext applicationContext;
50+
51+
@Test
52+
public void discoveryBeanConfig() {
53+
Discovery discovery = (Discovery) applicationContext.getBean("discoveryV2");
54+
55+
assertNotNull(discovery);
56+
assertEquals(url, discovery.getServiceUrl());
57+
58+
// Verify the credentials and versionDate -- the latter of which is stored in a private member variable
59+
try {
60+
assertEquals(Authenticator.AUTHTYPE_BASIC, discovery.getAuthenticator().authenticationType());
61+
BasicAuthenticator authenticator = (BasicAuthenticator) discovery.getAuthenticator();
62+
assertEquals(username, authenticator.getUsername());
63+
assertEquals(password, authenticator.getPassword());
64+
65+
Field versionField = Discovery.class.getDeclaredField("versionDate");
66+
versionField.setAccessible(true);
67+
assertEquals(versionDate, versionField.get(discovery));
68+
} catch (NoSuchFieldException | IllegalAccessException ex) {
69+
// This shouldn't happen
70+
assert false;
71+
}
72+
}
73+
}

0 commit comments

Comments
 (0)