diff --git a/.gitignore b/.gitignore index ed4d3bc..2819bfc 100755 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ target .classpath .project .settings +.metadata .idea *.ipr *.iml diff --git a/pom.xml b/pom.xml index 5b2e2fb..8cbba88 100644 --- a/pom.xml +++ b/pom.xml @@ -39,6 +39,7 @@ rest-trivial rest-trivial-kotlin rest + rest-junit5 servlet-trivial mw_bundle_config servlet-vaadin-v08 diff --git a/rest-junit5/pom.xml b/rest-junit5/pom.xml new file mode 100644 index 0000000..b5076cf --- /dev/null +++ b/rest-junit5/pom.xml @@ -0,0 +1,59 @@ + + + + org.apache.meecrowave + meecrowave-examples + 1.2.8-SNAPSHOT + + 4.0.0 + + samples-rest-junit5 + REST - JUnit 5 + + + + + org.apache.meecrowave + meecrowave-specs-api + ${meecrowave.version} + + + org.apache.meecrowave + meecrowave-core + ${meecrowave.version} + + + + + org.junit.jupiter + junit-jupiter-engine + ${junit.jupiter.version} + test + + + org.apache.meecrowave + meecrowave-junit + ${meecrowave.version} + test + + + + + + + + org.apache.meecrowave + meecrowave-maven-plugin + ${meecrowave.version} + + + + + + + diff --git a/rest-junit5/src/main/java/com/superbiz/configuration/Defaults.java b/rest-junit5/src/main/java/com/superbiz/configuration/Defaults.java new file mode 100644 index 0000000..2053c19 --- /dev/null +++ b/rest-junit5/src/main/java/com/superbiz/configuration/Defaults.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 com.superbiz.configuration; + +import org.apache.meecrowave.runner.Cli; +import org.apache.meecrowave.runner.cli.CliOption; + +public class Defaults implements Cli.Options { + @CliOption(name = "app-default-name", description = "The name used to say hello if not set") + private String name = "default value"; + + public String getName() { + return name; + } +} diff --git a/rest-junit5/src/main/java/com/superbiz/configuration/DefaultsProducer.java b/rest-junit5/src/main/java/com/superbiz/configuration/DefaultsProducer.java new file mode 100644 index 0000000..6e40cf0 --- /dev/null +++ b/rest-junit5/src/main/java/com/superbiz/configuration/DefaultsProducer.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 com.superbiz.configuration; + +import org.apache.meecrowave.Meecrowave; + +import javax.enterprise.context.ApplicationScoped; +import javax.enterprise.context.Dependent; +import javax.enterprise.inject.Produces; +import javax.inject.Inject; + +@Dependent +public class DefaultsProducer { + @Inject + private Meecrowave.Builder builder; + + @Produces + @ApplicationScoped + public Defaults defaults() { + return builder.getExtension(Defaults.class); + } +} diff --git a/rest-junit5/src/main/java/com/superbiz/jaxrs/HelloApplication.java b/rest-junit5/src/main/java/com/superbiz/jaxrs/HelloApplication.java new file mode 100644 index 0000000..813b6b9 --- /dev/null +++ b/rest-junit5/src/main/java/com/superbiz/jaxrs/HelloApplication.java @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 com.superbiz.jaxrs; + +import javax.enterprise.context.Dependent; +import javax.ws.rs.ApplicationPath; +import javax.ws.rs.core.Application; + +@Dependent +@ApplicationPath("api") +public class HelloApplication extends Application { +} diff --git a/rest-junit5/src/main/java/com/superbiz/jaxrs/HelloEndpoint.java b/rest-junit5/src/main/java/com/superbiz/jaxrs/HelloEndpoint.java new file mode 100644 index 0000000..3c3fe57 --- /dev/null +++ b/rest-junit5/src/main/java/com/superbiz/jaxrs/HelloEndpoint.java @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 com.superbiz.jaxrs; + +import com.superbiz.configuration.Defaults; + +import javax.enterprise.context.ApplicationScoped; +import javax.inject.Inject; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.MediaType; + +import static java.util.Optional.ofNullable; + +@Path("hello") +@ApplicationScoped +public class HelloEndpoint { + @Inject + private Defaults defaults; + + @GET + @Produces(MediaType.APPLICATION_JSON) + public Hello sayHi(@QueryParam("name") final String name) { + return new Hello(ofNullable(name) + .orElse(defaults.getName())); + } + + public static class Hello { + private String name; + + public Hello() { + // no-op + } + + private Hello(final String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public void setName(final String name) { + this.name = name; + } + } +} diff --git a/rest-junit5/src/test/java/com/superbiz/jaxrs/HelloEndpointTest.java b/rest-junit5/src/test/java/com/superbiz/jaxrs/HelloEndpointTest.java new file mode 100644 index 0000000..37062f3 --- /dev/null +++ b/rest-junit5/src/test/java/com/superbiz/jaxrs/HelloEndpointTest.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 com.superbiz.jaxrs; + +import static javax.ws.rs.core.MediaType.APPLICATION_JSON_TYPE; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.ClientBuilder; + +import org.apache.meecrowave.Meecrowave; +import org.apache.meecrowave.junit5.MeecrowaveConfig; +import org.apache.meecrowave.testing.ConfigurationInject; +import org.junit.jupiter.api.Test; + +@MeecrowaveConfig +public class HelloEndpointTest { + @ConfigurationInject + private Meecrowave.Builder configuration; + + @Test + public void hello() { + final Client client = ClientBuilder.newClient(); + try { + assertEquals("me", client.target("http://localhost:" + configuration.getHttpPort()) + .path("api/hello") + .queryParam("name", "me") + .request(APPLICATION_JSON_TYPE) + .get(HelloEndpoint.Hello.class) + .getName()); + } finally { + client.close(); + } + } +}