Skip to content

Commit f300a95

Browse files
author
Mike Kistler
committed
Fix warnings in build.gradle and add test for credentials in VCAP_SERVICES
1 parent 13d2d94 commit f300a95

File tree

2 files changed

+89
-45
lines changed

2 files changed

+89
-45
lines changed

build.gradle

Lines changed: 7 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ buildscript {
2222
}
2323

2424
plugins {
25-
id 'net.researchgate.release' version '2.4.0'
25+
id 'net.researchgate.release' version '2.6.0'
2626
}
2727

2828
apply plugin: 'java'
@@ -33,8 +33,6 @@ apply plugin: 'idea'
3333
apply plugin: 'propdeps'
3434
apply plugin: 'propdeps-maven'
3535

36-
import org.apache.tools.ant.filters.*
37-
3836
archivesBaseName = 'spring-boot-starter'
3937

4038
description = 'Spring Boot Starter for the IBM Watson Services'
@@ -45,10 +43,6 @@ version = new File(rootDir, 'VERSION').text.trim()
4543
// If the version says "snapshot" anywhere assume it is not a release
4644
ext.isReleaseVersion = !version.toUpperCase(Locale.ENGLISH).contains("SNAPSHOT")
4745

48-
javadoc {
49-
source = 'src/main/java'
50-
}
51-
5246
checkstyle {
5347
configFile = rootProject.file('checkstyle.xml')
5448
ignoreFailures = false
@@ -62,10 +56,6 @@ checkstyleTest {
6256
ignoreFailures = false
6357
}
6458

65-
task docs(type: Javadoc) {
66-
destinationDir = file("$buildDir/docs/all")
67-
}
68-
6959
task copyJars(type: Copy) {
7060
from subprojects.collect { it.tasks.withType(Jar) }
7161
into "$buildDir/allJars"
@@ -101,6 +91,7 @@ dependencies {
10191

10292
// testing
10393
testCompile group: 'junit', name: 'junit', version: '4.12'
94+
testCompile 'com.github.stefanbirkner:system-rules:1.16.0'
10495
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: springBootVersion
10596
}
10697

@@ -113,49 +104,20 @@ test {
113104
}
114105
}
115106

116-
afterEvaluate {
117-
if (plugins.hasPlugin(JavaPlugin)) {
118-
rootProject.tasks.docs {
119-
source += files(sourceSets.main.allJava)
120-
classpath += files(sourceSets*.compileClasspath)
121-
}
122-
}
123-
}
124-
125107
apply plugin: 'jacoco'
126108

127-
task codeCoverageReport(type: JacocoReport) {
128-
// Gather execution data from all sub projects
129-
// (change this if you e.g. want to calculate unit test/integration test coverage separately)
109+
task codeCoverageReport(type: JacocoReport, dependsOn: test) {
130110
executionData fileTree(project.rootDir.absolutePath).include("**/build/jacoco/*.exec")
131-
132-
// Add all relevant source sets from the sub projects
133-
subprojects.each {
134-
sourceSets it.sourceSets.main
135-
}
111+
sourceDirectories = files(sourceSets.main.allSource.srcDirs)
112+
classDirectories = files(sourceSets.main.output)
136113

137114
reports {
138-
xml.enabled true
139-
xml.destination file("${buildDir}/reports/jacoco/report.xml")
140-
html.enabled true
141-
html.destination file("${buildDir}/reports/jacoco")
142115
csv.enabled false
116+
html.destination file("${buildDir}/reports/jacoco")
117+
xml.destination file("${buildDir}/reports/jacoco/report.xml")
143118
}
144119
}
145120

146-
// always run the tests before generating the report
147-
codeCoverageReport.dependsOn {
148-
subprojects*.test
149-
testReport
150-
}
151-
152-
task testReport(type: TestReport) {
153-
destinationDir = file("$buildDir/reports/allTests")
154-
155-
// Include the results from the `test` task in all subprojects
156-
reportOn subprojects*.test
157-
}
158-
159121
release {
160122
tagTemplate = 'watson-spring-boot-$version'
161123
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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.watson.developer_cloud.language_translator.v2.LanguageTranslator;
18+
import com.ibm.watson.developer_cloud.service.WatsonService;
19+
import com.ibm.watson.developer_cloud.spring.boot.WatsonAutoConfiguration;
20+
import okhttp3.Credentials;
21+
import org.junit.ClassRule;
22+
import org.junit.Test;
23+
import org.junit.contrib.java.lang.system.EnvironmentVariables;
24+
import org.junit.runner.RunWith;
25+
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.context.ApplicationContext;
27+
import org.springframework.test.context.ContextConfiguration;
28+
import org.springframework.test.context.TestPropertySource;
29+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
30+
import org.springframework.test.context.support.AnnotationConfigContextLoader;
31+
32+
import java.lang.reflect.Field;
33+
34+
import static org.junit.Assert.assertEquals;
35+
import static org.junit.Assert.assertNotNull;
36+
37+
@RunWith(SpringJUnit4ClassRunner.class)
38+
@ContextConfiguration(classes = {WatsonAutoConfiguration.class}, loader = AnnotationConfigContextLoader.class)
39+
@TestPropertySource(properties = {"watson.language_translator.enabled=true"})
40+
public class WatsonAutoConfigTest {
41+
42+
private static final String url = "http://watson.com/language_translator";
43+
private static final String username = "sam";
44+
private static final String password = "secret";
45+
46+
@ClassRule
47+
public static final EnvironmentVariables environmentVariables = new EnvironmentVariables();
48+
49+
static {
50+
String vcapServices = "{\"language_translator\":[{"
51+
+ "\"credentials\": {"
52+
+ "\"url\":\"" + url + "\","
53+
+ "\"username\":\"" + username + "\","
54+
+ "\"password\":\"" + password + "\""
55+
+ "},"
56+
+ "\"plan\": \"free\""
57+
+ "}]}";
58+
environmentVariables.set("VCAP_SERVICES", vcapServices);
59+
}
60+
61+
@Autowired
62+
private ApplicationContext applicationContext;
63+
64+
@Test
65+
public void watsonBeanConfigFromEnvironment() {
66+
LanguageTranslator languageTranslator = (LanguageTranslator) applicationContext.getBean("languageTranslator");
67+
68+
assertNotNull(languageTranslator);
69+
// URL is not set from VCAP_SERVICES
70+
//assertEquals(url, languageTranslator.getEndPoint());
71+
72+
// Verify the credentials -- which are stored in a private member variable
73+
try {
74+
Field apiKeyField = WatsonService.class.getDeclaredField("apiKey");
75+
apiKeyField.setAccessible(true);
76+
assertEquals(Credentials.basic(username, password), apiKeyField.get(languageTranslator));
77+
} catch (NoSuchFieldException | IllegalAccessException ex) {
78+
// This shouldn't happen
79+
assert false;
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)