001/* 002 * (C) Copyright 2019 Nuxeo (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nour Al Kotob 018 */ 019 020package org.nuxeo.ecm.restapi.test; 021 022import static java.nio.charset.StandardCharsets.UTF_8; 023import static javax.ws.rs.core.MediaType.WILDCARD; 024import static org.apache.commons.io.FileUtils.readFileToString; 025import static org.junit.Assert.assertEquals; 026import static org.junit.Assert.assertNotNull; 027import static org.junit.Assert.assertTrue; 028import static org.nuxeo.ecm.core.bulk.io.BulkConstants.STATUS_COMMAND_ID; 029 030import java.io.File; 031import java.io.IOException; 032import java.time.Instant; 033 034import javax.inject.Inject; 035 036import org.json.JSONException; 037import org.junit.After; 038import org.junit.Before; 039import org.junit.runner.RunWith; 040import org.nuxeo.common.utils.FileUtils; 041import org.nuxeo.ecm.core.bulk.message.BulkStatus; 042import org.nuxeo.jaxrs.test.HttpClientTestRule; 043import org.nuxeo.runtime.test.runner.Features; 044import org.nuxeo.runtime.test.runner.FeaturesRunner; 045import org.nuxeo.runtime.test.runner.ServletContainerFeature; 046import org.nuxeo.runtime.test.runner.TransactionalFeature; 047import org.skyscreamer.jsonassert.JSONAssert; 048 049import com.fasterxml.jackson.databind.JsonNode; 050import com.fasterxml.jackson.databind.ObjectMapper; 051 052/** 053 * @since 11.3 054 */ 055@RunWith(FeaturesRunner.class) 056@Features(RestServerFeature.class) 057public abstract class ManagementBaseTest { 058 059 @Inject 060 protected ServletContainerFeature servletContainerFeature; 061 062 @Inject 063 protected TransactionalFeature txFeature; 064 065 protected ObjectMapper mapper = new ObjectMapper(); 066 067 protected HttpClientTestRule httpClientRule; 068 069 protected HttpClientTestRule getRule() { 070 String url = String.format("http://localhost:%d/api/v1", servletContainerFeature.getPort()); 071 return new HttpClientTestRule.Builder().url(url) 072 .accept(WILDCARD) 073 .credentials("Administrator", "Administrator") 074 .build(); 075 } 076 077 @Before 078 public void before() { 079 httpClientRule = getRule(); 080 httpClientRule.starting(); 081 } 082 083 @After 084 public void after() { 085 httpClientRule.finished(); 086 } 087 088 protected void assertJsonResponse(String actual, String expectedFile) throws IOException, JSONException { 089 File file = FileUtils.getResourceFileFromContext(expectedFile); 090 String expected = readFileToString(file, UTF_8); 091 JSONAssert.assertEquals(expected, actual, true); 092 } 093 094 protected String getBulkCommandId(JsonNode bulkStatus) { 095 return bulkStatus.get(STATUS_COMMAND_ID).asText(); 096 } 097 098 protected void assertBulkStatusScheduled(JsonNode bulkStatus) { 099 assertEquals(BulkStatus.State.SCHEDULED.name(), bulkStatus.get("state").asText()); 100 } 101 102 protected void assertBulkStatusCompleted(JsonNode bulkStatus) { 103 assertEquals(BulkStatus.State.COMPLETED.name(), bulkStatus.get("state").asText()); 104 Instant completed = Instant.parse(bulkStatus.get("completed").asText()); 105 assertTrue(completed.isBefore(Instant.now())); 106 assertNotNull(bulkStatus.get("processingMillis")); 107 } 108}