Skip to content

Commit

Permalink
Defend agains 1+ deployments
Browse files Browse the repository at this point in the history
Signed-off-by: Jorge Bescos Gascon <[email protected]>
  • Loading branch information
jbescos committed Oct 18, 2023
1 parent 6e7368e commit 7bfe1e7
Show file tree
Hide file tree
Showing 5 changed files with 378 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
*
* 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 io.helidon.microprofile.server;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;

import io.helidon.microprofile.testing.junit5.AddBean;
import io.helidon.microprofile.testing.junit5.HelidonTest;

import jakarta.enterprise.context.RequestScoped;
import jakarta.ws.rs.BeanParam;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DefaultValue;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.MatrixParam;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.MediaType;
import org.junit.jupiter.api.Test;

@HelidonTest
@AddBean(EntityMatrixTest.TestResource.class)
class EntityMatrixTest {

private static final String FOO = "foo";
private static final String BAR = "bar";

@Test
void defaultValueField(WebTarget target) {
String getResponse = target.path("/field").request().get(String.class);
assertThat(getResponse, is(equalTo(FOO)));
}

@Test
void customValueField(WebTarget target) {
String getResponse = target.path("/field;matrix=" + BAR).request().get(String.class);
assertThat(getResponse, is(equalTo(BAR)));
}

@Test
void defaultValueParam(WebTarget target) {
String getResponse = target.path("/param").request().get(String.class);
assertThat(getResponse, is(equalTo(FOO)));
}

@Test
void customValueParam(WebTarget target) {
String getResponse = target.path("/param;matrix=" + BAR).request().get(String.class);
assertThat(getResponse, is(equalTo(BAR)));
}

@Test
void entityMatrix(WebTarget target) {
String getResponse = target.path("/entitymatrix;param=" + BAR).request().get(String.class);
assertThat(getResponse, is(equalTo(BAR)));
}

@Path("/")
@RequestScoped
public static class TestResource {

@BeanParam
MatrixBeanParamEntity entity;

@GET
@Path("field")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String field() {
return entity.field.value;
}

@GET
@Path("param")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String param(@BeanParam MatrixBeanParamEntity entity) {
return entity.field.value;
}

@GET
@Path("entitymatrix")
public String entitymatrix(
@MatrixParam("param") ParamEntityWithFromStringAndValueOf param) {
return param.getValue();
}
}

public static class MatrixBeanParamEntity {

@DefaultValue(FOO)
@MatrixParam("matrix")
public FieldStr field;

}

public static class FieldStr {

private final String value;

public FieldStr(String value) {
this.value = value;
}

}

public static class ParamEntityWithFromStringAndValueOf {

private String value;

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public static ParamEntityWithFromStringAndValueOf valueOf(String arg) {
ParamEntityWithFromStringAndValueOf newEntity = new ParamEntityWithFromStringAndValueOf();
newEntity.value = arg;
return newEntity;
}

public static ParamEntityWithFromStringAndValueOf fromString(String arg) {
ParamEntityWithFromStringAndValueOf newEntity = new ParamEntityWithFromStringAndValueOf();
newEntity.value = arg;
return newEntity;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates.
*
* 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 io.helidon.microprofile.server;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;

import java.util.HashSet;
import java.util.Set;

import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.BeanParam;
import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DefaultValue;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.MatrixParam;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.Application;
import jakarta.ws.rs.core.MediaType;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

class JaxRsApplicationMatrixEntityTest {

private static final String FOO = "foo";
private static final String BAR = "bar";
private static Server server;
private static Client client;
private static int port;

@BeforeAll
static void beforeAll() {
server = Server.builder().addApplication(MyApplication.class).build();
server.start();
port = server.port();
client = ClientBuilder.newClient();
}

@AfterAll
static void afterAll() {
server.stop();
client.close();
}

@Test
void defaultValueField() {
String getResponse = client.target("http://localhost:" + port)
.path("/field").request().get(String.class);
assertThat(getResponse, is(equalTo(FOO)));
}

@Test
void customValueField() {
String getResponse = client.target("http://localhost:" + port)
.path("/field;matrix=" + BAR).request().get(String.class);
assertThat(getResponse, is(equalTo(BAR)));
}

@Test
void customAndDefaultValueField() {
String getResponse = client.target("http://localhost:" + port)
.path("/field").request().get(String.class);
assertThat(getResponse, is(equalTo(FOO)));
getResponse = client.target("http://localhost:" + port)
.path("/field;matrix=" + BAR).request().get(String.class);
assertThat(getResponse, is(equalTo(BAR)));
}

@Test
void defaultValueParam() {
String getResponse = client.target("http://localhost:" + port)
.path("/param").request().get(String.class);
assertThat(getResponse, is(equalTo(FOO)));
}

@Test
void customValueParam() {
String getResponse = client.target("http://localhost:" + port)
.path("/param;matrix=" + BAR).request().get(String.class);
assertThat(getResponse, is(equalTo(BAR)));
}

@ApplicationPath("/")
static class MyApplication extends Application {

@Override
public java.util.Set<java.lang.Class<?>> getClasses() {
Set<Class<?>> resources = new HashSet<Class<?>>();
resources.add(TestResource.class);
return resources;
}
}

@Path("/")
public static class TestResource {

@BeanParam
MatrixBeanParamEntity entity;

@GET
@Path("field")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String field() {
return entity.field.value;
}

@GET
@Path("param")
@Produces(MediaType.TEXT_PLAIN)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public String param(@BeanParam MatrixBeanParamEntity entity) {
return entity.field.value;
}

}

public static class MatrixBeanParamEntity {

@DefaultValue(FOO)
@MatrixParam("matrix")
public FieldStr field;

}

public static class FieldStr {

private final String value;

public FieldStr(String value) {
this.value = value;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
* <li>useBeanXmlTemplate: (Optional) defaults to true: will create the default templates/beans.xml when beans.xml is missing</li>
* <li>includeWarContextPath: (Optional) defaults to false: will include the war name as a root context.
* For example, if a example.war is deployed, the root context is going to be /example.</li>
* <li>multipleDeployments: (Optional) defaults to true: workaround for tests that unintentionally
* executes 1+ times @org.jboss.arquillian.container.test.api.Deployment</li>
* </ul>
*/
public class HelidonContainerConfiguration implements ContainerConfiguration {
Expand All @@ -52,6 +54,7 @@ public class HelidonContainerConfiguration implements ContainerConfiguration {
private boolean useParentClassloader = true;
private boolean inWebContainer = false;
private boolean useBeanXmlTemplate = true;
private boolean multipleDeployments = true;
/*
* Restful requires it, but core profile don't (because rest used to be deployed in a
* web container together with other apps and in core profile there is only one app)
Expand Down Expand Up @@ -140,6 +143,14 @@ public void setIncludeWarContextPath(boolean includeWarContextPath) {
this.includeWarContextPath = includeWarContextPath;
}

public boolean isMultipleDeployments() {
return multipleDeployments;
}

public void setMultipleDeployments(boolean multipleDeployments) {
this.multipleDeployments = multipleDeployments;
}

@Override
public void validate() throws ConfigurationException {
if ((port <= 0) || (port > Short.MAX_VALUE)) {
Expand Down
Loading

0 comments on commit 7bfe1e7

Please sign in to comment.