Skip to content

Commit

Permalink
Move from Junit to TestNG (#61)
Browse files Browse the repository at this point in the history
* tck with testNG
Co-authored-by: brunobat <brunobat@gmail.com>
Co-authored-by: Andrew Rouse <anrouse@uk.ibm.com>
  • Loading branch information
yasmin-aumeeruddy committed Oct 12, 2022
1 parent cf69a44 commit 68f452f
Show file tree
Hide file tree
Showing 18 changed files with 558 additions and 302 deletions.
5 changes: 5 additions & 0 deletions .gitignore
@@ -1,3 +1,8 @@
target/
.vscode/
*.DS_Store
.project
.classpath
.settings
.checkstyle
bin/
1 change: 0 additions & 1 deletion pom.xml
Expand Up @@ -60,7 +60,6 @@

<dependencyManagement>
<dependencies>
<!-- This BOM includes the opentelemetry-bom and the opentelemetry-bom-alpha -->
<dependency>
<groupId>io.opentelemetry.instrumentation</groupId>
<artifactId>opentelemetry-instrumentation-bom-alpha</artifactId>
Expand Down
1 change: 0 additions & 1 deletion tracing/pom.xml
Expand Up @@ -35,7 +35,6 @@
<version.mp.rest.client>3.0.1</version.mp.rest.client>
<version.microprofile-config-api>3.0.2</version.microprofile-config-api>
<version.junit-jupiter>5.9.1</version.junit-jupiter>
<version.rest-assured>5.2.0</version.rest-assured>
<version.awaitility>4.2.0</version.awaitility>
</properties>

Expand Down
20 changes: 6 additions & 14 deletions tracing/tck/pom.xml
Expand Up @@ -65,8 +65,12 @@
<artifactId>opentelemetry-semconv</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.junit5</groupId>
<artifactId>arquillian-junit5-container</artifactId>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.arquillian.testng</groupId>
<artifactId>arquillian-testng-container</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.shrinkwrap</groupId>
Expand All @@ -80,18 +84,6 @@
<groupId>org.jboss.shrinkwrap.resolver</groupId>
<artifactId>shrinkwrap-resolver-api-maven</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>${version.junit-jupiter}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>${version.rest-assured}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
Expand Down
@@ -0,0 +1,77 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* 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.eclipse.microprofile.telemetry.tracing.tck;

import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import org.jboss.arquillian.test.api.ArquillianResource;

/**
* A really basic client for doing Http requests
* <p>
* For use when we don't want to use JAX-RS client or something else which has integration with telemetry
*/
public class BasicHttpClient {

private URI baseUri;

/**
* @param baseUrl
* The base URL. Any path requested through this client will be appended to this URL. This should usually
* be a URL injected using {@link ArquillianResource}
*/
public BasicHttpClient(URL baseUrl) {
try {
baseUri = baseUrl.toURI();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}

/**
* Makes a GET request to a path and returns the response code
*
* @param path
* the path to request, relative to the baseUrl
* @return the response code
*/
public int get(String path) {
if (path.startsWith("/")) {
path = path.substring(1);
}
try {
URL spanUrl = baseUri.resolve(path).toURL();
HttpURLConnection connection = (HttpURLConnection) spanUrl.openConnection();
try {
return connection.getResponseCode();
} catch (Exception e) {
throw new RuntimeException("Exception retriving " + spanUrl, e);
} finally {
connection.disconnect();
}
} catch (Exception e) {
throw new RuntimeException("Exception retriving path " + path, e);
}
}

}
@@ -0,0 +1,51 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* 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.eclipse.microprofile.telemetry.tracing.tck;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.jboss.shrinkwrap.api.asset.Asset;

public class ConfigAsset implements Asset {

private Properties properties = new Properties();

@Override
public InputStream openStream() {
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
properties.store(os, null);
return new ByteArrayInputStream(os.toByteArray());
} catch (IOException e) {
// Shouldn't happen since we're only using in memory streams
throw new RuntimeException("Unexpected error saving properties", e);
}
}

public ConfigAsset add(String key, String value) {
properties.put(key, value);
return this;
}

}
Expand Up @@ -24,13 +24,12 @@

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit5.ArquillianExtension;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.arquillian.testng.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.testng.Assert;
import org.testng.annotations.Test;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
Expand All @@ -43,8 +42,7 @@
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

@ExtendWith(ArquillianExtension.class)
class TestApplication {
class TestApplication extends Arquillian {
@ArquillianResource
private URL url;

Expand All @@ -59,7 +57,7 @@ public void rest() {
String uri = url.toExternalForm() + "rest";
WebTarget echoEndpointTarget = ClientBuilder.newClient().target(uri);
Response response = echoEndpointTarget.request(MediaType.TEXT_PLAIN).get();
Assertions.assertEquals(response.getStatus(), HttpURLConnection.HTTP_OK);
Assert.assertEquals(response.getStatus(), HttpURLConnection.HTTP_OK);
}

@ApplicationPath("/rest")
Expand Down
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* 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.eclipse.microprofile.telemetry.tracing.tck;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;

public class TestLibraries {

public static final JavaArchive AWAITILITY_LIB = ShrinkWrap.create(JavaArchive.class, "awaitility.jar")
.addPackages(true, "org.awaitility", "org.hamcrest");

private TestLibraries() {
}
}
Expand Up @@ -21,23 +21,23 @@
package org.eclipse.microprofile.telemetry.tracing.tck.cdi;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit5.ArquillianExtension;
import org.jboss.arquillian.testng.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.testng.Assert;
import org.testng.annotations.Test;

import io.opentelemetry.api.trace.Tracer;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;

@ExtendWith(ArquillianExtension.class)
class TracerTest {
class TracerTest extends Arquillian {

@Deployment
public static WebArchive createDeployment() {
return ShrinkWrap.create(WebArchive.class)
.addClasses(TracerBean.class)
.addAsResource(new StringAsset("otel.experimental.sdk.enabled=true"),
"META-INF/microprofile-config.properties");
}
Expand All @@ -47,7 +47,7 @@ public static WebArchive createDeployment() {

@Test
void tracer() {
Assertions.assertNotNull(tracerBean.getTracer());
Assert.assertNotNull(tracerBean.getTracer());
}

@ApplicationScoped
Expand Down
Expand Up @@ -29,7 +29,7 @@
import java.util.stream.Collectors;

import org.awaitility.Awaitility;
import org.junit.jupiter.api.Assertions;
import org.testng.Assert;

import io.opentelemetry.sdk.common.CompletableResultCode;
import io.opentelemetry.sdk.trace.data.SpanData;
Expand All @@ -54,8 +54,8 @@ public List<SpanData> getFinishedSpanItems(int spanCount) {
}

public void assertSpanCount(int spanCount) {
Awaitility.await().atMost(10, SECONDS)
.untilAsserted(() -> Assertions.assertEquals(spanCount, finishedSpanItems.size()));
Awaitility.await().pollDelay(3, SECONDS).atMost(10, SECONDS)
.untilAsserted(() -> Assert.assertEquals(finishedSpanItems.size(), spanCount));
}

public void reset() {
Expand Down

0 comments on commit 68f452f

Please sign in to comment.