Skip to content

Commit 21845e7

Browse files
committedJul 22, 2016
cleaned up the pom.xml, updated parametermap impl and unit tests to be deterministic, updated pom.xml to prepare for release to maven central
1 parent f5e7563 commit 21845e7

10 files changed

+358
-197
lines changed
 

‎.gitignore

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
out
22
*.iml
33
.idea
4-
5-
/target/
4+
.classpath
5+
.project
6+
.settings
7+
/target/

‎pom.xml

+180-108
Original file line numberDiff line numberDiff line change
@@ -1,117 +1,189 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
2-
<repositories>
3-
</repositories>
4-
<modelVersion>4.0.0</modelVersion>
5-
<groupId>net.threescale</groupId>
6-
<artifactId>threescale-api</artifactId>
7-
<name>3scale Java API</name>
8-
<url>http://http://www.3scale.net/</url>
9-
<version>3.1.0</version>
10-
<description>This API is used to encapsulate the communication with the 3scale backend server for validating and
11-
reporting transactions between the user and the provider of a service
12-
</description>
13-
<packaging>jar</packaging>
14-
<dependencies>
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>net.3scale</groupId>
5+
<artifactId>3scale-api</artifactId>
6+
<version>3.1.0-SNAPSHOT</version>
157

16-
<dependency>
17-
<groupId>joda-time</groupId>
18-
<artifactId>joda-time</artifactId>
19-
<version>2.3</version>
20-
</dependency>
8+
<name>3scale Java API</name>
9+
<description>
10+
This API is used to encapsulate the communication with the 3scale backend server for validating and
11+
reporting transactions between the user and the provider of a service
12+
</description>
13+
<url>https://www.3scale.net/</url>
2114

22-
<dependency>
23-
<groupId>xom</groupId>
24-
<artifactId>xom</artifactId>
25-
<version>1.2.5</version>
26-
</dependency>
27-
<!-- Test dep's -->
28-
<dependency>
29-
<groupId>org.jmock</groupId>
30-
<artifactId>jmock-junit4</artifactId>
31-
<version>2.6.0</version>
32-
<scope>test</scope>
33-
</dependency>
15+
<licenses>
16+
<license>
17+
<name>MIT</name>
18+
<url>http://creativecommons.org/licenses/MIT/</url>
19+
</license>
20+
</licenses>
3421

35-
<dependency>
36-
<groupId>org.hamcrest</groupId>
37-
<artifactId>hamcrest-core</artifactId>
38-
<version>1.2</version>
39-
</dependency>
22+
<developers>
23+
<developer>
24+
<name>Andrew Mackenzie</name>
25+
<email>andrew@3scale.net</email>
26+
<organization>3scale</organization>
27+
<organizationUrl>http://www.3scale.net</organizationUrl>
28+
</developer>
29+
</developers>
4030

41-
<dependency>
42-
<groupId>junit</groupId>
43-
<artifactId>junit</artifactId>
44-
<version>4.10</version>
45-
</dependency>
31+
<issueManagement>
32+
<system>GitHub</system>
33+
<url>https://github.com/3scale/3scale_ws_api_for_java/issues</url>
34+
</issueManagement>
4635

36+
<scm>
37+
<url>https://github.com/3scale/3scale_ws_api_for_java</url>
38+
<connection>scm:git:git@github.com:3scale/3scale_ws_api_for_java.git</connection>
39+
<developerConnection>scm:git:git@github.com:3scale/3scale_ws_api_for_java.git</developerConnection>
40+
</scm>
4741

48-
</dependencies>
42+
<properties>
43+
<maven.compile.source>1.6</maven.compile.source>
44+
<maven.compile.target>1.6</maven.compile.target>
45+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
46+
</properties>
4947

50-
<properties>
51-
<maven.compile.source>1.6</maven.compile.source>
52-
<maven.compile.target>1.6</maven.compile.target>
53-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
54-
</properties>
55-
<licenses>
56-
<license>
57-
<name>MIT</name>
58-
<url>http://creativecommons.org/licenses/MIT/</url>
59-
</license>
60-
</licenses>
61-
<issueManagement>
62-
<system>GitHub</system>
63-
<url>https://github.com/3scale/3scale_ws_api_for_java/issues</url>
64-
</issueManagement>
65-
<build>
66-
<plugins>
67-
<plugin>
68-
<groupId>org.apache.maven.plugins</groupId>
69-
<artifactId>maven-compiler-plugin</artifactId>
70-
<version>3.1</version>
71-
<configuration>
72-
<source>1.6</source>
73-
<target>1.6</target>
74-
</configuration>
75-
</plugin>
76-
<plugin>
77-
<groupId>org.apache.maven.plugins</groupId>
78-
<artifactId>maven-javadoc-plugin</artifactId>
79-
<version>2.9.1</version>
80-
<configuration>
81-
<debug>true</debug>
82-
<outputDirectory>${basedir}/dist/docs</outputDirectory>
83-
<reportOutputDirectory>${basedir}/dist/docs</reportOutputDirectory>
84-
<author>false</author>
85-
<excludePackageNames>org.jboss.*</excludePackageNames>
86-
<aggregate>true</aggregate>
87-
</configuration>
48+
<dependencies>
49+
<dependency>
50+
<groupId>xom</groupId>
51+
<artifactId>xom</artifactId>
52+
<version>1.2.5</version>
53+
</dependency>
8854

