Skip to content

Commit

Permalink
Merge pull request #3691 from ChristinaDsl/RESTEASY-3029
Browse files Browse the repository at this point in the history
[RESTEASY-3029] Allow @Separator annotation at fields and methods
  • Loading branch information
jamezp committed Aug 2, 2023
2 parents 0c9fe65 + 61ad2ba commit bab1baa
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* Determines a separator for String representations of multivalued parameters.
*/
@Target({ ElementType.PARAMETER })
@Target({ ElementType.PARAMETER, ElementType.FIELD, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface Separator {
String value() default "";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* JBoss, Home of Professional Open Source.
*
* Copyright 2023 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* 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.jboss.resteasy.test.core.spi;

import java.io.IOException;

import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.core.Response;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.resteasy.spi.HttpResponseCodes;
import org.jboss.resteasy.test.core.spi.resource.SeparatorAnnotationAsFieldTargetEndPoint;
import org.jboss.resteasy.test.core.spi.resource.SeparatorAnnotationAsMethodTargetEndPoint;
import org.jboss.resteasy.test.core.spi.resource.SeparatorAnnotationAsParameterTargetEndPoint;
import org.jboss.resteasy.test.core.spi.resource.SeparatorAnnotationBeanParam;
import org.jboss.resteasy.utils.PortProviderUtil;
import org.jboss.resteasy.utils.TestUtil;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
* @tpSubChapter Separator Annotation
* @tpChapter Integration tests
* @tpTestCaseDetails Testing Separator annotation, for each of its targets, see RESTEASY-3029
* @tpSince RESTEasy 6.3.0.Alpha1-SNAPSHOT
*/
@RunWith(Arquillian.class)
@RunAsClient
public class SeparatorAnnotationTest {

@Deployment
public static Archive<?> deploy() {
WebArchive war = TestUtil.prepareArchive(SeparatorAnnotationTest.class.getSimpleName())
.addClass(SeparatorAnnotationTest.class);
return TestUtil.finishContainerPrepare(war, null,
SeparatorAnnotationAsParameterTargetEndPoint.class,
SeparatorAnnotationAsFieldTargetEndPoint.class,
SeparatorAnnotationAsMethodTargetEndPoint.class,
SeparatorAnnotationBeanParam.class);
}

private String generateURL(String path) {
return PortProviderUtil.generateURL(path, SeparatorAnnotationTest.class.getSimpleName());
}

@Test
public void testSeparatorAsParameterTarget() throws IOException {
try (Client client = ClientBuilder.newClient()) {
Response response = client
.target(generateURL("/separator/parameter/1,2,3"))
.request()
.get();

Assert.assertEquals(HttpResponseCodes.SC_OK, response.getStatus());
Assert.assertEquals("This is your sentence:123", response.readEntity(String.class));
}
}

@Test
public void testSeparatorAsFieldTarget() throws IOException {
try (Client client = ClientBuilder.newClient()) {
Response response = client
.target(generateURL("/separator/field/1,2,3"))
.request()
.get();

Assert.assertEquals(HttpResponseCodes.SC_OK, response.getStatus());
Assert.assertEquals("This is your sentence:123", response.readEntity(String.class));
}
}

@Test
public void testSeparatorAsMethodTarget() throws IOException {
try (Client client = ClientBuilder.newClient()) {
Response response = client
.target(generateURL("/separator/method/1,2,3"))
.request()
.get();

Assert.assertEquals(HttpResponseCodes.SC_OK, response.getStatus());
Assert.assertEquals("This is your sentence:123", response.readEntity(String.class));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* JBoss, Home of Professional Open Source.
*
* Copyright 2023 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* 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.jboss.resteasy.test.core.spi.resource;

import java.util.List;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;

import org.jboss.resteasy.annotations.Separator;

@Path("/separator/field/{ids}")
public class SeparatorAnnotationAsFieldTargetEndPoint {
@PathParam("ids")
@Separator(",")
private List<String> ids;

@GET
@Produces("text/plain")
public String getSentence() {
return "This is your sentence:" + String.join("", ids);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* JBoss, Home of Professional Open Source.
*
* Copyright 2023 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* 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.jboss.resteasy.test.core.spi.resource;

import jakarta.ws.rs.BeanParam;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;

@Path("/separator/method/{ids}")
public class SeparatorAnnotationAsMethodTargetEndPoint {
@GET
@Produces("text/plain")
public String getSentence(@BeanParam SeparatorAnnotationBeanParam resource) {
return resource.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* JBoss, Home of Professional Open Source.
*
* Copyright 2023 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* 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.jboss.resteasy.test.core.spi.resource;

import java.util.List;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;

import org.jboss.resteasy.annotations.Separator;

@Path("/separator/parameter/{ids}")
public class SeparatorAnnotationAsParameterTargetEndPoint {
@GET
@Produces("text/plain")
public String getSentence(@PathParam("ids") @Separator(",") List<String> ids) {
return "This is your sentence:" + String.join("", ids);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* JBoss, Home of Professional Open Source.
*
* Copyright 2023 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* 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.jboss.resteasy.test.core.spi.resource;

import java.util.List;

import jakarta.ws.rs.PathParam;

import org.jboss.resteasy.annotations.Separator;

public class SeparatorAnnotationBeanParam {
private List<String> ids;

public List<String> getIds() {
return ids;
}

@PathParam("ids")
@Separator(",")
public void setIds(List<String> ids) {
this.ids = ids;
}

@Override
public String toString() {
return "This is your sentence:" + String.join("", ids);
}
}

0 comments on commit bab1baa

Please sign in to comment.