Skip to content

Commit e21e384

Browse files
committed
Restructuring tests for Gradle
1 parent 38e08e6 commit e21e384

333 files changed

Lines changed: 47139 additions & 204 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎alchemy/build.gradle‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
plugins {
2+
id 'ru.vyarus.animalsniffer' version '1.2.0'
3+
id 'net.saliman.cobertura' version '2.3.2'
4+
}
5+
6+
defaultTasks 'clean'
7+
8+
apply from: '../utils.gradle'
9+
import org.apache.tools.ant.filters.*
10+
11+
apply plugin: 'java'
12+
apply plugin: 'ru.vyarus.animalsniffer'
13+
apply plugin: 'net.saliman.cobertura'
14+
apply plugin: 'maven'
15+
apply plugin: 'checkstyle'
16+
17+
version = '3.5.2-SNAPSHOT'
18+
group = 'com.ibm.watson.developercloud'
19+
20+
task sourcesJar(type: Jar) {
21+
classifier = 'sources'
22+
from sourceSets.main.allSource
23+
}
24+
25+
task javadocJar(type: Jar, dependsOn: javadoc) {
26+
classifier = 'javadoc'
27+
from javadoc.destinationDir
28+
}
29+
30+
repositories {
31+
mavenCentral()
32+
}
33+
34+
artifacts {
35+
archives sourcesJar
36+
archives javadocJar
37+
}
38+
39+
checkstyle {
40+
configFile = rootProject.file('checkstyle.xml')
41+
}
42+
43+
dependencies {
44+
compile project(':core')
45+
46+
testCompile project(':core')
47+
// testCompile project(path: ':core', configuration: 'tests')
48+
49+
testCompile group: 'com.squareup.okhttp3', name: 'mockwebserver', version: '3.3.1'
50+
testCompile group: 'ch.qos.logback', name: 'logback-classic', version: '1.1.7'
51+
testCompile group: 'com.google.guava', name: 'guava', version: '19.0'
52+
testCompile group: 'junit', name: 'junit-dep', version: '4.11'
53+
signature 'org.codehaus.mojo.signature:java17:1.0@signature'
54+
}
55+
56+
processResources {
57+
filter ReplaceTokens, tokens: [
58+
"pom.version": project.version,
59+
"build.date" : getDate()
60+
]
61+
}
62+
63+
uploadArchives {
64+
repositories.mavenDeployer {
65+
repository(url: "http://oss.sonatype.org/service/local/staging/deploy/maven2/") {
66+
authentication(userName: System.env.CI_DEPLOY_USERNAME, password: System.env.CI_DEPLOY_PASSWORD)
67+
}
68+
snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/"){
69+
authentication(userName: System.env.CI_DEPLOY_USERNAME, password: System.env.CI_DEPLOY_PASSWORD)
70+
}
71+
}
72+
}
73+
74+
cobertura {
75+
coverageFormats = ['html', 'xml']
76+
coverageIgnoreTrivial = true
77+
coverageIgnores = ['org.slf4j.Logger.*']
78+
coverageReportDir = new File("$buildDir/reports/cobertura")
79+
}
Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
/**
2+
* Copyright 2015 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* 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 License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package com.ibm.watson.developer_cloud;
14+
15+
import static org.junit.Assert.fail;
16+
17+
import java.io.BufferedReader;
18+
import java.io.File;
19+
import java.io.FileInputStream;
20+
import java.io.FileNotFoundException;
21+
import java.io.FileOutputStream;
22+
import java.io.IOException;
23+
import java.io.InputStream;
24+
import java.io.InputStreamReader;
25+
import java.io.OutputStream;
26+
import java.util.HashMap;
27+
import java.util.Map;
28+
import java.util.Properties;
29+
import java.util.logging.Level;
30+
import java.util.logging.LogManager;
31+
import java.util.logging.Logger;
32+
33+
import org.slf4j.LoggerFactory;
34+
35+
import com.ibm.watson.developer_cloud.http.HttpHeaders;
36+
import com.ibm.watson.developer_cloud.util.GsonSingleton;
37+
38+
/**
39+
* Utility class to test the Watson Services.
40+
*/
41+
public abstract class WatsonServiceTest {
42+
43+
private static final String DEFAULT_PROPERTIES = "config.properties";
44+
private static final String LOCAL_PROPERTIES = ".config.properties";
45+
46+
/** The Constant PLACEHOLDER. */
47+
protected static final String PLACEHOLDER = "SERVICE_USERNAME";
48+
49+
private static final Logger LOG = Logger.getLogger(WatsonServiceTest.class.getName());
50+
51+
/** The Constant CONTENT_TYPE. */
52+
protected static final String CONTENT_TYPE = "Content-Type";
53+
54+
/**
55+
* Instantiates a new watson service test.
56+
*/
57+
public WatsonServiceTest() {
58+
if (properties == null) {
59+
loadProperties();
60+
}
61+
setupLogging();
62+
}
63+
64+
/**
65+
* Gets the default test headers.
66+
*
67+
* @return the default headers
68+
*/
69+
protected Map<String, String> getDefaultHeaders() {
70+
Map<String, String> headers = new HashMap<String, String>();
71+
headers.put(HttpHeaders.X_WATSON_LEARNING_OPT_OUT, String.valueOf(true));
72+
headers.put(HttpHeaders.X_WATSON_TEST, String.valueOf(true));
73+
return headers;
74+
}
75+
76+
/**
77+
* Gets the string from input stream.
78+
*
79+
* @param is the input stream
80+
* @return the string from input stream
81+
*/
82+
public static String getStringFromInputStream(InputStream is) {
83+
BufferedReader br = null;
84+
final StringBuilder sb = new StringBuilder();
85+
86+
String line;
87+
try {
88+
89+
br = new BufferedReader(new InputStreamReader(is));
90+
while ((line = br.readLine()) != null) {
91+
sb.append(line);
92+
}
93+
94+
} catch (final IOException e) {
95+
e.printStackTrace();
96+
} finally {
97+
if (br != null) {
98+
try {
99+
br.close();
100+
} catch (final IOException e) {
101+
e.printStackTrace();
102+
}
103+
}
104+
}
105+
106+
return sb.toString();
107+
108+
}
109+
110+
/** The prop. */
111+
protected static Properties properties = null;
112+
113+
/**
114+
* Gets the valid property.
115+
*
116+
* @param property the property
117+
* @return the valid property
118+
*/
119+
public String getProperty(String property) {
120+
return properties.getProperty(property);
121+
}
122+
123+
private void loadProperties() {
124+
properties = new Properties();
125+
126+
InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream(LOCAL_PROPERTIES);
127+
if (input == null) {
128+
input = Thread.currentThread().getContextClassLoader().getResourceAsStream(DEFAULT_PROPERTIES);
129+
} else {
130+
LOG.info("Using " + LOCAL_PROPERTIES);
131+
}
132+
if (input == null) {
133+
LOG.warning(DEFAULT_PROPERTIES + " was not found.");
134+
return;
135+
}
136+
// load a properties file from class path, inside static method
137+
try {
138+
properties.load(input);
139+
} catch (IOException e) {
140+
LOG.log(Level.SEVERE, "Error loading the config.properties");
141+
}
142+
143+
}
144+
145+
/**
146+
* Setup logging.
147+
*/
148+
private void setupLogging() {
149+
ch.qos.logback.classic.Logger root =
150+
(ch.qos.logback.classic.Logger) LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
151+
root.setLevel(ch.qos.logback.classic.Level.OFF);
152+
try {
153+
FileInputStream configFile = new FileInputStream("src/test/resources/logging.properties");
154+
LogManager.getLogManager().readConfiguration(configFile);
155+
} catch (IOException ex) {
156+
System.out.println("WARNING: Could not open configuration file");
157+
System.out.println("WARNING: Logging not configured (console output only)");
158+
}
159+
}
160+
161+
/**
162+
* Write input stream to file.
163+
*
164+
* @param inputStream the input stream
165+
* @param file the file
166+
*/
167+
public static void writeInputStreamToFile(InputStream inputStream, File file) {
168+
OutputStream outStream = null;
169+
try {
170+
outStream = new FileOutputStream(file);
171+
172+
byte[] buffer = new byte[8 * 1024];
173+
int bytesRead;
174+
while ((bytesRead = inputStream.read(buffer)) != -1) {
175+
outStream.write(buffer, 0, bytesRead);
176+
}
177+
} catch (Exception e) {
178+
fail();
179+
} finally {
180+
try {
181+
inputStream.close();
182+
outStream.close();
183+
} catch (Exception e) {
184+
fail();
185+
}
186+
}
187+
}
188+
189+
/**
190+
* Loads fixture.
191+
*
192+
* @param <T> the return type
193+
* @param filename the file name
194+
* @param returnType the return type
195+
* @return the t
196+
* @throws FileNotFoundException the file not found exception
197+
*/
198+
public static <T> T loadFixture(String filename, Class<T> returnType) throws FileNotFoundException {
199+
String jsonString = getStringFromInputStream(new FileInputStream(filename));
200+
return GsonSingleton.getGsonWithoutPrettyPrinting().fromJson(jsonString, returnType);
201+
}
202+
203+
/**
204+
* Sets the up.
205+
*
206+
* @throws Exception the exception
207+
*/
208+
public void setUp() throws Exception { }
209+
210+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/**
2+
* Copyright 2015 IBM Corp. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5+
* 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 License is distributed on
10+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11+
* specific language governing permissions and limitations under the License.
12+
*/
13+
package com.ibm.watson.developer_cloud;
14+
15+
import java.io.IOException;
16+
17+
import org.apache.commons.lang3.StringUtils;
18+
import org.junit.After;
19+
20+
import com.google.gson.Gson;
21+
import com.ibm.watson.developer_cloud.http.HttpMediaType;
22+
import com.ibm.watson.developer_cloud.util.GsonSingleton;
23+
24+
import okhttp3.mockwebserver.MockResponse;
25+
import okhttp3.mockwebserver.MockWebServer;
26+
27+
/**
28+
* Utility class to Mock the Watson Services.
29+
*
30+
*/
31+
public abstract class WatsonServiceUnitTest extends WatsonServiceTest {
32+
33+
/** The Constant DELETE. */
34+
protected static final String DELETE = "DELETE";
35+
36+
/** The Constant GET. */
37+
protected static final String GET = "GET";
38+
39+
/** The Constant POST. */
40+
protected static final String POST = "POST";
41+
42+
/** The Constant PUT. */
43+
protected static final String PUT = "PUT";
44+
45+
private static final Gson GSON = GsonSingleton.getGson();
46+
47+
/** The server. */
48+
protected MockWebServer server;
49+
50+
/**
51+
* Setups and starts the mock server.
52+
*
53+
* @throws Exception the exception
54+
*/
55+
@Override
56+
public void setUp() throws Exception {
57+
server = new MockWebServer();
58+
server.start();
59+
}
60+
61+
/**
62+
* Tear down.
63+
*
64+
* @throws IOException Signals that an I/O exception has occurred.
65+
*/
66+
@After
67+
public void tearDown() throws IOException {
68+
server.shutdown();
69+
}
70+
71+
/**
72+
* Gets the mock web server url.
73+
*
74+
* @return the server url
75+
*/
76+
protected String getMockWebServerUrl() {
77+
return StringUtils.chop(server.url("/").toString());
78+
}
79+
80+
/**
81+
* Create a MockResponse with JSON content type and the object serialized to JSON as body.
82+
*
83+
* @param body the body
84+
* @return the mock response
85+
*/
86+
protected static MockResponse jsonResponse(Object body) {
87+
return new MockResponse().addHeader(CONTENT_TYPE, HttpMediaType.APPLICATION_JSON).setBody(GSON.toJson(body));
88+
}
89+
90+
}

0 commit comments

Comments
 (0)