89-
</plugin>
90-
<plugin>
91-
<groupId>org.apache.maven.plugins</groupId>
92-
<artifactId>maven-surefire-plugin</artifactId>
93-
<version>2.16</version>
94-
</plugin>
95-
<plugin>
96-
<groupId>org.apache.maven.plugins</groupId>
97-
<artifactId>maven-surefire-report-plugin</artifactId>
98-
<version>2.16</version>
99-
</plugin>
100-
</plugins>
101-
<resources>
102-
<resource>
103-
<filtering>true</filtering>
104-
<directory>src/main/resources</directory>
105-
</resource>
106-
</resources>
107-
</build>
108-
<reporting>
109-
<plugins>
110-
</plugins>
111-
</reporting>
112-
<scm>
113-
<url>https://github.com/3scale/3scale_ws_api_for_java</url>
114-
<connection>scm:git:git@github.com:3scale/3scale_ws_api_for_java.git</connection>
115-
<tag>HEAD</tag>
116-
</scm>
55+
<!-- Test only -->
56+
<dependency>
57+
<groupId>joda-time</groupId>
58+
<artifactId>joda-time</artifactId>
59+
<version>2.3</version>
60+
<scope>test</scope>
61+
</dependency>
62+
<dependency>
63+
<groupId>org.hamcrest</groupId>
64+
<artifactId>hamcrest-core</artifactId>
65+
<version>1.2</version>
66+
<scope>test</scope>
67+
</dependency>
68+
<dependency>
69+
<groupId>org.jmock</groupId>
70+
<artifactId>jmock-junit4</artifactId>
71+
<version>2.6.0</version>
72+
<scope>test</scope>
73+
</dependency>
74+
<dependency>
75+
<groupId>junit</groupId>
76+
<artifactId>junit</artifactId>
77+
<version>4.10</version>
78+
<scope>test</scope>
79+
</dependency>
80+
</dependencies>
81+
82+
<build>
83+
<resources>
84+
<resource>
85+
<filtering>true</filtering>
86+
<directory>src/main/resources</directory>
87+
</resource>
88+
</resources>
89+
<plugins>
90+
<plugin>
91+
<groupId>org.apache.maven.plugins</groupId>
92+
<artifactId>maven-compiler-plugin</artifactId>
93+
<version>3.1</version>
94+
<configuration>
95+
<source>${maven.compile.source}</source>
96+
<target>${maven.compile.source}</target>
97+
</configuration>
98+
</plugin>
99+
<plugin>
100+
<groupId>org.apache.maven.plugins</groupId>
101+
<artifactId>maven-javadoc-plugin</artifactId>
102+
<version>2.9.1</version>
103+
<executions>
104+
<execution>
105+
<id>attach-javadocs</id>
106+
<goals>
107+
<goal>jar</goal>
108+
</goals>
109+
</execution>
110+
</executions>
111+
</plugin>
112+
<plugin>
113+
<groupId>org.apache.maven.plugins</groupId>
114+
<artifactId>maven-source-plugin</artifactId>
115+
<version>3.0.1</version>
116+
<executions>
117+
<execution>
118+
<id>attach-sources</id>
119+
<goals>
120+
<goal>jar-no-fork</goal>
121+
</goals>
122+
</execution>
123+
</executions>
124+
</plugin>
125+
<plugin>
126+
<groupId>org.apache.maven.plugins</groupId>
127+
<artifactId>maven-surefire-plugin</artifactId>
128+
<version>2.16</version>
129+
</plugin>
130+
<plugin>
131+
<groupId>org.apache.maven.plugins</groupId>
132+
<artifactId>maven-surefire-report-plugin</artifactId>
133+
<version>2.16</version>
134+
</plugin>
135+
</plugins>
136+
</build>
137+
138+
<profiles>
139+
<profile>
140+
<id>release</id>
141+
<build>
142+
<plugins>
143+
<plugin>
144+
<groupId>org.sonatype.plugins</groupId>
145+
<artifactId>nexus-staging-maven-plugin</artifactId>
146+
<version>1.6.7</version>
147+
<extensions>true</extensions>
148+
<configuration>
149+
<serverId>ossrh</serverId>
150+
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
151+
<autoReleaseAfterClose>true</autoReleaseAfterClose>
152+
</configuration>
153+
</plugin>
154+
<plugin>
155+
<groupId>org.apache.maven.plugins</groupId>
156+
<artifactId>maven-gpg-plugin</artifactId>
157+
<version>1.6</version>
158+
<executions>
159+
<execution>
160+
<id>sign-artifacts</id>
161+
<phase>verify</phase>
162+
<goals>
163+
<goal>sign</goal>
164+
</goals>
165+
</execution>
166+
</executions>
167+
</plugin>
168+
</plugins>
169+
</build>
170+
</profile>
171+
<profile>
172+
<id>java8</id>
173+
<activation>
174+
<jdk>[1.8,)</jdk>
175+
</activation>
176+
<build>
177+
<plugins>
178+
<plugin>
179+
<groupId>org.apache.maven.plugins</groupId>
180+
<artifactId>maven-javadoc-plugin</artifactId>
181+
<configuration>
182+
<additionalparam>-Xdoclint:none</additionalparam>
183+
</configuration>
184+
</plugin>
185+
</plugins>
186+
</build>
187+
</profile>
188+
</profiles>
117189
</project>

