blob: d22664d28207e2438b21faf71afbfda8d67f69a6 [file] [log] [blame]
/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.aries.cdi.test.cases.base;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.osgi.services.HttpClientBuilderFactory;
import org.assertj.core.util.Arrays;
import org.junit.jupiter.api.Assertions;
import org.osgi.service.http.runtime.HttpServiceRuntime;
import org.osgi.service.http.runtime.dto.ServletContextDTO;
import org.osgi.service.http.runtime.dto.ServletDTO;
import org.osgi.service.http.whiteboard.HttpWhiteboardConstants;
import org.osgi.test.common.annotation.InjectService;
import org.osgi.test.common.service.ServiceAware;
public abstract class HttpBaseTestCase extends SlimBaseTestCase {
@InjectService
public HttpServiceRuntime hsr;
@InjectService
public ServiceAware<HttpServiceRuntime> hsrSA;
@InjectService
public HttpClientBuilderFactory hcbf;
public String getHttpEndpoint() {
String[] endpoints = (String[])hsrSA.getServiceReference().getProperty("osgi.http.endpoint");
if (endpoints == null || endpoints.length == 0) {
String port = (String)hsrSA.getServiceReference().getProperty("org.osgi.service.http.port");
return "http://localhost:" + port;
}
return endpoints[0];
}
public String read(HttpEntity entity) throws Exception {
if (entity == null) {
return null;
}
try (InputStream in = entity.getContent();
java.util.Scanner s = new java.util.Scanner(in)) {
s.useDelimiter("\\A");
return s.hasNext() ? s.next() : "";
}
}
public ServletDTO waitFor(String path) throws InterruptedException {
return waitFor(path, 20);
}
public ServletDTO waitFor(String path, int intervals) throws InterruptedException {
for (int j = intervals; j > 0; j--) {
for (ServletContextDTO scDTO : hsr.getRuntimeDTO().servletContextDTOs) {
if (scDTO.name.equals(HttpWhiteboardConstants.HTTP_WHITEBOARD_DEFAULT_CONTEXT_NAME)) {
for (ServletDTO sDTO : scDTO.servletDTOs) {
if (Arrays.asList(sDTO.patterns).contains(path)) {
return sDTO;
}
}
}
}
Thread.sleep(50);
}
Assertions.assertTrue(false, String.format("%s not found in time", path));
return null;
}
}