libcvrapi/src/test/groovy/biz/nellemann/libcvrapi/CvrApiSpec.groovy

182 lines
4.1 KiB
Groovy
Raw Normal View History

2019-08-17 17:53:11 +00:00
package biz.nellemann.libcvrapi
2019-08-23 11:11:33 +00:00
import okhttp3.HttpUrl
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import com.google.gson.JsonSyntaxException;
2019-08-17 17:53:11 +00:00
import spock.lang.Specification
class CvrApiSpec extends Specification {
CvrApi api
2019-08-23 11:11:33 +00:00
MockWebServer mockServer = new MockWebServer();
2019-08-17 17:53:11 +00:00
def setup() {
api = new CvrApi("Test User Agent", "testAuthToken")
2019-08-23 11:11:33 +00:00
mockServer.start();
}
2019-08-17 17:53:11 +00:00
2019-08-23 11:11:33 +00:00
def cleanup() {
mockServer.shutdown()
}
2019-08-17 17:53:11 +00:00
2019-08-23 11:11:33 +00:00
void "test unsuccessful parsing of JSON"() {
setup:
def testJson = "{'foo':'bar'}"
2019-08-17 17:53:11 +00:00
2019-08-23 11:11:33 +00:00
when:
def company = api.parseJsonIntoCompany(testJson)
then:
company.vat == null
}
void "test unsuccessful parsing of JSON w. VAT as text"() {
setup:
def testJson = "{'vat':'abcd1234'}"
when:
api.parseJsonIntoCompany(testJson)
then:
thrown Exception
}
2019-10-22 19:12:44 +00:00
void "test successful parsing company with CVR 25063864"() {
2019-08-23 11:11:33 +00:00
setup:
2019-08-23 06:18:13 +00:00
def testFile = new File(getClass().getResource('/25063864.json').toURI())
def testJson = testFile.getText('UTF-8')
2019-08-23 11:11:33 +00:00
when:
2019-08-23 06:18:13 +00:00
def company = api.parseJsonIntoCompany(testJson)
2019-08-17 17:53:11 +00:00
then:
2019-08-19 18:58:01 +00:00
company != null
2019-08-17 17:53:11 +00:00
company.vat == 25063864
company.life.name == 'AGILLIC A/S'
2019-08-19 08:00:01 +00:00
company.secondarynames.contains('Wavetech A/S')
2019-08-19 18:58:01 +00:00
company.info.employment.amountEmployeesLow == 50
company.info.employment.amountEmployeesHigh == 99
2019-08-17 17:53:11 +00:00
}
2019-10-22 19:12:44 +00:00
void "test successful parsing of company with CVR 15027800"() {
2019-08-23 06:18:13 +00:00
2019-08-23 11:11:33 +00:00
setup:
2019-08-23 06:18:13 +00:00
def testFile = new File(getClass().getResource('/15027800.json').toURI())
def testJson = testFile.getText('UTF-8')
2019-08-23 11:11:33 +00:00
when:
2019-08-23 06:18:13 +00:00
def company = api.parseJsonIntoCompany(testJson)
then:
company != null
company.vat == 15027800
company.life.name == 'FREJA TRANSPORT & LOGISTICS A/S'
company.secondarynames.contains('KT TRANSPORT A/S')
company.info.employment.amountEmployeesLow == 200
company.info.employment.amountEmployeesHigh == 499
}
2019-10-22 19:12:44 +00:00
void "test successful HTTP GET company with CVR 15027800 as JSON"() {
2019-08-23 11:11:33 +00:00
setup:
mockServer.enqueue(new MockResponse().setBody("{}"));
HttpUrl baseUrl = mockServer.url("/v1/dk/");
api.baseUrl = baseUrl.toString()
when:
def jsonString = api.getCompanyJson("15027800")
then:
jsonString != null
jsonString == "{}"
}
void "test we can HTTP GET company with CVR 25063864 deserialized into Company"() {
setup:
def testFile = new File(getClass().getResource('/25063864.json').toURI())
def testJson = testFile.getText('UTF-8')
mockServer.enqueue(new MockResponse().setBody(testJson));
HttpUrl baseUrl = mockServer.url("/v1/dk/");
api.baseUrl = baseUrl.toString()
when:
def company = api.getCompanyByVatNumber("25063864")
then:
company != null
company.vat == 25063864
}
void "test wrong json"() {
setup:
mockServer.enqueue(new MockResponse().setBody("{'foo':'bar'}"));
HttpUrl baseUrl = mockServer.url("/v1/dk/");
api.baseUrl = baseUrl.toString()
when:
def company = api.getCompanyByVatNumber("15027800")
then:
company.vat == null
}
void "test empty json"() {
setup:
mockServer.enqueue(new MockResponse().setBody(''));
HttpUrl baseUrl = mockServer.url("/v1/dk/");
api.baseUrl = baseUrl.toString()
when:
def company = api.getCompanyByVatNumber("15027800")
then:
company == null
}
void "test HTTP 404 reply"() {
setup:
mockServer.enqueue(new MockResponse().setResponseCode(404));
HttpUrl baseUrl = mockServer.url("/v1/dk/");
when:
def company = api.get(baseUrl.toString())
then:
Exception ex = thrown()
ex.message == 'Not Found'
}
void "test HTTP 401 reply"() {
setup:
mockServer.enqueue(new MockResponse().setResponseCode(401));
HttpUrl baseUrl = mockServer.url("/v1/dk/");
api.baseUrl = baseUrl.toString()
when:
def company = api.get(baseUrl.toString())
then:
Exception ex = thrown()
ex.message == 'Access Denied'
}
void "test other HTTP reply"() {
setup:
mockServer.enqueue(new MockResponse().setResponseCode(405));
HttpUrl baseUrl = mockServer.url("/v1/dk/");
api.baseUrl = baseUrl.toString()
when:
def company = api.get(baseUrl.toString())
then:
Exception ex = thrown()
}
2019-08-23 06:18:13 +00:00
2019-08-23 11:11:33 +00:00
// TODO: Test 401, 404, timeouts, gson deserialization errors
2019-08-17 17:53:11 +00:00
}