‎src/main/java/threescale/v3/api/ParameterMap.java

+20-20
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,36 @@
11
package threescale.v3.api;
22

33
import java.util.HashMap;
4+
import java.util.LinkedHashMap;
45
import java.util.Set;
56

67
/**
78
* Hold a set of parameter and metrics for an AuthRep, Authorize, OAuth Authorize or Report.
89
* <p/>
910
* Each item consists of a name/value pair, where the value can be a String, An Array of ParameterMaps or another Parameter Map.
1011
* <p/>
12+
* <p>
1113
* E.g. For an AuthRep:
14+
* </p>
1215
* <code>
13-
* ParameterMap params = new ParameterMap();
14-
* params.add("app_id", "app_1234");
15-
* <p/>
16-
* ParameterMap usage = new ParameterMap();
17-
* usage.add("hits", "3");
18-
* <p/>
19-
* params.add("usage", usage);
20-
* <p/>
21-
* AuthorizeResponse response = serviceApi.authrep(params);
16+
* ParameterMap params = new ParameterMap();<br/>
17+
* params.add("app_id", "app_1234");<br/>
18+
* ParameterMap usage = new ParameterMap();<br/>
19+
* usage.add("hits", "3");<br/>
20+
* params.add("usage", usage);<br/>
21+
* AuthorizeResponse response = serviceApi.authrep(params);<br/>
2222
* </code>
23-
* <p/>
23+
* <p>
2424
* An example for a report might be:
25-
* <code>
26-
* ParameterMap params = new ParameterMap();
27-
* params.add("app_id", "foo");
28-
* params.add("timestamp", fmt.print(new DateTime(2010, 4, 27, 15, 0)));
29-
* <p/>
30-
* ParameterMap usage = new ParameterMap();
31-
* usage.add("hits", "1");
32-
* params.add("usage", usage);
3325
* <p/>
34-
* ReportResponse response = serviceApi.report(params);
26+
* <code>
27+
* ParameterMap params = new ParameterMap();<br/>
28+
* params.add("app_id", "foo");<br/>
29+
* params.add("timestamp", fmt.print(new DateTime(2010, 4, 27, 15, 0)));<br/>
30+
* ParameterMap usage = new ParameterMap();<br/>
31+
* usage.add("hits", "1");<br/>
32+
* params.add("usage", usage);<br/>
33+
* ReportResponse response = serviceApi.report(params);<br/>
3534
* </code>
3635
*/
3736
public class ParameterMap {
@@ -42,7 +41,8 @@ public class ParameterMap {
4241
* Construct and empty ParameterMap
4342
*/
4443
public ParameterMap() {
45-
data = new HashMap<String, Object>();
44+
// Note: use a linked hash map for more predictable serialization of the parameters (mostly for testing)
45+
data = new LinkedHashMap<String, Object>();
4646
}
4747

4848
/**

‎src/main/java/threescale/v3/api/ServerError.java

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
public class ServerError extends Exception {
77

8+
private static final long serialVersionUID = -5900004126517852322L;
9+
810
public ServerError(String reason) {
911
super(reason);
1012
}

‎src/main/java/threescale/v3/api/impl/ParameterEncoder.java

+17-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class ParameterEncoder {
1414
* @return Encoded string
1515
*/
1616
public String encode(ParameterMap params) {
17-
StringBuffer result = new StringBuffer();
17+
StringBuilder result = new StringBuilder();
1818

1919
int index = 0;
2020
for (String mapKey : params.getKeys()) {
@@ -37,7 +37,7 @@ public String encode(ParameterMap params) {
3737
}
3838

3939
private String emitNormalArray(String mapKey, ParameterMap[] mapValue) {
40-
StringBuffer b = new StringBuffer();
40+
StringBuilder b = new StringBuilder();
4141
int index = 0;
4242

4343
for (ParameterMap arrayMap : mapValue) {
@@ -49,7 +49,7 @@ private String emitNormalArray(String mapKey, ParameterMap[] mapValue) {
4949
}
5050

5151
private String emitArray(String mapKey, ParameterMap arrayMap, int arrayIndex) {
52-
StringBuffer b = new StringBuffer();
52+
StringBuilder b = new StringBuilder();
5353
int index = 0;
5454

5555
for (String key : arrayMap.getKeys()) {
@@ -67,42 +67,52 @@ private String emitArray(String mapKey, ParameterMap arrayMap, int arrayIndex) {
6767
b.append(emitArrayValue(mapKey, key, itemKey, map.getStringValue(itemKey), arrayIndex));
6868
index++;
6969
}
70+
break;
71+
case ARRAY:
72+
// TODO does ARRAY need to be handled?
73+
break;
7074
}
7175
}
7276
return b.toString();
7377
}
7478

7579
private String emitArrayValue(String mapKey, String key, String itemKey, String stringValue, int index) {
76-
StringBuffer b = new StringBuffer();
80+
StringBuilder b = new StringBuilder();
7781
b.append(mapKey).append("[").append(index).append("]");
7882
b.append("[").append(key).append("]");
7983
b.append("[").append(itemKey).append("]=").append(stringValue);
8084
return b.toString();
8185
}
8286

8387
private String emitNormalMap(String mapKey, ParameterMap mapValue) {
84-
StringBuffer b = new StringBuffer();
88+
StringBuilder b = new StringBuilder();
8589
int index = 0;
8690
for (String key : mapValue.getKeys()) {
8791
if (index != 0) b.append("&");
8892
switch (mapValue.getType(key)) {
8993
case STRING:
9094
b.append(emitMapValue(mapKey, key, mapValue.getStringValue(key)));
9195
break;
96+
case MAP:
97+
// TODO does MAP need to be handled?
98+
break;
99+
case ARRAY:
100+
// TODO does ARRAY need to be handled?
101+
break;
92102
}
93103
index++;
94104
}
95105
return b.toString();
96106
}
97107

98108
private String emitMapValue(String mapKey, String key, String stringValue) {
99-
StringBuffer b = new StringBuffer();
109+
StringBuilder b = new StringBuilder();
100110
b.append("[").append(mapKey).append("][").append(key).append("]=").append(stringValue);
101111
return b.toString();
102112
}
103113

104114
private String emitNormalValue(String key, String value) {
105-
StringBuffer b = new StringBuffer();
115+
StringBuilder b = new StringBuilder();
106116
b.append(key).append("=").append(value);
107117
return b.toString();
108118
}

‎src/main/java/threescale/v3/api/impl/ServiceApiDriver.java

+1-9
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@ public class ServiceApiDriver implements ServiceApi {
1212
private String provider_key = null;
1313
private String host = DEFAULT_HOST;
1414
private boolean useHttps = false;
15-
private String redirect_url = "http://localhost:8080/oauth/oauth_redirect";
15+
// private String redirect_url = "http://localhost:8080/oauth/oauth_redirect";
1616

1717
private ServerAccessor server = null;
1818

1919
public ServiceApiDriver() {
2020
this.server = new ServerAccessorDriver();
21-
2221
}
2322

2423
public ServiceApiDriver(String provider_key) {
@@ -78,15 +77,8 @@ public ReportResponse report(String service_id, ParameterMap... transactions) th
7877
if (service_id != null) {
7978
params.add("service_id", service_id);
8079
}
81-
ParameterMap trans = new ParameterMap();
8280
params.add("transactions", transactions);
8381

84-
int index = 0;
85-
for (ParameterMap transaction : transactions) {
86-
trans.add("" + index, transaction);
87-
index++;
88-
}
89-
9082
HttpResponse response = server.post(getFullHostUrl() + "/transactions.xml", encodeAsString(params));
9183
if (response.getStatus() == 500) {
9284
throw new ServerError(response.getBody());

‎src/test/java/threescale/v3/api/ServiceApiDriverIntegrationTest.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22

33
import static org.junit.Assert.assertTrue;
44

5-
import org.jmock.Expectations;
6-
import org.joda.time.DateTime;
75
import org.joda.time.format.DateTimeFormat;
86
import org.joda.time.format.DateTimeFormatter;
97
import org.junit.Before;
10-
import org.junit.Test;
118

129
import threescale.v3.api.example.TestKeys;
1310
import threescale.v3.api.impl.ServiceApiDriver;
@@ -31,7 +28,7 @@ public class ServiceApiDriverIntegrationTest {
3128
private final String provider_key = TestKeys.my_provider_key;
3229

3330
private ServiceApi serviceApi;
34-
private ServerAccessor htmlServer;
31+
// private ServerAccessor htmlServer;
3532

3633
DateTimeFormatter fmt;
3734
ParameterMap params;

‎src/test/java/threescale/v3/api/ServiceApiDriverTest.java

+121-38
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertFalse;
5-
import static org.junit.Assert.assertNull;
65
import static org.junit.Assert.assertTrue;
76

7+
import java.util.Arrays;
8+
import java.util.List;
9+
10+
import org.hamcrest.Description;
11+
import org.hamcrest.TypeSafeMatcher;
812
import org.jmock.Expectations;
913
import org.jmock.integration.junit4.JUnitRuleMockery;
1014
import org.joda.time.DateTime;
@@ -22,6 +26,7 @@
2226
*/
2327

2428
public class ServiceApiDriverTest {
29+
2530
@Rule
2631
public JUnitRuleMockery context = new JUnitRuleMockery();
2732

@@ -57,7 +62,7 @@ public void test_custom_host() {
5762

5863
@Test
5964
public void test_authrep_usage_is_encoded() throws ServerError {
60-
assertAuthrepUrlWithParams("&%5Busage%5D%5Bmethod%5D=666");
65+
assertAuthrepUrlWithParams("%5Busage%5D%5Bmethod%5D=666");
6166

6267
ParameterMap params = new ParameterMap();
6368
ParameterMap usage = new ParameterMap();
@@ -70,7 +75,7 @@ public void test_authrep_usage_is_encoded() throws ServerError {
7075
@Test
7176
public void test_authrep_usage_values_are_encoded() throws ServerError {
7277

73-
assertAuthrepUrlWithParams("&%5Busage%5D%5Bhits%5D=%230");
78+
assertAuthrepUrlWithParams("%5Busage%5D%5Bhits%5D=%230");
7479

7580
ParameterMap params = new ParameterMap();
7681
ParameterMap usage = new ParameterMap();
@@ -83,7 +88,7 @@ public void test_authrep_usage_values_are_encoded() throws ServerError {
8388
@Test
8489
public void test_authrep_usage_defaults_to_hits_1() throws ServerError {
8590

86-
assertAuthrepUrlWithParams("&%5Busage%5D%5Bhits%5D=1&app_id=appid");
91+
assertAuthrepUrlWithParams("%5Busage%5D%5Bhits%5D=1&app_id=appid");
8792

8893
ParameterMap params = new ParameterMap();
8994
params.add("app_id", "appid");
@@ -93,7 +98,7 @@ public void test_authrep_usage_defaults_to_hits_1() throws ServerError {
9398

9499
@Test
95100
public void test_authrep_supports_app_id_app_key_auth_mode() throws ServerError {
96-
assertAuthrepUrlWithParams("&%5Busage%5D%5Bhits%5D=1&app_key=appkey&app_id=appid");
101+
assertAuthrepUrlWithParams("%5Busage%5D%5Bhits%5D=1&app_key=appkey&app_id=appid");
97102

98103
ParameterMap params = new ParameterMap();
99104
params.add("app_id", "appid");
@@ -108,8 +113,8 @@ public void test_successful_authrep_with_app_keys() throws ServerError {
108113
"<plan>Ultimate</plan>" +
109114
"</status>";
110115

111-
context.checking(new Expectations() {{
112-
oneOf(htmlServer).get("http://" + host + "/transactions/authrep.xml?provider_key=1234abcd&%5Busage%5D%5Bhits%5D=1&app_key=toosecret&app_id=foo");
116+
context.checking(new UrlWithParamsExpectations() {{
117+
oneOf(htmlServer).get(withUrl("http://" + host + "/transactions/authrep.xml?provider_key=1234abcd&%5Busage%5D%5Bhits%5D=1&app_key=toosecret&app_id=foo"));
113118
will(returnValue(new HttpResponse(200, body)));
114119
}});
115120

@@ -144,8 +149,8 @@ public void test_successful_authorize() throws ServerError {
144149
" </usage_reports>" +
145150
"</status>";
146151

147-
context.checking(new Expectations() {{
148-
oneOf(htmlServer).get("http://" + host + "/transactions/authorize.xml?provider_key=1234abcd&app_id=foo");
152+
context.checking(new UrlWithParamsExpectations() {{
153+
oneOf(htmlServer).get(withUrl("http://" + host + "/transactions/authorize.xml?provider_key=1234abcd&app_id=foo"));
149154
will(returnValue(new HttpResponse(200, body)));
150155
}});
151156

@@ -177,8 +182,8 @@ public void test_successful_authorize_with_app_keys() throws ServerError {
177182
"<plan>Ultimate</plan>" +
178183
"</status>";
179184

180-
context.checking(new Expectations() {{
181-
oneOf(htmlServer).get("http://" + host + "/transactions/authorize.xml?provider_key=1234abcd&app_key=toosecret&app_id=foo");
185+
context.checking(new UrlWithParamsExpectations() {{
186+
oneOf(htmlServer).get(withUrl("http://" + host + "/transactions/authorize.xml?provider_key=1234abcd&app_key=toosecret&app_id=foo"));
182187
will(returnValue(new HttpResponse(200, body)));
183188
}});
184189

@@ -216,8 +221,8 @@ public void test_authorize_with_exceeded_usage_limits() throws ServerError {
216221
"</status>";
217222

218223

219-
context.checking(new Expectations() {{
220-
oneOf(htmlServer).get("http://" + host + "/transactions/authorize.xml?provider_key=1234abcd&app_id=foo");
224+
context.checking(new UrlWithParamsExpectations() {{
225+
oneOf(htmlServer).get(withUrl("http://" + host + "/transactions/authorize.xml?provider_key=1234abcd&app_id=foo"));
221226
will(returnValue(new HttpResponse(409, body)));
222227
}});
223228

@@ -234,8 +239,8 @@ public void test_authorize_with_exceeded_usage_limits() throws ServerError {
234239
public void test_authorize_with_invalid_app_id() throws ServerError {
235240
final String body = "<error code=\"application_not_found\">application with id=\"foo\" was not found</error>";
236241

237-
context.checking(new Expectations() {{
238-
oneOf(htmlServer).get("http://" + host + "/transactions/authorize.xml?provider_key=1234abcd&app_id=foo");
242+
context.checking(new UrlWithParamsExpectations() {{
243+
oneOf(htmlServer).get(withUrl("http://" + host + "/transactions/authorize.xml?provider_key=1234abcd&app_id=foo"));
239244
will(returnValue(new HttpResponse(403, body)));
240245
}});
241246

@@ -263,8 +268,8 @@ public void test_authorize_metric_period_eternity() throws ServerError {
263268
"</status>";
264269

265270

266-
context.checking(new Expectations() {{
267-
oneOf(htmlServer).get("http://" + host + "/transactions/authorize.xml?provider_key=1234abcd&app_id=foo");
271+
context.checking(new UrlWithParamsExpectations() {{
272+
oneOf(htmlServer).get(withUrl("http://" + host + "/transactions/authorize.xml?provider_key=1234abcd&app_id=foo"));
268273
will(returnValue(new HttpResponse(200, body)));
269274
}});
270275

@@ -287,8 +292,8 @@ public void test_authorize_metric_period_eternity() throws ServerError {
287292

288293
@Test(expected = ServerError.class)
289294
public void test_authorize_with_server_error() throws ServerError {
290-
context.checking(new Expectations() {{
291-
oneOf(htmlServer).get("http://" + host + "/transactions/authorize.xml?provider_key=1234abcd&app_id=foo");
295+
context.checking(new UrlWithParamsExpectations() {{
296+
oneOf(htmlServer).get(withUrl("http://" + host + "/transactions/authorize.xml?provider_key=1234abcd&app_id=foo"));
292297
will(returnValue(new HttpResponse(500, "OMG! WTF!")));
293298
}});
294299

@@ -325,8 +330,8 @@ public void test_successful_oauth_authorize() throws ServerError {
325330
"</usage_reports>" +
326331
"</status>";
327332

328-
context.checking(new Expectations() {{
329-
oneOf(htmlServer).get("http://" + host + "/transactions/oauth_authorize.xml?redirect_url=http%3A//localhost%3A8080/oauth/oauth_redirect&provider_key=1234abcd&app_id=foo");
333+
context.checking(new UrlWithParamsExpectations() {{
334+
oneOf(htmlServer).get(withUrl("http://" + host + "/transactions/oauth_authorize.xml?redirect_url=http%3A//localhost%3A8080/oauth/oauth_redirect&provider_key=1234abcd&app_id=foo"));
330335
will(returnValue(new HttpResponse(200, body)));
331336
}});
332337

@@ -384,8 +389,8 @@ public void test_oauth_authorize_with_exceeded_usage_limits() throws ServerError
384389
"</usage_reports>" +
385390
"</status>";
386391

387-
context.checking(new Expectations() {{
388-
oneOf(htmlServer).get("http://" + host + "/transactions/oauth_authorize.xml?provider_key=1234abcd&app_id=foo");
392+
context.checking(new UrlWithParamsExpectations() {{
393+
oneOf(htmlServer).get(withUrl("http://" + host + "/transactions/oauth_authorize.xml?provider_key=1234abcd&app_id=foo"));
389394
will(returnValue(new HttpResponse(409, body)));
390395
}});
391396

@@ -403,8 +408,8 @@ public void test_oauth_authorize_with_exceeded_usage_limits() throws ServerError
403408
public void test_oauth_authorize_with_invalid_app_id() throws ServerError {
404409
final String body = "<error code=\"application_not_found\">application with id=\"foo\" was not found</error>";
405410

406-
context.checking(new Expectations() {{
407-
oneOf(htmlServer).get("http://" + host + "/transactions/oauth_authorize.xml?provider_key=1234abcd&app_id=foo");
411+
context.checking(new UrlWithParamsExpectations() {{
412+
oneOf(htmlServer).get(withUrl("http://" + host + "/transactions/oauth_authorize.xml?provider_key=1234abcd&app_id=foo"));
408413
will(returnValue(new HttpResponse(403, body)));
409414
}});
410415

@@ -421,8 +426,8 @@ public void test_oauth_authorize_with_invalid_app_id() throws ServerError {
421426
@Test(expected = ServerError.class)
422427
public void test_oath_authorize_with_server_error() throws ServerError {
423428

424-
context.checking(new Expectations() {{
425-
oneOf(htmlServer).get("http://" + host + "/transactions/oauth_authorize.xml?provider_key=1234abcd&app_id=foo");
429+
context.checking(new UrlWithParamsExpectations() {{
430+
oneOf(htmlServer).get(withUrl("http://" + host + "/transactions/oauth_authorize.xml?provider_key=1234abcd&app_id=foo"));
426431
will(returnValue(new HttpResponse(500, "OMG! WTF!")));
427432
}});
428433

@@ -434,13 +439,13 @@ public void test_oath_authorize_with_server_error() throws ServerError {
434439

435440
@Test(expected = IllegalArgumentException.class)
436441
public void test_report_raises_an_exception_if_no_transactions_given() throws ServerError {
437-
serviceApi.report(null, null);
442+
serviceApi.report(null);
438443
}
439444

440445
@Test
441446
public void test_successful_report() throws ServerError {
442447

443-
context.checking(new Expectations() {{
448+
context.checking(new UrlWithParamsExpectations() {{
444449
oneOf(htmlServer).post(with("http://" + host + "/transactions.xml"), with(any(String.class)));
445450
will(returnValue(new HttpResponse(202, "")));
446451
}});
@@ -471,8 +476,8 @@ public void test_report_encodes_transactions() throws ServerError {
471476
"&transactions%5B1%5D%5Bapp_id%5D=bar" +
472477
"&provider_key=1234abcd";
473478

474-
context.checking(new Expectations() {{
475-
oneOf(htmlServer).post("http://" + host + "/transactions.xml", urlParams);
479+
context.checking(new UrlWithParamsExpectations() {{
480+
oneOf(htmlServer).post(with("http://" + host + "/transactions.xml"), withParams(urlParams));
476481
will(returnValue(new HttpResponse(200, "")));
477482
}});
478483

@@ -501,7 +506,7 @@ public void test_report_encodes_transactions() throws ServerError {
501506
public void test_failed_report() throws ServerError {
502507
final String error_body = "<error code=\"provider_key_invalid\">provider key \"foo\" is invalid</error>";
503508

504-
context.checking(new Expectations() {{
509+
context.checking(new UrlWithParamsExpectations() {{
505510
oneOf(htmlServer).post(with("http://" + host + "/transactions.xml"), with(any(String.class)));
506511
will(returnValue(new HttpResponse(403, error_body)));
507512
}});
@@ -522,7 +527,7 @@ public void test_failed_report() throws ServerError {
522527
@Test(expected = ServerError.class)
523528
public void test_report_with_server_error() throws ServerError {
524529

525-
context.checking(new Expectations() {{
530+
context.checking(new UrlWithParamsExpectations() {{
526531
oneOf(htmlServer).post(with("http://" + host + "/transactions.xml"), with(any(String.class)));
527532
will(returnValue(new HttpResponse(500, "OMG! WTF!")));
528533
}});
@@ -537,16 +542,94 @@ public void test_report_with_server_error() throws ServerError {
537542

538543

539544
private void assertAuthrepUrlWithParams(final String params) throws ServerError {
540-
final String authrep_url = "http://" + host + "/transactions/authrep.xml?provider_key=" + provider_key + params;
545+
final String authrep_url = "http://" + host + "/transactions/authrep.xml?" + params + "&provider_key=" + provider_key;
541546

542547
final String body = "<status>" +
543548
"<authorized>true</authorized>" +
544549
"<plan>Ultimate</plan>" +
545550
"</status>";
546551
//System.out.println("Expect: "+ authrep_url);
547-
context.checking(new Expectations() {{
548-
oneOf(htmlServer).get(authrep_url);
549-
will(returnValue(new HttpResponse(200, body)));
550-
}});
552+
context.checking(new UrlWithParamsExpectations() {
553+
{
554+
oneOf(htmlServer).get(withUrl(authrep_url));
555+
will(returnValue(new HttpResponse(200, body)));
556+
}
557+
});
558+
}
559+
560+
private static class UrlWithParamsExpectations extends Expectations {
561+
562+
protected String withUrl(String url) {
563+
currentBuilder().addParameterMatcher(new UrlWithParamsMatcher(url));
564+
return url;
565+
}
566+
567+
protected String withParams(String params) {
568+
currentBuilder().addParameterMatcher(new ParamsMatcher(params));
569+
return params;
570+
}
571+
551572
}
573+
574+
private static class UrlWithParamsMatcher extends TypeSafeMatcher<String> {
575+
576+
private final String expectedUrl;
577+
578+
public UrlWithParamsMatcher(String expectedUrl) {
579+
this.expectedUrl = expectedUrl;
580+
}
581+
582+
@Override
583+
public boolean matchesSafely(String actualUrl) {
584+
// Match the first part of the URL (scheme, host, port, etc)
585+
String expectedUrlHead = expectedUrl.substring(0, expectedUrl.indexOf("?"));
586+
String actualUrlHead = actualUrl.substring(0, actualUrl.indexOf("?"));
587+
588+
// Match the params (order then compare)
589+
String expectedParams = expectedUrl.substring(expectedUrl.indexOf("?") + 1);
590+
String[] expectedParamsSplit = expectedParams.split("&");
591+
Arrays.sort(expectedParamsSplit);
592+
String actualParams = actualUrl.substring(actualUrl.indexOf("?") + 1);
593+
String[] actualParamsSplit = actualParams.split("&");
594+
Arrays.sort(actualParamsSplit);
595+
596+
List<String> expected = Arrays.asList(expectedParamsSplit);
597+
List<String> actual = Arrays.asList(actualParamsSplit);
598+
599+
return expected.equals(actual) && expectedUrlHead.equals(actualUrlHead);
600+
}
601+
602+
@Override
603+
public void describeTo(Description description) {
604+
description.appendText("a url matching '" + this.expectedUrl + "'");
605+
}
606+
}
607+
608+
private static class ParamsMatcher extends TypeSafeMatcher<String> {
609+
610+
private final String expectedParams;
611+
612+
public ParamsMatcher(String expectedParams) {
613+
this.expectedParams = expectedParams;
614+
}
615+
616+
@Override
617+
public boolean matchesSafely(String actualParams) {
618+
String[] expectedParamsSplit = expectedParams.split("&");
619+
Arrays.sort(expectedParamsSplit);
620+
String[] actualParamsSplit = actualParams.split("&");
621+
Arrays.sort(actualParamsSplit);
622+
623+
List<String> expected = Arrays.asList(expectedParamsSplit);
624+
List<String> actual = Arrays.asList(actualParamsSplit);
625+
626+
return expected.equals(actual);
627+
}
628+
629+
@Override
630+
public void describeTo(Description description) {
631+
description.appendText("params matching '" + this.expectedParams + "'");
632+
}
633+
}
634+
552635
}

‎src/test/java/threescale/v3/api/impl/ParameterEncoderTest.java

+9-5
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void testEncodeTwoParametersAndOneMap() throws Exception {
5252
usage.add("hits", "111");
5353
param.add("usage", usage);
5454

55-
assertEquals("provider_key=123abc&%5Busage%5D%5Bhits%5D=111&app_id=3456aaa", encoder.encode(param));
55+
assertEquals("provider_key=123abc&app_id=3456aaa&%5Busage%5D%5Bhits%5D=111", encoder.encode(param));
5656
}
5757

5858
@Test
@@ -68,16 +68,20 @@ public void testEncodeTwoParametersAndTwoMap() throws Exception {
6868

6969

7070
assertEquals(
71-
"provider_key=123abc&%5Busage%5D%5Btimestamp%5D=2010-04-27%2015%3A00%3A00%20+0000&%5Busage%5D%5Bhits%5D=111&app_id=3456aaa",
71+
"provider_key=123abc&app_id=3456aaa&%5Busage%5D%5Bhits%5D=111&%5Busage%5D%5Btimestamp%5D=2010-04-27%2015%3A00%3A00%20+0000",
7272
encoder.encode(param));
7373
}
7474

7575
@Test
7676
public void testEncodingAnArray() throws Exception {
7777
final String expected =
78-
"transactions%5B0%5D%5Btimestamp%5D=2010-04-27%2015%3A42%3A17%200200&transactions%5B0%5D%5Busage%5D%5Bhits%5D=1&transactions%5B0%5D%5Bapp_id%5D=foo&" +
79-
"transactions%5B1%5D%5Btimestamp%5D=2010-04-27%2015%3A55%3A12%200200&transactions%5B1%5D%5Busage%5D%5Bhits%5D=1&transactions%5B1%5D%5Bapp_id%5D=bar&" +
80-
"provider_key=1234abcd";
78+
"provider_key=1234abcd&"
79+
+ "transactions%5B0%5D%5Bapp_id%5D=foo&"
80+
+ "transactions%5B0%5D%5Btimestamp%5D=2010-04-27%2015%3A42%3A17%200200&"
81+
+ "transactions%5B0%5D%5Busage%5D%5Bhits%5D=1&"
82+
+ "transactions%5B1%5D%5Bapp_id%5D=bar&"
83+
+ "transactions%5B1%5D%5Btimestamp%5D=2010-04-27%2015%3A55%3A12%200200&"
84+
+ "transactions%5B1%5D%5Busage%5D%5Bhits%5D=1";
8185

8286
ParameterMap app1 = new ParameterMap();
8387
app1.add("app_id", "foo");

‎src/test/java/threescale/v3/api/impl/ServiceAccessorDriverTest.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.junit.Assert.assertEquals;
44

5+
import org.junit.Assert;
56
import org.junit.Before;
67
import org.junit.Test;
78

@@ -14,8 +15,6 @@
1415
*/
1516

1617
public class ServiceAccessorDriverTest {
17-
private static String expectedMvenVersion="3.1.0";
18-
1918
private ServerAccessorDriver underTest = new ServerAccessorDriver();;
2019

2120

@@ -27,8 +26,8 @@ public void setup() {
2726
@Test
2827
public void testClientHeader() {
2928
String actualClientHeader = underTest.getPluginHeaderValue();
30-
String expectedHedaer = ServerAccessor.X_3SCALE_USER_CLIENT_HEADER_JAVA_PLUGIN+expectedMvenVersion;
31-
assertEquals(actualClientHeader, expectedHedaer);
29+
Assert.assertNotNull(actualClientHeader);
30+
Assert.assertTrue(actualClientHeader.startsWith(ServerAccessor.X_3SCALE_USER_CLIENT_HEADER_JAVA_PLUGIN));
3231
}
3332

3433
//@Test

0 commit comments

Comments
 (0)
Please sign in to comment.