|
29 | 29 | import static cwms.cda.data.dao.DaoTest.getDslContext;
|
30 | 30 | import static cwms.cda.security.KeyAccessManager.AUTH_HEADER;
|
31 | 31 | import static io.restassured.RestAssured.given;
|
| 32 | +import static java.lang.String.format; |
32 | 33 | import static org.hamcrest.Matchers.equalTo;
|
33 | 34 | import static org.hamcrest.Matchers.is;
|
34 | 35 |
|
| 36 | +import cwms.cda.api.errors.NotFoundException; |
35 | 37 | import cwms.cda.data.dao.LookupTypeDao;
|
36 | 38 | import cwms.cda.data.dto.LookupType;
|
37 | 39 | import cwms.cda.formatters.Formats;
|
38 | 40 | import cwms.cda.formatters.json.JsonV1;
|
| 41 | +import cwms.cda.helpers.DTOMatch; |
39 | 42 | import fixtures.CwmsDataApiSetupCallback;
|
40 | 43 | import fixtures.TestAccounts;
|
41 | 44 | import io.restassured.filter.log.LogDetail;
|
42 | 45 | import mil.army.usace.hec.test.database.CwmsDatabaseContainer;
|
43 | 46 | import org.jooq.DSLContext;
|
| 47 | +import org.junit.jupiter.api.AfterEach; |
44 | 48 | import org.junit.jupiter.api.Tag;
|
45 | 49 | import org.junit.jupiter.api.Test;
|
46 | 50 | import javax.servlet.http.HttpServletResponse;
|
47 | 51 | import java.sql.SQLException;
|
| 52 | +import java.util.Arrays; |
| 53 | +import java.util.List; |
| 54 | +import java.util.logging.Level; |
| 55 | +import java.util.logging.Logger; |
48 | 56 |
|
49 | 57 |
|
50 | 58 | @Tag("integration")
|
51 |
| -class WaterContractTypeCreateControllerTestIT extends DataApiTestIT { |
| 59 | +class WaterContractTypeControllerTestIT extends DataApiTestIT { |
52 | 60 | private static final String OFFICE_ID = "SWT";
|
53 | 61 | private static final LookupType CONTRACT_TYPE;
|
| 62 | + public static final Logger LOGGER = |
| 63 | + Logger.getLogger(WaterContractTypeControllerTestIT.class.getName()); |
54 | 64 | static {
|
55 | 65 | CONTRACT_TYPE = new LookupType.Builder().withActive(true).withOfficeId(OFFICE_ID)
|
56 | 66 | .withDisplayValue("TEST Contract Type").withTooltip("TEST LOOKUP").build();
|
57 | 67 | }
|
58 | 68 |
|
| 69 | + @AfterEach |
| 70 | + void cleanup() throws SQLException { |
| 71 | + cleanupType(); |
| 72 | + } |
| 73 | + |
59 | 74 | @Test
|
60 | 75 | void test_create_get_WaterContractType() throws Exception {
|
61 | 76 | // Test Structure
|
62 | 77 | // 1) Create a WaterContractType
|
63 | 78 | // 2) Get the WaterContractType, assert it exists
|
64 |
| - // 3) Cleanup |
65 | 79 |
|
66 | 80 | TestAccounts.KeyUser user = TestAccounts.KeyUser.SWT_NORMAL;
|
67 | 81 | String json = JsonV1.buildObjectMapper().writeValueAsString(CONTRACT_TYPE);
|
@@ -101,18 +115,101 @@ void test_create_get_WaterContractType() throws Exception {
|
101 | 115 | .body("[0].tooltip", equalTo(CONTRACT_TYPE.getTooltip()))
|
102 | 116 | .body("[0].active", equalTo(CONTRACT_TYPE.getActive()))
|
103 | 117 | ;
|
| 118 | + } |
104 | 119 |
|
105 |
| - // cleanup |
106 |
| - cleanupType(); |
| 120 | + @Test |
| 121 | + void test_store_delete_WaterContractType() throws Exception { |
| 122 | + // Test Structure |
| 123 | + // 1) Create a WaterContractType |
| 124 | + // 2) Get the WaterContractType, assert it exists |
| 125 | + // 3) Delete the WaterContractType |
| 126 | + // 4) Get the WaterContractType, assert it does not exist |
| 127 | + |
| 128 | + TestAccounts.KeyUser user = TestAccounts.KeyUser.SWT_NORMAL; |
| 129 | + String json = JsonV1.buildObjectMapper().writeValueAsString(CONTRACT_TYPE); |
| 130 | + |
| 131 | + // create water contract type |
| 132 | + given() |
| 133 | + .log().ifValidationFails(LogDetail.ALL, true) |
| 134 | + .contentType(Formats.JSONV1) |
| 135 | + .body(json) |
| 136 | + .queryParam("fail-if-exists", false) |
| 137 | + .header(AUTH_HEADER, user.toHeaderValue()) |
| 138 | + .when() |
| 139 | + .redirects().follow(true) |
| 140 | + .redirects().max(3) |
| 141 | + .post("/projects/" + OFFICE_ID + "/contract-types") |
| 142 | + .then() |
| 143 | + .log().ifValidationFails(LogDetail.ALL, true) |
| 144 | + .assertThat() |
| 145 | + .statusCode(is(HttpServletResponse.SC_CREATED)) |
| 146 | + ; |
| 147 | + |
| 148 | + // get water contract type and assert that it exists |
| 149 | + given() |
| 150 | + .log().ifValidationFails(LogDetail.ALL, true) |
| 151 | + .contentType(Formats.JSONV1) |
| 152 | + .header(AUTH_HEADER, user.toHeaderValue()) |
| 153 | + .when() |
| 154 | + .redirects().follow(true) |
| 155 | + .redirects().max(3) |
| 156 | + .get("/projects/" + OFFICE_ID + "/contract-types") |
| 157 | + .then() |
| 158 | + .log().ifValidationFails(LogDetail.ALL, true) |
| 159 | + .assertThat() |
| 160 | + .statusCode(is(HttpServletResponse.SC_OK)) |
| 161 | + .body("[0].office-id", equalTo(OFFICE_ID)) |
| 162 | + .body("[0].display-value", equalTo(CONTRACT_TYPE.getDisplayValue())) |
| 163 | + .body("[0].tooltip", equalTo(CONTRACT_TYPE.getTooltip())) |
| 164 | + .body("[0].active", equalTo(CONTRACT_TYPE.getActive())) |
| 165 | + ; |
| 166 | + |
| 167 | + // delete water contract type |
| 168 | + given() |
| 169 | + .log().ifValidationFails(LogDetail.ALL, true) |
| 170 | + .contentType(Formats.JSONV1) |
| 171 | + .header(AUTH_HEADER, user.toHeaderValue()) |
| 172 | + .when() |
| 173 | + .redirects().follow(true) |
| 174 | + .redirects().max(3) |
| 175 | + .delete("/projects/" + OFFICE_ID + "/contract-types/" + CONTRACT_TYPE.getDisplayValue()) |
| 176 | + .then() |
| 177 | + .log().ifValidationFails(LogDetail.ALL, true) |
| 178 | + .assertThat() |
| 179 | + .statusCode(is(HttpServletResponse.SC_NO_CONTENT)) |
| 180 | + ; |
| 181 | + |
| 182 | + // get water contract type and assert that it does not exist |
| 183 | + List<LookupType> results = Arrays.asList(given() |
| 184 | + .log().ifValidationFails(LogDetail.ALL, true) |
| 185 | + .contentType(Formats.JSONV1) |
| 186 | + .header(AUTH_HEADER, user.toHeaderValue()) |
| 187 | + .when() |
| 188 | + .redirects().follow(true) |
| 189 | + .redirects().max(3) |
| 190 | + .get("/projects/" + OFFICE_ID + "/contract-types") |
| 191 | + .then() |
| 192 | + .log().ifValidationFails(LogDetail.ALL, true) |
| 193 | + .assertThat() |
| 194 | + .statusCode(is(HttpServletResponse.SC_OK)) |
| 195 | + .extract().body().as(LookupType[].class)) |
| 196 | + ; |
| 197 | + DTOMatch.assertDoesNotContainDto(results, CONTRACT_TYPE, |
| 198 | + (i, s) -> i.getDisplayValue().equalsIgnoreCase(s.getDisplayValue()), "Contract Type not deleted"); |
107 | 199 | }
|
108 | 200 |
|
109 | 201 | private void cleanupType() throws SQLException {
|
110 | 202 | CwmsDatabaseContainer<?> databaseLink = CwmsDataApiSetupCallback.getDatabaseLink();
|
111 | 203 | databaseLink.connection(c -> {
|
112 | 204 | DSLContext ctx = getDslContext(c, OFFICE_ID);
|
113 | 205 | LookupTypeDao lookupTypeDao = new LookupTypeDao(ctx);
|
114 |
| - lookupTypeDao.deleteLookupType("AT_WS_CONTRACT_TYPE", "WS_CONTRACT_TYPE", |
115 |
| - CONTRACT_TYPE.getOfficeId(), CONTRACT_TYPE.getDisplayValue()); |
| 206 | + try { |
| 207 | + |
| 208 | + lookupTypeDao.deleteLookupType("AT_WS_CONTRACT_TYPE", "WS_CONTRACT_TYPE", |
| 209 | + CONTRACT_TYPE.getOfficeId(), CONTRACT_TYPE.getDisplayValue()); |
| 210 | + } catch (NotFoundException e) { |
| 211 | + LOGGER.log(Level.CONFIG, format("Cleanup failed to delete lookup type: %s", e.getMessage())); |
| 212 | + } |
116 | 213 | }, CwmsDataApiSetupCallback.getWebUser());
|
117 | 214 | }
|
118 | 215 | }
|
0 commit comments