Skip to content
This repository has been archived by the owner on Sep 12, 2022. It is now read-only.

WIP: Adds the ability to add a dynamic value to a JSON #15

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
buildscript {
repositories {
google()
jcenter()
maven { url "http://maven.google.com" }
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7'
classpath 'com.android.tools.build:gradle:3.0.0'
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
}
}

allprojects {
repositories {
google()
jcenter()
maven { url "http://maven.google.com" }
}
}

ext {
minSdkVersion = 15
targetSdkVersion = 25
targetSdkVersion = 27
versionCode = Integer.parseInt(VERSION_CODE)
versionName = VERSION_NAME
}
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
52 changes: 27 additions & 25 deletions requestmatcher/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ apply from: "$rootDir/tools/publish.gradle"
apply from: "$rootDir/tools/jacoco.gradle"

android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
compileSdkVersion 27
buildToolsVersion '27.0.0'

defaultConfig {
minSdkVersion rootProject.ext.minSdkVersion
Expand All @@ -25,40 +25,42 @@ android {
}

sourceSets {
androidTest.java.srcDirs += "src/test-common/java"
test.java.srcDirs += "src/test-common/java"
androidTest.java.srcDirs += 'src/test-common/java'
test.java.srcDirs += 'src/test-common/java'
}
}

final supportLibrary = '27.0.0'
final junit = '4.12'
final jsonPathAssert = '2.2.0'
final jsonPathAssert = '2.4.0'
final hamcrest = '1.3'
final okhttpVersion = '3.8.0'
final testRunner = '0.5'
final okhttpVersion = '3.9.0'
final astl = '1.0.1'

