From ef472a41ee0542dcd9770bdfadbe41b8cf434cf2 Mon Sep 17 00:00:00 2001 From: Mark Nellemann Date: Fri, 25 Oct 2019 16:57:15 +0200 Subject: [PATCH] Fix tests. --- .../java/biz/nellemann/libcvrapi/CvrApi.java | 11 ++++++++++- .../biz/nellemann/libcvrapi/CvrApiSpec.groovy | 18 +++++++++++------- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/main/java/biz/nellemann/libcvrapi/CvrApi.java b/src/main/java/biz/nellemann/libcvrapi/CvrApi.java index 0fd6317..ed6c43a 100644 --- a/src/main/java/biz/nellemann/libcvrapi/CvrApi.java +++ b/src/main/java/biz/nellemann/libcvrapi/CvrApi.java @@ -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; + } } diff --git a/src/test/groovy/biz/nellemann/libcvrapi/CvrApiSpec.groovy b/src/test/groovy/biz/nellemann/libcvrapi/CvrApiSpec.groovy index c3de92e..46b1f03 100644 --- a/src/test/groovy/biz/nellemann/libcvrapi/CvrApiSpec.groovy +++ b/src/test/groovy/biz/nellemann/libcvrapi/CvrApiSpec.groovy @@ -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())