More tests to improve code coverage.

This commit is contained in:
Mark Nellemann 2019-08-23 13:11:33 +02:00
parent 231c0e7ac0
commit 8e49a66b14
3 changed files with 149 additions and 12 deletions

View file

@ -95,7 +95,7 @@ jacocoTestCoverageVerification {
limit {
counter = 'LINE'
value = 'COVEREDRATIO'
minimum = 0.5
minimum = 0.7
}
excludes = [
'biz.nellemann.libcvrapi.pojo.*'

View file

@ -41,6 +41,8 @@ public class CvrApi {
private final String userAgent;
private final String authenticationToken;
protected String baseUrl = "https://rest.cvrapi.dk/v1/dk";
public CvrApi(String userAgent, String authenticationToken) {
this.userAgent = userAgent;
@ -49,7 +51,7 @@ public class CvrApi {
}
private String get(String url) throws IOException, Exception {
protected String get(String url) throws IOException, Exception {
String credential = Credentials.basic(authenticationToken, "");
@ -61,9 +63,13 @@ public class CvrApi {
case 200:
return response.body().string();
case 401:
throw new Exception("Access denied");
log.warn("get() - 401 - Access Denied");
throw new Exception("Access Denied");
case 404:
log.warn("get() - 404 - Not Found");
throw new Exception("Not Found");
default:
throw new Exception("Unknown error, status code: " + response.code());
throw new Exception("get() - Unknown Error - status code: " + response.code());
}
}
@ -77,7 +83,7 @@ public class CvrApi {
* @throws IOException
*/
protected String getCompanyJson(String vatNumber) throws IOException, Exception {
String response = get("https://rest.cvrapi.dk/v1/dk/company/" + vatNumber);
String response = get(baseUrl + "/company/" + vatNumber);
log.debug("getCompanyJson() response: " + response);
return response;
}

View file

@ -1,22 +1,54 @@
package biz.nellemann.libcvrapi
import okhttp3.HttpUrl
import okhttp3.mockwebserver.MockResponse
import okhttp3.mockwebserver.MockWebServer
import com.google.gson.JsonSyntaxException;
import spock.lang.Specification
class CvrApiSpec extends Specification {
CvrApi api
MockWebServer mockServer = new MockWebServer();
def setup() {
api = new CvrApi("Test User Agent", "testAuthToken")
mockServer.start();
}
def cleanup() { }
def cleanup() {
mockServer.shutdown()
}
void "test we can parse company with CVR 25063864"() {
void "test unsuccessful parsing of JSON"() {
setup:
def testJson = "{'foo':'bar'}"
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
}
void "test succesful parsing company with CVR 25063864"() {
setup:
def testFile = new File(getClass().getResource('/25063864.json').toURI())
def testJson = testFile.getText('UTF-8')
when:
def company = api.parseJsonIntoCompany(testJson)
then:
@ -28,11 +60,13 @@ class CvrApiSpec extends Specification {
company.info.employment.amountEmployeesHigh == 99
}
void "test we can parse company with CVR 15027800"() {
void "test succesful parsing of company with CVR 15027800"() {
when:
setup:
def testFile = new File(getClass().getResource('/15027800.json').toURI())
def testJson = testFile.getText('UTF-8')
when:
def company = api.parseJsonIntoCompany(testJson)
then:
@ -44,7 +78,104 @@ class CvrApiSpec extends Specification {
company.info.employment.amountEmployeesHigh == 499
}
// TODO: Get better code coverage in tests, eg. by mocking okHttp interface - but how ?
void "test succesful HTTP GET company with CVR 15027800 as JSON"() {
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()
}
// TODO: Test 401, 404, timeouts, gson deserialization errors
}