dependencies {

provided "junit:junit:$junit"
provided "com.jayway.jsonpath:json-path-assert:$jsonPathAssert"
provided "org.hamcrest:hamcrest-all:$hamcrest"
provided "com.squareup.okhttp3:mockwebserver:$okhttpVersion"
provided "com.android.support.test:runner:$testRunner"
compileOnly "junit:junit:$junit"
compileOnly "com.jayway.jsonpath:json-path-assert:$jsonPathAssert"
compileOnly "org.hamcrest:hamcrest-all:$hamcrest"
compileOnly "com.squareup.okhttp3:mockwebserver:$okhttpVersion"
compileOnly "com.android.support.test:runner:$astl"

testCompile "junit:junit:$junit"
testCompile "com.jayway.jsonpath:json-path-assert:$jsonPathAssert"
testCompile "org.hamcrest:hamcrest-all:$hamcrest"
testCompile "com.squareup.okhttp3:mockwebserver:$okhttpVersion"
testCompile "com.squareup.okhttp3:okhttp:$okhttpVersion"
testCompile 'org.robolectric:robolectric:3.3.2'
testCompile 'org.mockito:mockito-all:1.10.19'
testImplementation "junit:junit:$junit"
testImplementation "com.jayway.jsonpath:json-path-assert:$jsonPathAssert"
testImplementation "org.hamcrest:hamcrest-all:$hamcrest"
testImplementation "com.squareup.okhttp3:mockwebserver:$okhttpVersion"
testImplementation "com.squareup.okhttp3:okhttp:$okhttpVersion"
testImplementation 'org.robolectric:robolectric:3.5'
testImplementation 'org.mockito:mockito-core:2.11.0'
testImplementation "com.android.support:support-annotations:$supportLibrary"

androidTestCompile "com.squareup.okhttp3:mockwebserver:$okhttpVersion"
androidTestCompile("com.jayway.jsonpath:json-path-assert:$jsonPathAssert") {
androidTestImplementation "com.squareup.okhttp3:mockwebserver:$okhttpVersion"
androidTestImplementation("com.jayway.jsonpath:json-path-assert:$jsonPathAssert") {
exclude group: "org.ow2.asm"
}
androidTestCompile "com.squareup.okhttp3:okhttp:$okhttpVersion"
androidTestCompile "com.android.support.test.espresso:espresso-core:2.2.2"
androidTestCompile "com.android.support.test:runner:0.5"
androidTestCompile 'com.android.support:support-annotations:25.4.0'
androidTestImplementation "com.squareup.okhttp3:okhttp:$okhttpVersion"
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
androidTestImplementation "com.android.support.test:runner:$astl"
androidTestImplementation "com.android.support:support-annotations:$supportLibrary"
}

Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package br.com.concretesolutions.requestmatcher.test;
package br.com.concrete.requestmatcher.test;

import android.support.test.runner.AndroidJUnit4;

import org.junit.runner.RunWith;

import br.com.concretesolutions.requestmatcher.InstrumentedTestRequestMatcherRule;
import br.com.concretesolutions.requestmatcher.RequestMatcherRule;
import br.com.concrete.requestmatcher.InstrumentedTestRequestMatcherRule;
import br.com.concrete.requestmatcher.RequestMatcherRule;

@RunWith(AndroidJUnit4.class)
public class InstrumentedExceptionInStatementTest extends ExceptionInStatementTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package br.com.concretesolutions.requestmatcher.test;
package br.com.concrete.requestmatcher.test;

import android.support.test.runner.AndroidJUnit4;

import org.junit.runner.RunWith;

import br.com.concretesolutions.requestmatcher.InstrumentedTestRequestMatcherRule;
import br.com.concretesolutions.requestmatcher.LocalTestRequestMatcherRule;
import br.com.concretesolutions.requestmatcher.RequestMatcherRule;
import br.com.concrete.requestmatcher.InstrumentedTestRequestMatcherRule;
import br.com.concrete.requestmatcher.RequestMatcherRule;

@RunWith(AndroidJUnit4.class)
public class InstrumentedExceptionWitouhtEnqueuesTest extends ExceptionWitouhtEnqueuesTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package br.com.concretesolutions.requestmatcher.test;
package br.com.concrete.requestmatcher.test;

import android.support.test.runner.AndroidJUnit4;

import org.junit.runner.RunWith;

import br.com.concretesolutions.requestmatcher.InstrumentedTestRequestMatcherRule;
import br.com.concretesolutions.requestmatcher.RequestMatcherRule;
import br.com.concrete.requestmatcher.InstrumentedTestRequestMatcherRule;
import br.com.concrete.requestmatcher.RequestMatcherRule;

@RunWith(AndroidJUnit4.class)
public class InstrumentedRequestMatcherRuleTest extends RequestMatcherRuleTest {
Expand Down
14 changes: 1 addition & 13 deletions requestmatcher/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="br.com.concretesolutions.requestmatcher">

<uses-permission android:name="android.permission.INTERNET" />

<application
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true">

</application>

</manifest>
<manifest package="br.com.concrete.requestmatcher" />
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.com.concretesolutions.requestmatcher;
package br.com.concrete.requestmatcher;

import android.util.Log;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.com.concretesolutions.requestmatcher;
package br.com.concrete.requestmatcher;

import android.support.test.InstrumentationRegistry;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.com.concretesolutions.requestmatcher;
package br.com.concrete.requestmatcher;

import java.io.IOException;
import java.io.InputStream;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.com.concretesolutions.requestmatcher;
package br.com.concrete.requestmatcher;

import java.util.Collections;
import java.util.Set;
Expand All @@ -7,8 +7,8 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import br.com.concretesolutions.requestmatcher.exception.NoMatchersForRequestException;
import br.com.concretesolutions.requestmatcher.exception.RequestAssertionException;
import br.com.concrete.requestmatcher.exception.NoMatchersForRequestException;
import br.com.concrete.requestmatcher.exception.RequestAssertionException;
import okhttp3.mockwebserver.Dispatcher;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.RecordedRequest;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.com.concretesolutions.requestmatcher;
package br.com.concrete.requestmatcher;

import org.junit.rules.TestRule;
import org.junit.runner.Description;
Expand All @@ -11,7 +11,7 @@
import java.util.logging.Level;
import java.util.logging.Logger;

import br.com.concretesolutions.requestmatcher.exception.RequestAssertionException;
import br.com.concrete.requestmatcher.exception.RequestAssertionException;
import okhttp3.HttpUrl;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
Expand Down Expand Up @@ -172,24 +172,24 @@ public IfRequestMatches<RequestMatchersGroup> addResponse(MockResponse response)
* Adds a fixture to be used during the test case.
*
* @param fixturePath The path of the fixture inside the fixtures folder.
* @param values Optional dynamic values to be replaced in the fixture file
* @return A dsl instance {@link IfRequestMatches} for chaining
*/
public IfRequestMatches<RequestMatchersGroup> addFixture(String fixturePath) {
return addFixture(200, fixturePath);
public IfRequestMatches<RequestMatchersGroup> addFixture(String fixturePath, String... values) {
return addFixture(200, fixturePath, values);
}

/**
* Adds a fixture to be used during the test case.
*
* @param fixturePath The path of the fixture inside the fixtures folder.
* @param statusCode The status of the mocked response.
* @param values Optional dynamic values to be replaced in the fixture file
* @return A dsl instance {@link IfRequestMatches} for chaining
*/
public IfRequestMatches<RequestMatchersGroup> addFixture(int statusCode, String fixturePath) {
public IfRequestMatches<RequestMatchersGroup> addFixture(int statusCode, String fixturePath, String... values) {

final MockResponse mockResponse = new MockResponse()
.setResponseCode(statusCode)
.setBody(readFixture(fixturePath));
final MockResponse mockResponse = createMockResponse(statusCode, fixturePath, values);

if (guessMimeType) {
final String mimeType = IOReader.mimeTypeFromExtension(fixturePath);
Expand All @@ -213,13 +213,15 @@ public IfRequestMatches<RequestMatchersGroup> addFixture(int statusCode, String
*
* @param fixturePath The path of the fixture inside the fixtures fodler.
* @param matcher The {@link RequestMatchersGroup} instance to use for matching.
* @param values Optional dynamic values to be replaced in the fixture file
* @return A dsl instance {@link IfRequestMatches} for chaining
*/
public <T extends RequestMatchersGroup> IfRequestMatches<T> addFixture(String fixturePath,
T matcher) {
T matcher,
String... values) {
return addResponse(new MockResponse()
.setResponseCode(200)
.setBody(readFixture(fixturePath)), matcher);
.setBody(replaceAndValidate(readFixture(fixturePath), values)), matcher);
}

/**
Expand All @@ -228,14 +230,38 @@ public <T extends RequestMatchersGroup> IfRequestMatches<T> addFixture(String fi
* @param statusCode The status of the mocked response.
* @param fixturePath The path of the fixture inside the fixtures fodler.
* @param matcher The {@link RequestMatchersGroup} instance to use for matching.
* @param values Optional dynamic values to be replaced in the fixture file
* @return A dsl instance {@link IfRequestMatches} for chaining
*/
public <T extends RequestMatchersGroup> IfRequestMatches<T> addFixture(int statusCode,
String fixturePath,
T matcher) {
T matcher,
String... values) {
return addResponse(new MockResponse()
.setResponseCode(statusCode)
.setBody(readFixture(fixturePath)), matcher);
.setBody(replaceAndValidate(readFixture(fixturePath), values)), matcher);
}

private MockResponse createMockResponse(int statusCode, String fixturePath, String... values) {
return new MockResponse()
.setResponseCode(statusCode)
.setBody(replaceAndValidate(readFixture(fixturePath), values));
}

private String replaceAndValidate(String fixture, String[] values) {
if (values == null) {
return fixture;
}

for (int i = 0; i < values.length; i++) {
if (!fixture.contains("{" + i + "}")) {
fail("Could not replace dynamic value at position " + i);
}

fixture = fixture.replace("{" + i + "}", values[i]);
}

return fixture;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.com.concretesolutions.requestmatcher;
package br.com.concrete.requestmatcher;

import android.support.annotation.NonNull;

Expand All @@ -7,10 +7,10 @@
import java.util.HashMap;
import java.util.Map;

import br.com.concretesolutions.requestmatcher.model.HttpMethod;
import br.com.concrete.requestmatcher.model.HttpMethod;
import okhttp3.mockwebserver.RecordedRequest;

import static br.com.concretesolutions.requestmatcher.matchers.IsMapWithSize.anEmptyMap;
import static br.com.concrete.requestmatcher.matchers.IsMapWithSize.anEmptyMap;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.hasEntry;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.com.concretesolutions.requestmatcher;
package br.com.concrete.requestmatcher;

import java.util.HashMap;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package br.com.concretesolutions.requestmatcher.exception;
package br.com.concrete.requestmatcher.exception;

import br.com.concretesolutions.requestmatcher.RequestMatchersGroup;
import br.com.concrete.requestmatcher.RequestMatchersGroup;
import okhttp3.Headers;
import okhttp3.mockwebserver.RecordedRequest;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.com.concretesolutions.requestmatcher.exception;
package br.com.concrete.requestmatcher.exception;

import java.util.Locale;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.com.concretesolutions.requestmatcher.exception;
package br.com.concrete.requestmatcher.exception;

public final class RequestAssertionException extends RuntimeException {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.com.concretesolutions.requestmatcher.matchers;
package br.com.concrete.requestmatcher.matchers;


import org.hamcrest.FeatureMatcher;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package br.com.concretesolutions.requestmatcher.model;
package br.com.concrete.requestmatcher.model;

import okhttp3.mockwebserver.RecordedRequest;

Expand Down
3 changes: 0 additions & 3 deletions requestmatcher/src/main/res/values/strings.xml

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package br.com.concrete.requestmatcher.test;


import br.com.concrete.requestmatcher.RequestMatcherRule;

public abstract class BaseTest {

protected abstract RequestMatcherRule getRequestMatcherRule();
}
Loading