Skip to content

Commit

Permalink
Implemented updated IT for siteresources
Browse files Browse the repository at this point in the history
  • Loading branch information
zack-rma committed Dec 30, 2024
1 parent 3b2baee commit ef83c54
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package org.opendcs.odcsapi.res.it;

import java.sql.Date;
import java.time.Instant;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.core.MediaType;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.restassured.filter.log.LogDetail;
import io.restassured.filter.session.SessionFilter;
import io.restassured.path.json.JsonPath;
import io.restassured.response.ExtractableResponse;
import io.restassured.response.Response;
import org.junit.jupiter.api.AfterEach;
Expand All @@ -20,10 +19,13 @@
import org.opendcs.odcsapi.fixtures.DatabaseContextProvider;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

@Tag("integration")
@Tag("integration-opentsdb-only")
@ExtendWith(DatabaseContextProvider.class)
final class SiteResourcesIT extends BaseIT
{
Expand All @@ -39,25 +41,7 @@ void setUp() throws Exception
setUpCreds();
authenticate(sessionFilter);

ApiSite site = new ApiSite();
site.setPublicName("Pump Site");
site.setElevUnits("m");
site.setCountry("US");
site.setLatitude("0.0");
site.setLocationtype("PUMP");
site.setLongitude("0.0");
site.setActive(true);
site.setDescription("Pump located in NM, USA");
site.setElevation(56.3);
site.setLastModified(Date.from(Instant.parse("2021-01-01T15:45:10Z")));
site.setNearestCity("Albuquerque");
site.setState("NM");
site.setTimezone("UTC");
HashMap<String, String> siteNames = new HashMap<>();
siteNames.put("PUMP", "Pump Site");
site.setSitenames(siteNames);

String siteJson = OBJECT_MAPPER.writeValueAsString(site);
String siteJson = getJsonFromResource("site_setup_dto.json");

ExtractableResponse<Response> response = given()
.log().ifValidationFails(LogDetail.ALL, true)
Expand Down Expand Up @@ -105,6 +89,8 @@ void tearDown()
@TestTemplate
void testGetSiteRefs()
{
JsonPath expected = getJsonPathFromResource("site_get_refs_expected.json");

given()
.log().ifValidationFails(LogDetail.ALL, true)
.accept(MediaType.APPLICATION_JSON)
Expand All @@ -120,12 +106,17 @@ void testGetSiteRefs()
.assertThat()
.statusCode(is(HttpServletResponse.SC_OK))
.body("size()", greaterThan(0))
.body("sitenames", equalTo(expected.get("sitenames")))
.body("public-name", equalTo(expected.get("public-name")))
.body("description", equalTo(expected.get("description")))
;
}

@TestTemplate
void testGetSite()
{
JsonPath expected = getJsonPathFromResource("site_get_expected.json");

given()
.log().ifValidationFails(LogDetail.ALL, true)
.accept(MediaType.APPLICATION_JSON)
Expand All @@ -140,31 +131,34 @@ void testGetSite()
.log().ifValidationFails(LogDetail.ALL, true)
.assertThat()
.statusCode(is(HttpServletResponse.SC_OK))
.body("sitenames", equalTo(expected.get("sitenames")))
.body("publicName", equalTo(expected.get("publicName")))
.body("description", equalTo(expected.get("description")))
.body("country", equalTo(expected.get("country")))
.body("elevation", equalTo(expected.get("elevation")))
.body("elevUnits", equalTo(expected.get("elevUnits")))
.body("latitude", equalTo(expected.get("latitude")))
.body("longitude", equalTo(expected.get("longitude")))
.body("state", equalTo(expected.get("state")))
.body("properties.pumpType", equalTo(expected.get("properties.pumpType")))
.body("properties.pumpSize", equalTo(expected.get("properties.pumpSize")))
.body("properties.pumpModel", equalTo(expected.get("properties.pumpModel")))
.body("properties.pumpSerial", equalTo(expected.get("properties.pumpSerial")))
.body("properties.pumpManufacturer", equalTo(expected.get("properties.pumpManufacturer")))
.body("properties.pumpInstallDate", equalTo(expected.get("properties.pumpInstallDate").toString()))
.body("properties.pumpLastMaintDate", equalTo(expected.get("properties.pumpLastMaintDate").toString()))
.body("properties.pumpMaintInterval", equalTo(expected.get("properties.pumpMaintInterval").toString()))
.body("properties.pumpMaintNotes", equalTo(expected.get("properties.pumpMaintNotes")))
.body("properties.pumpMaintIntervalUnits", equalTo(expected.get("properties.pumpMaintIntervalUnits")))
.body("active", equalTo(expected.get("active")))
;
}

@TestTemplate
void testPostAndDeleteSite() throws Exception
{
ApiSite site = new ApiSite();
site.setPublicName("Pool Site");
site.setElevUnits("m");
site.setCountry("US");
site.setLatitude("38.56");
site.setLongitude("-121.72");
site.setLocationtype("LOCK");
site.setActive(true);
site.setDescription("LOCK Site");
site.setElevation(56.3);
site.setLastModified(Date.from(Instant.parse("2021-02-01T15:45:10Z")));
site.setNearestCity("Davis");
site.setTimezone("UTC");
site.setState("CA");
HashMap<String, String> siteNames = new HashMap<>();
siteNames.put("en", "Test Lock");
site.setSitenames(siteNames);
site.setSiteId(18817L);

ApiSite site = getDtoFromResource("site_post_delete_dto.json", ApiSite.class);
assertNotNull(site);
String siteJson = OBJECT_MAPPER.writeValueAsString(site);

ExtractableResponse<Response> response = given()
Expand All @@ -187,6 +181,10 @@ void testPostAndDeleteSite() throws Exception

Long id = response.body().jsonPath().getLong("siteId");

ApiSite returnedSite = OBJECT_MAPPER.readValue(siteJson, ApiSite.class);

assertSiteMatch(site, returnedSite);

given()
.log().ifValidationFails(LogDetail.ALL, true)
.accept(MediaType.APPLICATION_JSON)
Expand All @@ -204,4 +202,28 @@ void testPostAndDeleteSite() throws Exception
.statusCode(is(HttpServletResponse.SC_OK))
;
}

private void assertSiteMatch(ApiSite expected, ApiSite actual)
{
// Do not compare the siteId, since it is generated by the db
assertEquals(expected.getCountry(), actual.getCountry());
assertEquals(expected.getDescription(), actual.getDescription());
assertEquals(expected.getElevation(), actual.getElevation());
assertEquals(expected.getLatitude(), actual.getLatitude());
assertEquals(expected.getLongitude(), actual.getLongitude());
assertEquals(expected.getState(), actual.getState());
assertEquals(expected.getLastModified(), actual.getLastModified());
assertEquals(expected.getPublicName(), actual.getPublicName());
assertEquals(expected.getProperties(), actual.getProperties());
assertEquals(expected.getElevUnits(), actual.getElevUnits());
assertEquals(expected.getRegion(), actual.getRegion());
assertEquals(expected.getTimezone(), actual.getTimezone());
assertEquals(expected.isActive(), actual.isActive());
assertEquals(expected.getLocationType(), actual.getLocationType());
assertEquals(expected.getNearestCity(), actual.getNearestCity());
for (Map.Entry<Object, Object> entry : expected.getProperties().entrySet())
{
assertEquals(entry.getValue(), actual.getProperties().get(entry.getKey()));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"siteId" : 1,
"sitenames" : {
"PUMP" : "Pump Site"
},
"description" : "Pump located in NM, USA",
"latitude" : "0.0",
"longitude" : "0.0",
"elevation" : 56.3,
"elevUnits" : "m",
"nearestCity" : "Albuquerque",
"timezone" : "UTC",
"state" : "NM",
"country" : "US",
"region" : null,
"active" : true,
"locationType" : "PUMP",
"publicName" : "Pump Site",
"properties" : {
"pumpType" : "Centrifugal",
"pumpSize" : "1000 gpm",
"pumpModel" : "ABC-123",
"pumpSerial" : "123456",
"pumpManufacturer" : "PumpCo",
"pumpInstallDate" : 1612194310000,
"pumpLastMaintDate" : 1612194310000,
"pumpMaintInterval" : 365,
"pumpMaintIntervalUnits" : "days",
"pumpMaintNotes" : "This is a test pump"
},
"lastModified" : 1609515910000
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[
{
"siteId": 4,
"sitenames":
{
"PUMP": "Pump Site"
},
"publicName": "Pump Site",
"description": "Pump located in NM, USA"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"siteId" : 18817,
"sitenames" : {
"LOCK" : "Test Lock"
},
"description" : "LOCK Site",
"latitude" : "38.56",
"longitude" : "-121.72",
"elevation" : 56.3,
"elevUnits" : "m",
"nearestCity" : "Davis",
"timezone" : "UTC",
"state" : "CA",
"country" : "US",
"region" : null,
"active" : true,
"locationType" : "LOCK",
"publicName" : "Pool Site",
"properties" : {
"pumpType" : "Centrifugal",
"pumpSize" : "1000 gpm",
"pumpModel" : "ABC-123",
"pumpSerial" : "123456",
"pumpManufacturer" : "PumpCo",
"pumpInstallDate" : 161219431000,
"pumpLastMaintDate" : 1612194510000,
"pumpMaintInterval" : 365,
"pumpMaintIntervalUnits" : "days",
"pumpMaintNotes" : "This is a test pump"
},
"lastModified" : 1612194310000
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"siteId" : null,
"sitenames" : {
"PUMP" : "Pump Site"
},
"description" : "Pump located in NM, USA",
"latitude" : "0.0",
"longitude" : "0.0",
"elevation" : 56.3,
"elevUnits" : "m",
"nearestCity" : "Albuquerque",
"timezone" : "UTC",
"state" : "NM",
"country" : "US",
"region" : null,
"active" : true,
"locationType" : "PUMP",
"publicName" : "Pump Site",
"properties" : {
"pumpType" : "Centrifugal",
"pumpSize" : "1000 gpm",
"pumpModel" : "ABC-123",
"pumpSerial" : "123456",
"pumpManufacturer" : "PumpCo",
"pumpInstallDate" : 1612194310000,
"pumpLastMaintDate" : 1612194310000,
"pumpMaintInterval" : 365,
"pumpMaintIntervalUnits" : "days",
"pumpMaintNotes" : "This is a test pump"
},
"lastModified" : 1609515910000
}

0 comments on commit ef83c54

Please sign in to comment.