Fix tests.

This commit is contained in:
Mark Nellemann 2019-10-25 16:57:15 +02:00
parent af74b269e4
commit ef472a41ee
2 changed files with 21 additions and 8 deletions

View file

@ -39,12 +39,21 @@ public class CvrApi {
private final String userAgent;
private final String authenticationToken;
private static final String BASE_URL = "https://rest.cvrapi.dk/v1/dk";
private final String BASE_URL;
public CvrApi(String userAgent, String authenticationToken) {
this(userAgent, authenticationToken, null);
}
public CvrApi(String userAgent, String authenticationToken, String baseUrl) {
this.userAgent = userAgent;
this.authenticationToken = authenticationToken;
if(baseUrl == null) {
this.BASE_URL = "https://rest.cvrapi.dk/v1/dk";
} else {
this.BASE_URL = baseUrl;
}
}

View file

@ -12,7 +12,6 @@ class CvrApiSpec extends Specification {
MockWebServer mockServer = new MockWebServer();
def setup() {
api = new CvrApi("Test User Agent", "testAuthToken")
mockServer.start()
}
@ -22,6 +21,7 @@ class CvrApiSpec extends Specification {
void "test unsuccessful parsing of JSON"() {
setup:
api = new CvrApi("Test User Agent", "testAuthToken")
def testJson = "{'foo':'bar'}"
when:
@ -33,6 +33,7 @@ class CvrApiSpec extends Specification {
void "test unsuccessful parsing of JSON w. VAT as text"() {
setup:
api = new CvrApi("Test User Agent", "testAuthToken")
def testJson = "{'vat':'abcd1234'}"
when:
@ -45,6 +46,7 @@ class CvrApiSpec extends Specification {
void "test succesful parsing company with CVR 25063864"() {
setup:
api = new CvrApi("Test User Agent", "testAuthToken")
def testFile = new File(getClass().getResource('/25063864.json').toURI())
def testJson = testFile.getText('UTF-8')
@ -63,6 +65,7 @@ class CvrApiSpec extends Specification {
void "test succesful parsing of company with CVR 15027800"() {
setup:
api = new CvrApi("Test User Agent", "testAuthToken")
def testFile = new File(getClass().getResource('/15027800.json').toURI())
def testJson = testFile.getText('UTF-8')
@ -83,7 +86,7 @@ class CvrApiSpec extends Specification {
setup:
mockServer.enqueue(new MockResponse().setBody("{}"));
HttpUrl baseUrl = mockServer.url("/v1/dk/");
api.baseUrl = baseUrl.toString()
api = new CvrApi("Test User Agent", "testAuthToken", baseUrl.toString())
when:
def jsonString = api.getCompanyJson("15027800")
@ -100,7 +103,7 @@ class CvrApiSpec extends Specification {
def testJson = testFile.getText('UTF-8')
mockServer.enqueue(new MockResponse().setBody(testJson));
HttpUrl baseUrl = mockServer.url("/v1/dk/");
api.baseUrl = baseUrl.toString()
api = new CvrApi("Test User Agent", "testAuthToken", baseUrl.toString())
when:
def company = api.getCompanyByVatNumber("25063864")
@ -114,7 +117,7 @@ class CvrApiSpec extends Specification {
setup:
mockServer.enqueue(new MockResponse().setBody("{'foo':'bar'}"));
HttpUrl baseUrl = mockServer.url("/v1/dk/");
api.baseUrl = baseUrl.toString()
api = new CvrApi("Test User Agent", "testAuthToken", baseUrl.toString())
when:
def company = api.getCompanyByVatNumber("15027800")
@ -127,7 +130,7 @@ class CvrApiSpec extends Specification {
setup:
mockServer.enqueue(new MockResponse().setBody(''));
HttpUrl baseUrl = mockServer.url("/v1/dk/");
api.baseUrl = baseUrl.toString()
api = new CvrApi("Test User Agent", "testAuthToken", baseUrl.toString())
when:
def company = api.getCompanyByVatNumber("15027800")
@ -140,6 +143,7 @@ class CvrApiSpec extends Specification {
setup:
mockServer.enqueue(new MockResponse().setResponseCode(404));
HttpUrl baseUrl = mockServer.url("/v1/dk/");
api = new CvrApi("Test User Agent", "testAuthToken", baseUrl.toString())
when:
def company = api.get(baseUrl.toString())
@ -153,7 +157,7 @@ class CvrApiSpec extends Specification {
setup:
mockServer.enqueue(new MockResponse().setResponseCode(401));
HttpUrl baseUrl = mockServer.url("/v1/dk/");
api.baseUrl = baseUrl.toString()
api = new CvrApi("Test User Agent", "testAuthToken", baseUrl.toString())
when:
def company = api.get(baseUrl.toString())
@ -167,7 +171,7 @@ class CvrApiSpec extends Specification {
setup:
mockServer.enqueue(new MockResponse().setResponseCode(405));
HttpUrl baseUrl = mockServer.url("/v1/dk/");
api.baseUrl = baseUrl.toString()
api = new CvrApi("Test User Agent", "testAuthToken", baseUrl.toString())
when:
def company = api.get(baseUrl.toString())