Skip to content

Commit

Permalink
[AB2D-6150] increase ab2d-properties-client test coverage (#386)
Browse files Browse the repository at this point in the history
* testing ci

* increasing PropertiesClientImpl code cov

* git restore

* null case

* cleanup

* adds another test

* refactor

* add assertDoesNotThrow

* increase patch version
  • Loading branch information
coilysiren authored Jun 7, 2024
1 parent 55fe2cd commit 32ab0d2
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
public class PropertiesClientImpl implements PropertiesClient {
@Getter
private String url = "http://localhost:8060";

@Getter
private String configFileName = "application.properties";

private static final String JSON = "application/json";
private static final String ACCEPT = "accept";

Expand All @@ -28,7 +32,7 @@ public PropertiesClientImpl() {
}
PropertiesConfiguration config = new PropertiesConfiguration();
try {
config.load("application.properties");
config.load(getConfigFileName());
String configUrl = config.getString("properties.service.url");
if (StringUtils.isNotEmpty(configUrl)) {
url = configUrl;
Expand All @@ -50,7 +54,7 @@ public List<Property> getAllProperties() {
.asObject(new GenericType<List<Property>>() {
});
List<Property> values = response.getBody();
if (values == null) {
if (values == null) {
throw new PropertyNotFoundException("Cannot find the list of properties");
}
return values;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,26 @@
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

class ProperyServiceMockTest {
class PropertiesClientImplMock extends PropertiesClientImpl {
class PropertiesClientImplMockEnv extends PropertiesClientImpl {
@Override
public String getFromEnvironment() {
return "http://localhost:8065";
}
}

class PropertiesClientImplMockConfig extends PropertiesClientImpl {
@Override
public String getConfigFileName() {
return "does.not.exist";
}
}

@Test
void testIt() {
PropertiesClientImpl impl = new PropertiesClientImpl();
Expand All @@ -37,12 +45,14 @@ void testIt() {
List<Property> propertiesToReturn = List.of(new Property("a.key", "a.value"), new Property("b.key", "b.value"));
JSONArray jsonArray = new JSONArray(propertiesToReturn);
stubFor(get(urlEqualTo("/properties")).willReturn(aResponse().withBody(jsonArray.toString())));
System.out.println(propertiesToReturn.get(0).toString());
stubFor(get(urlEqualTo("/properties/a.key")).willReturn(aResponse().withBody("{ \"key\": \"a.key\", \"value\": \"a.value\"}")));
stubFor(get(urlEqualTo("/properties/a.key"))
.willReturn(aResponse().withBody("{ \"key\": \"a.key\", \"value\": \"a.value\"}")));
stubFor(post(urlEqualTo("/properties"))
.willReturn(aResponse().withBody("{ \"key\": \"one\", \"value\": \"two\"}")));
stubFor(get(urlEqualTo("/properties/one")).willReturn(aResponse().withBody("{ \"key\": \"one\", \"value\": \"two\"}")));
stubFor(get(urlEqualTo("/properties/bogus")).willReturn(aResponse().withStatus(404).withBody("{ \"key\": \"null\", \"value\": \"null\"}")));
stubFor(get(urlEqualTo("/properties/one"))
.willReturn(aResponse().withBody("{ \"key\": \"one\", \"value\": \"two\"}")));
stubFor(get(urlEqualTo("/properties/bogus"))
.willReturn(aResponse().withStatus(404).withBody("{ \"key\": \"null\", \"value\": \"null\"}")));
stubFor(delete(urlEqualTo("/properties/one")).willReturn(aResponse().withBody("true")));

List<Property> properties = impl.getAllProperties();
Expand All @@ -69,11 +79,17 @@ void testIt() {
}

@Test
void testImpl() {
PropertiesClientImplMock mock = new PropertiesClientImplMock();
void testMockEnv() {
PropertiesClientImplMockEnv mock = new PropertiesClientImplMockEnv();
assertEquals("http://localhost:8065", mock.getUrl());
}

@Test
void testMockConfig() {
PropertiesClientImplMockConfig mock = new PropertiesClientImplMockConfig();
assertEquals("http://localhost:8060", mock.getUrl());
}

@Test
void testErrors() {
int port = 8065;
Expand All @@ -87,11 +103,6 @@ void testErrors() {
stubFor(get(urlEqualTo("/properties/a.key")).willReturn(aResponse().withStatus(520)));
stubFor(post(urlEqualTo("/properties")).willReturn((aResponse().withStatus(404))));
stubFor(delete(urlEqualTo("/properties/one")).willReturn(aResponse().withBody("false")));
//stubFor(post(urlEqualTo("/properties"))
// .willReturn(aResponse().withBody("{ \"key\": \"one\", \"value\": \"two\"}")));
//stubFor(get(urlEqualTo("/properties/one")).willReturn(aResponse().withBody("{ \"key\": \"one\", \"value\": \"two\"}")));
//stubFor(get(urlEqualTo("/properties/bogus")).willReturn(aResponse().withStatus(404).withBody("{ \"key\": \"null\", \"value\": \"null\"}")));
//stubFor(delete(urlEqualTo("/properties/one")).willReturn(aResponse().withBody("true")));

assertThrows(PropertyNotFoundException.class, () -> impl.getAllProperties());

Expand All @@ -104,4 +115,38 @@ void testErrors() {

wireMockServer.stop();
}

@Test
void testDeleteCases() {
int port = 8066;
PropertiesClientImpl impl = new PropertiesClientImpl("http://localhost:" + port);
WireMockServer wireMockServer = new WireMockServer(port);
wireMockServer.start();

configureFor("localhost", port);

// test running delete with false
stubFor(delete(urlEqualTo("/properties/one")).willReturn(aResponse().withBody("false")));
assertThrows(PropertyNotFoundException.class, () -> impl.deleteProperty("one"));

// test running delete with true
stubFor(delete(urlEqualTo("/properties/one")).willReturn(aResponse().withBody("true")));
assertDoesNotThrow(() -> impl.deleteProperty("one"));

// test running delete with null (presumably)
stubFor(delete(urlEqualTo("/properties/one")).willReturn(aResponse()));
assertThrows(PropertyNotFoundException.class, () -> impl.deleteProperty("one"));

wireMockServer.stop();
}

@Test
void testErrorsWithoutMock() {
PropertiesClientImpl impl = new PropertiesClientImpl();
assertThrows(PropertyNotFoundException.class, () -> impl.getAllProperties());
assertThrows(PropertyNotFoundException.class, () -> impl.getProperty("a.key"));
assertThrows(PropertyNotFoundException.class, () -> impl.setProperty("one", "two"));
assertThrows(PropertyNotFoundException.class, () -> impl.deleteProperty("one"));
}

}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ ext {
aggregatorVersion='1.3.4'
filtersVersion='1.9.4'
eventClientVersion='1.12.7'
propertiesClientVersion='1.2.4'
propertiesClientVersion='1.2.5'
contractClientVersion='1.2.4'
snsClientVersion='0.2.4'

Expand Down

0 comments on commit 32ab0d2

Please sign in to comment.