You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by jl...@apache.org on 2018/12/12 16:02:31 UTC
[07/11] tomee git commit: TOMEE-2304 - removed jpa and greeting
service and test. Added JWT rolesAllowed tests.
TOMEE-2304 - removed jpa and greeting service and test. Added JWT rolesAllowed tests.
Project: http://git-wip-us.apache.org/repos/asf/tomee/repo
Commit: http://git-wip-us.apache.org/repos/asf/tomee/commit/2e460433
Tree: http://git-wip-us.apache.org/repos/asf/tomee/tree/2e460433
Diff: http://git-wip-us.apache.org/repos/asf/tomee/diff/2e460433
Branch: refs/heads/master
Commit: 2e460433869df496fdf904b4820092d733870f95
Parents: 7cba202
Author: CesarHernandezGt <cf...@gmail.com>
Authored: Tue Dec 11 00:15:09 2018 -0600
Committer: CesarHernandezGt <cf...@gmail.com>
Committed: Tue Dec 11 00:15:09 2018 -0600
----------------------------------------------------------------------
.../main/java/org/superbiz/moviefun/Movie.java | 58 ++-----
.../java/org/superbiz/moviefun/MoviesBean.java | 78 +++------
.../org/superbiz/moviefun/rest/LoadRest.java | 42 -----
.../org/superbiz/moviefun/rest/MoviesRest.java | 164 +++++++++++--------
.../java/org/superbiz/rest/GreetingService.java | 41 -----
.../src/main/resources/META-INF/persistence.xml | 31 ----
examples/mp-rest-jwt/src/main/tomee/tomee.xml | 27 ---
.../java/org/superbiz/moviefun/MoviesTest.java | 56 +++++--
.../org/superbiz/rest/GreetingServiceTest.java | 61 -------
.../mp-rest-jwt/src/test/resources/Token1.json | 9 +-
.../mp-rest-jwt/src/test/resources/Token2.json | 14 +-
11 files changed, 183 insertions(+), 398 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tomee/blob/2e460433/examples/mp-rest-jwt/src/main/java/org/superbiz/moviefun/Movie.java
----------------------------------------------------------------------
diff --git a/examples/mp-rest-jwt/src/main/java/org/superbiz/moviefun/Movie.java b/examples/mp-rest-jwt/src/main/java/org/superbiz/moviefun/Movie.java
index 4e067bb..1e21a1d 100644
--- a/examples/mp-rest-jwt/src/main/java/org/superbiz/moviefun/Movie.java
+++ b/examples/mp-rest-jwt/src/main/java/org/superbiz/moviefun/Movie.java
@@ -16,47 +16,30 @@
*/
package org.superbiz.moviefun;
-import javax.persistence.Entity;
-import javax.persistence.GeneratedValue;
-import javax.persistence.GenerationType;
-import javax.persistence.Id;
import javax.xml.bind.annotation.XmlRootElement;
-@Entity
@XmlRootElement(name = "movie")
public class Movie {
- @Id
- @GeneratedValue(strategy = GenerationType.AUTO)
- private long id;
+ private int id;
private String director;
private String title;
- private int year;
- private String genre;
- private int rating;
-
public Movie() {
}
- public Movie(String title, String director, String genre, int rating, int year) {
+ public Movie(int id, String director, String title) {
+ this.id = id;
this.director = director;
this.title = title;
- this.year = year;
- this.genre = genre;
- this.rating = rating;
- }
- public Movie(String director, String title, int year) {
- this.director = director;
- this.title = title;
- this.year = year;
}
- public long getId() {
+
+ public int getId() {
return id;
}
- public void setId(long id) {
+ public void setId(int id) {
this.id = id;
}
@@ -76,27 +59,12 @@ public class Movie {
this.title = title;
}
- public int getYear() {
- return year;
- }
-
- public void setYear(int year) {
- this.year = year;
- }
-
- public String getGenre() {
- return genre;
- }
-
- public void setGenre(String genre) {
- this.genre = genre;
- }
-
- public int getRating() {
- return rating;
- }
-
- public void setRating(int rating) {
- this.rating = rating;
+ @Override
+ public String toString() {
+ return "Movie{" +
+ "id=" + id +
+ ", director='" + director + '\'' +
+ ", title='" + title + '\'' +
+ '}';
}
}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/tomee/blob/2e460433/examples/mp-rest-jwt/src/main/java/org/superbiz/moviefun/MoviesBean.java
----------------------------------------------------------------------
diff --git a/examples/mp-rest-jwt/src/main/java/org/superbiz/moviefun/MoviesBean.java b/examples/mp-rest-jwt/src/main/java/org/superbiz/moviefun/MoviesBean.java
index 2580c9f..3f28097 100644
--- a/examples/mp-rest-jwt/src/main/java/org/superbiz/moviefun/MoviesBean.java
+++ b/examples/mp-rest-jwt/src/main/java/org/superbiz/moviefun/MoviesBean.java
@@ -16,76 +16,46 @@
*/
package org.superbiz.moviefun;
+import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
-import javax.persistence.EntityManager;
-import javax.persistence.PersistenceContext;
-import javax.persistence.TypedQuery;
-import javax.persistence.criteria.CriteriaBuilder;
-import javax.persistence.criteria.CriteriaQuery;
-import javax.persistence.criteria.Path;
-import javax.persistence.criteria.Predicate;
-import javax.persistence.criteria.Root;
-import javax.persistence.metamodel.EntityType;
+import javax.enterprise.context.ApplicationScoped;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
import java.util.List;
-@Stateless
+@ApplicationScoped
public class MoviesBean {
+
+ private HashMap<Integer,Movie> MovieStore;
- @PersistenceContext(unitName = "movie-unit")
- private EntityManager entityManager;
- public Movie find(Long id) {
- return entityManager.find(Movie.class, id);
+ @PostConstruct
+ public void MovieBean() {
+ MovieStore = new HashMap();
}
- public void addMovie(Movie movie) {
- entityManager.persist(movie);
+ public void addMovie(Movie newMovie) {
+ MovieStore.put(newMovie.getId(), newMovie);
}
- public void editMovie(Movie movie) {
- entityManager.merge(movie);
+ public void deleteMovie(int id) {
+ MovieStore.remove(id);
}
- public void deleteMovie(long id) {
- Movie movie = entityManager.find(Movie.class, id);
- entityManager.remove(movie);
+ public void updateMovie(Movie updatedMovie) {
+ MovieStore.put(updatedMovie.getId(),updatedMovie);
}
- public List<Movie> getMovies(Integer firstResult, Integer maxResults, String field, String searchTerm) {
- CriteriaBuilder qb = entityManager.getCriteriaBuilder();
- CriteriaQuery<Movie> cq = qb.createQuery(Movie.class);
- Root<Movie> root = cq.from(Movie.class);
- EntityType<Movie> type = entityManager.getMetamodel().entity(Movie.class);
- if (field != null && searchTerm != null && !"".equals(field.trim()) && !"".equals(searchTerm.trim())) {
- Path<String> path = root.get(type.getDeclaredSingularAttribute(field.trim(), String.class));
- Predicate condition = qb.like(path, "%" + searchTerm.trim() + "%");
- cq.where(condition);
- }
- TypedQuery<Movie> q = entityManager.createQuery(cq);
- if (maxResults != null) {
- q.setMaxResults(maxResults);
- }
- if (firstResult != null) {
- q.setFirstResult(firstResult);
- }
- return q.getResultList();
+ public Movie getMovie(int id) {
+ return MovieStore.get(id);
}
- public int count(String field, String searchTerm) {
- CriteriaBuilder qb = entityManager.getCriteriaBuilder();
- CriteriaQuery<Long> cq = qb.createQuery(Long.class);
- Root<Movie> root = cq.from(Movie.class);
- EntityType<Movie> type = entityManager.getMetamodel().entity(Movie.class);
- cq.select(qb.count(root));
- if (field != null && searchTerm != null && !"".equals(field.trim()) && !"".equals(searchTerm.trim())) {
- Path<String> path = root.get(type.getDeclaredSingularAttribute(field.trim(), String.class));
- Predicate condition = qb.like(path, "%" + searchTerm.trim() + "%");
- cq.where(condition);
- }
- return entityManager.createQuery(cq).getSingleResult().intValue();
- }
+ public List getMovies() {
+ Collection<Movie> Movies = MovieStore.values();
+ return new ArrayList<Movie>(Movies);
- public void clean() {
- entityManager.createQuery("delete from Movie").executeUpdate();
}
+
+
}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/tomee/blob/2e460433/examples/mp-rest-jwt/src/main/java/org/superbiz/moviefun/rest/LoadRest.java
----------------------------------------------------------------------
diff --git a/examples/mp-rest-jwt/src/main/java/org/superbiz/moviefun/rest/LoadRest.java b/examples/mp-rest-jwt/src/main/java/org/superbiz/moviefun/rest/LoadRest.java
deleted file mode 100644
index 6969221..0000000
--- a/examples/mp-rest-jwt/src/main/java/org/superbiz/moviefun/rest/LoadRest.java
+++ /dev/null
@@ -1,42 +0,0 @@
-/**
- * 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
- * <p/>
- * http://www.apache.org/licenses/LICENSE-2.0
- * <p/>
- * 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.superbiz.moviefun.rest;
-
-import org.superbiz.moviefun.Movie;
-import org.superbiz.moviefun.MoviesBean;
-
-import javax.ejb.EJB;
-import javax.ws.rs.POST;
-import javax.ws.rs.Path;
-
-@Path("load")
-public class LoadRest {
- @EJB
- private MoviesBean moviesBean;
-
- @POST
- public void load() {
- moviesBean.addMovie(new Movie("Wedding Crashers", "David Dobkin", "Comedy", 7, 2005));
- moviesBean.addMovie(new Movie("Starsky & Hutch", "Todd Phillips", "Action", 6, 2004));
- moviesBean.addMovie(new Movie("Shanghai Knights", "David Dobkin", "Action", 6, 2003));
- moviesBean.addMovie(new Movie("I-Spy", "Betty Thomas", "Adventure", 5, 2002));
- moviesBean.addMovie(new Movie("The Royal Tenenbaums", "Wes Anderson", "Comedy", 8, 2001));
- moviesBean.addMovie(new Movie("Zoolander", "Ben Stiller", "Comedy", 6, 2001));
- moviesBean.addMovie(new Movie("Shanghai Noon", "Tom Dey", "Comedy", 7, 2000));
- }
-
-}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/tomee/blob/2e460433/examples/mp-rest-jwt/src/main/java/org/superbiz/moviefun/rest/MoviesRest.java
----------------------------------------------------------------------
diff --git a/examples/mp-rest-jwt/src/main/java/org/superbiz/moviefun/rest/MoviesRest.java b/examples/mp-rest-jwt/src/main/java/org/superbiz/moviefun/rest/MoviesRest.java
index 7020864..1ded167 100644
--- a/examples/mp-rest-jwt/src/main/java/org/superbiz/moviefun/rest/MoviesRest.java
+++ b/examples/mp-rest-jwt/src/main/java/org/superbiz/moviefun/rest/MoviesRest.java
@@ -16,98 +16,128 @@
*/
package org.superbiz.moviefun.rest;
-import org.eclipse.microprofile.jwt.Claim;
-import org.eclipse.microprofile.jwt.ClaimValue;
-import org.eclipse.microprofile.jwt.JsonWebToken;
import org.superbiz.moviefun.Movie;
import org.superbiz.moviefun.MoviesBean;
import javax.annotation.security.RolesAllowed;
-import javax.ejb.EJB;
import javax.inject.Inject;
-import javax.ws.rs.Consumes;
-import javax.ws.rs.DELETE;
-import javax.ws.rs.GET;
-import javax.ws.rs.POST;
-import javax.ws.rs.PUT;
-import javax.ws.rs.Path;
-import javax.ws.rs.PathParam;
-import javax.ws.rs.Produces;
-import javax.ws.rs.QueryParam;
-import javax.ws.rs.core.Context;
+import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
-import javax.ws.rs.core.SecurityContext;
import java.util.List;
-@Path("movies")
-@Produces({"application/json"})
+@Path("cinema")
+@Produces(MediaType.APPLICATION_JSON)
+@Consumes(MediaType.APPLICATION_JSON)
public class MoviesRest {
- @EJB
- private MoviesBean service;
-
- @Inject
- @Claim("raw_token")
- private ClaimValue<String> rawToken;
-
- @Inject
- @Claim("iss")
- private ClaimValue<String> issuer;
-
@Inject
- @Claim("jti")
- private ClaimValue<String> jti;
-
- @Inject
- private JsonWebToken jwtPrincipal;
-
- @Context
- private SecurityContext securityContext;
+ private MoviesBean moviesBean;
@GET
- @Path("{id}")
- public Movie find(@PathParam("id") Long id) {
- return service.find(id);
+ @Produces(MediaType.TEXT_PLAIN)
+ public String status() {
+ return "ok";
}
- @GET
- public List<Movie> getMovies(@QueryParam("first") Integer first, @QueryParam("max") Integer max,
- @QueryParam("field") String field, @QueryParam("searchTerm") String searchTerm) {
- return service.getMovies(first, max, field, searchTerm);
+ @POST
+ @Path("/movies")
+ @RolesAllowed("crud")
+ @Produces(MediaType.APPLICATION_JSON)
+ @Consumes(MediaType.APPLICATION_JSON)
+ public void addMovie(Movie newMovie) {
+ moviesBean.addMovie(newMovie);
}
- @POST
- @Consumes("application/json")
- @RolesAllowed("create")
- public Movie addMovie(Movie movie) {
- service.addMovie(movie);
- return movie;
+ @DELETE
+ @Path("/movies/{id}")
+ @RolesAllowed("read-only")
+ public void deleteMovie(@PathParam("id") int id) {
+ moviesBean.deleteMovie(id);
}
@PUT
- @Path("{id}")
- @Consumes("application/json")
- @RolesAllowed("update")
- public Movie editMovie(
- @PathParam("id") final long id,
- Movie movie
- ) {
- service.editMovie(movie);
- return movie;
+ @Path("/movies")
+ public void updateMovie(Movie updatedMovie) {
+ moviesBean.updateMovie(updatedMovie);
}
- @DELETE
- @Path("{id}")
- @RolesAllowed("delete")
- public void deleteMovie(@PathParam("id") long id) {
- service.deleteMovie(id);
+ @GET
+ @Path("/movies/{id}")
+ @RolesAllowed({"read-only","crud"})
+ public Movie getMovie(@PathParam("id") int id) {
+ return moviesBean.getMovie(id);
}
@GET
- @Path("count")
- @Produces(MediaType.TEXT_PLAIN)
- public int count(@QueryParam("field") String field, @QueryParam("searchTerm") String searchTerm) {
- return service.count(field, searchTerm);
+ @Path("/movies")
+ @RolesAllowed({"crud", "read-only"})
+ public List<Movie> getListOfMovies() {
+ return moviesBean.getMovies();
}
+
+// @Inject
+// @Claim("raw_token")
+// private ClaimValue<String> rawToken;
+//
+// @Inject
+// @Claim("iss")
+// private ClaimValue<String> issuer;
+//
+// @Inject
+// @Claim("jti")
+// private ClaimValue<String> jti;
+//
+// @Inject
+// private JsonWebToken jwtPrincipal;
+//
+// @Context
+// private SecurityContext securityContext;
+//
+// @GET
+// @Path("{id}")
+// public Movie find(@PathParam("id") Long id) {
+// return service.find(id);
+// }
+//
+// @GET
+// public List<Movie> getMovies(@QueryParam("first") Integer first, @QueryParam("max") Integer max,
+// @QueryParam("field") String field, @QueryParam("searchTerm") String searchTerm) {
+// return service.getMovies(first, max, field, searchTerm);
+// }
+//
+// @POST
+// @Consumes("application/json")
+// @RolesAllowed("create")
+// public Movie addMovie(Movie movie) {
+// service.addMovie(movie);
+// return movie;
+// }
+//
+// @PUT
+// @Path("{id}")
+// @Consumes("application/json")
+// @RolesAllowed("update")
+// public Movie editMovie(
+// @PathParam("id") final long id,
+// Movie movie
+// ) {
+// service.editMovie(movie);
+// return movie;
+// }
+//
+// @DELETE
+// @Path("{id}")
+// @RolesAllowed("delete")
+// public void deleteMovie(@PathParam("id") long id) {
+// service.deleteMovie(id);
+// }
+//
+// @GET
+// @Path("count")
+// @Produces(MediaType.TEXT_PLAIN)
+// public int count(@QueryParam("field") String field, @QueryParam("searchTerm") String searchTerm) {
+// return service.count(field, searchTerm);
+// }
+
}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/tomee/blob/2e460433/examples/mp-rest-jwt/src/main/java/org/superbiz/rest/GreetingService.java
----------------------------------------------------------------------
diff --git a/examples/mp-rest-jwt/src/main/java/org/superbiz/rest/GreetingService.java b/examples/mp-rest-jwt/src/main/java/org/superbiz/rest/GreetingService.java
deleted file mode 100644
index eb4434e..0000000
--- a/examples/mp-rest-jwt/src/main/java/org/superbiz/rest/GreetingService.java
+++ /dev/null
@@ -1,41 +0,0 @@
-/**
- * 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
- * <p/>
- * http://www.apache.org/licenses/LICENSE-2.0
- * <p/>
- * 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.superbiz.rest;
-
-import javax.ws.rs.Consumes;
-import javax.ws.rs.GET;
-import javax.ws.rs.POST;
-import javax.ws.rs.Path;
-import javax.ws.rs.Produces;
-import javax.ws.rs.core.MediaType;
-import java.util.Locale;
-
-@Path("/greeting")
-@Produces(MediaType.APPLICATION_JSON)
-@Consumes(MediaType.APPLICATION_JSON)
-public class GreetingService {
-
- @GET
- public String message() {
- return "Hi Microprofile JWT!";
- }
-
- @POST
- public String lowerCase(final String message) {
- return message.toLowerCase(Locale.ENGLISH);
- }
-}
http://git-wip-us.apache.org/repos/asf/tomee/blob/2e460433/examples/mp-rest-jwt/src/main/resources/META-INF/persistence.xml
----------------------------------------------------------------------
diff --git a/examples/mp-rest-jwt/src/main/resources/META-INF/persistence.xml b/examples/mp-rest-jwt/src/main/resources/META-INF/persistence.xml
deleted file mode 100644
index ec38aaa..0000000
--- a/examples/mp-rest-jwt/src/main/resources/META-INF/persistence.xml
+++ /dev/null
@@ -1,31 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
-
- 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.
--->
-<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
- <persistence-unit name="movie-unit">
- <jta-data-source>movieDatabase</jta-data-source>
- <non-jta-data-source>movieDatabaseUnmanaged</non-jta-data-source>
- <class>org.superbiz.moviefun.Movie</class>
-
- <properties>
- <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
- </properties>
- </persistence-unit>
-</persistence>
http://git-wip-us.apache.org/repos/asf/tomee/blob/2e460433/examples/mp-rest-jwt/src/main/tomee/tomee.xml
----------------------------------------------------------------------
diff --git a/examples/mp-rest-jwt/src/main/tomee/tomee.xml b/examples/mp-rest-jwt/src/main/tomee/tomee.xml
deleted file mode 100644
index f25d5f8..0000000
--- a/examples/mp-rest-jwt/src/main/tomee/tomee.xml
+++ /dev/null
@@ -1,27 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<!--
- 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.
--->
-<tomee>
- <Resource id="HSQLDB Database" type="DataSource">
- JdbcDriver org.hsqldb.jdbcDriver
- JdbcUrl jdbc:hsqldb:file:target/db/moviefun
- UserName sa
- Password
- </Resource>
-</tomee>
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/tomee/blob/2e460433/examples/mp-rest-jwt/src/test/java/org/superbiz/moviefun/MoviesTest.java
----------------------------------------------------------------------
diff --git a/examples/mp-rest-jwt/src/test/java/org/superbiz/moviefun/MoviesTest.java b/examples/mp-rest-jwt/src/test/java/org/superbiz/moviefun/MoviesTest.java
index b18e64e..ea622b8 100644
--- a/examples/mp-rest-jwt/src/test/java/org/superbiz/moviefun/MoviesTest.java
+++ b/examples/mp-rest-jwt/src/test/java/org/superbiz/moviefun/MoviesTest.java
@@ -23,22 +23,22 @@ import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.ShrinkWrap;
-import org.jboss.shrinkwrap.api.asset.ClassLoaderAsset;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.superbiz.moviefun.rest.ApplicationConfig;
-import org.superbiz.moviefun.rest.LoadRest;
import org.superbiz.moviefun.rest.MoviesMPJWTConfigurationProvider;
import org.superbiz.moviefun.rest.MoviesRest;
-import org.superbiz.rest.GreetingService;
+import javax.ws.rs.core.Response;
import java.net.URL;
import java.util.Collection;
import java.util.HashMap;
+import java.util.logging.Logger;
import static java.util.Collections.singletonList;
+import static org.junit.Assert.assertTrue;
@RunWith(Arquillian.class)
public class MoviesTest {
@@ -46,11 +46,10 @@ public class MoviesTest {
@Deployment(testable = false)
public static WebArchive createDeployment() {
final WebArchive webArchive = ShrinkWrap.create(WebArchive.class, "test.war")
- .addClasses(Movie.class, MoviesBean.class, MoviesTest.class, LoadRest.class)
- .addClasses(MoviesRest.class, GreetingService.class, ApplicationConfig.class)
+ .addClasses(Movie.class, MoviesBean.class, MoviesTest.class)
+ .addClasses(MoviesRest.class, ApplicationConfig.class)
.addClass(MoviesMPJWTConfigurationProvider.class)
- .addAsWebInfResource(new StringAsset("<beans/>"), "beans.xml")
- .addAsResource(new ClassLoaderAsset("META-INF/persistence.xml"), "META-INF/persistence.xml");
+ .addAsWebInfResource(new StringAsset("<beans/>"), "beans.xml");
System.out.println(webArchive.toString(true));
@@ -60,29 +59,50 @@ public class MoviesTest {
@ArquillianResource
private URL base;
+
+ private final static Logger LOGGER = Logger.getLogger(MoviesTest.class.getName());
+
@Test
- public void sthg() throws Exception {
+ public void movieRestTest() throws Exception {
final WebClient webClient = WebClient
.create(base.toExternalForm(), singletonList(new JohnzonProvider<>()), singletonList(new LoggingFeature()), null);
- webClient
- .reset()
- .path("/rest/greeting/")
- .get(String.class);
+
+ //Testing rest endpoint deployment (GET without security header)
+ String responsePayload = webClient.reset().path("/rest/cinema/").get(String.class);
+ LOGGER.info("responsePayload = " + responsePayload);
+ assertTrue(responsePayload.equalsIgnoreCase("ok"));
+
+ //POST (Using token1.json with group of claims: [CRUD])
+ Movie newMovie = new Movie(1,"David Dobkin","Wedding Crashers");
+ Response response = webClient.reset().path("/rest/cinema/movies").header("Content-Type","application/json").header("Authorization", "Bearer " + token(1)).post(newMovie);
+ LOGGER.info("responseCode = " + response.getStatus());
+ assertTrue(response.getStatus() == 204);
+
+
+
+ //GET movies (Using token2.json with group of claims: [read-only])
final Collection<? extends Movie> movies = webClient
.reset()
- .path("/rest/movies/")
- .header("Authorization", "Bearer " + token())
+ .path("/rest/cinema/movies")
+ .header("Content-Type","application/json")
+ .header("Authorization", "Bearer " + token(1))
.getCollection(Movie.class);
-
- System.out.println(movies);
+ LOGGER.info(movies.toString());
+ assertTrue(movies.size() == 1);
}
- private String token() throws Exception {
+
+
+ private String token(int token_type) throws Exception {
HashMap<String, Long> timeClaims = new HashMap<>();
- return TokenUtils.generateTokenString("/Token1.json", null, timeClaims);
+ if(token_type==1){
+ return TokenUtils.generateTokenString("/Token1.json", null, timeClaims);
+ }else{
+ return TokenUtils.generateTokenString("/Token2.json", null, timeClaims);
+ }
}
}
http://git-wip-us.apache.org/repos/asf/tomee/blob/2e460433/examples/mp-rest-jwt/src/test/java/org/superbiz/rest/GreetingServiceTest.java
----------------------------------------------------------------------
diff --git a/examples/mp-rest-jwt/src/test/java/org/superbiz/rest/GreetingServiceTest.java b/examples/mp-rest-jwt/src/test/java/org/superbiz/rest/GreetingServiceTest.java
deleted file mode 100644
index f38063f..0000000
--- a/examples/mp-rest-jwt/src/test/java/org/superbiz/rest/GreetingServiceTest.java
+++ /dev/null
@@ -1,61 +0,0 @@
-/**
- * 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
- * <p/>
- * http://www.apache.org/licenses/LICENSE-2.0
- * <p/>
- * 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.superbiz.rest;
-
-import org.apache.cxf.jaxrs.client.WebClient;
-import org.apache.openejb.jee.WebApp;
-import org.apache.openejb.junit.ApplicationComposer;
-import org.apache.openejb.testing.Classes;
-import org.apache.openejb.testing.EnableServices;
-import org.apache.openejb.testing.Module;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-
-import javax.ws.rs.core.MediaType;
-import java.io.IOException;
-
-import static org.junit.Assert.assertEquals;
-
-@EnableServices(value = "jaxrs", httpDebug = true)
-@RunWith(ApplicationComposer.class)
-public class GreetingServiceTest {
-
- @Module
- @Classes(GreetingService.class)
- public WebApp app() {
- return new WebApp().contextRoot("test");
- }
-
- @Test
- public void get() throws IOException {
- final String message = WebClient.create("http://localhost:4204")
- .path("/test/greeting/")
- .accept(MediaType.APPLICATION_JSON_TYPE)
- .get(String.class);
- assertEquals("Hi Microprofile JWT!", message);
- }
-
- @Test
- public void post() throws IOException {
- final String message = WebClient.create("http://localhost:4204")
- .path("/test/greeting/")
- .type(MediaType.APPLICATION_JSON_TYPE)
- .accept(MediaType.APPLICATION_JSON_TYPE)
- .post("Hi REST!", String.class);
- assertEquals("hi rest!", message);
- }
-}
http://git-wip-us.apache.org/repos/asf/tomee/blob/2e460433/examples/mp-rest-jwt/src/test/resources/Token1.json
----------------------------------------------------------------------
diff --git a/examples/mp-rest-jwt/src/test/resources/Token1.json b/examples/mp-rest-jwt/src/test/resources/Token1.json
index 32b03c8..2766575 100644
--- a/examples/mp-rest-jwt/src/test/resources/Token1.json
+++ b/examples/mp-rest-jwt/src/test/resources/Token1.json
@@ -8,13 +8,10 @@
"exp": 1311281970,
"iat": 1311280970,
"auth_time": 1311280969,
- "roles": [
- "Echoer"
- ],
"groups": [
- "Echoer",
- "Tester",
"group1",
- "group2"
+ "group2",
+ "crud",
+ "read-only"
]
}
http://git-wip-us.apache.org/repos/asf/tomee/blob/2e460433/examples/mp-rest-jwt/src/test/resources/Token2.json
----------------------------------------------------------------------
diff --git a/examples/mp-rest-jwt/src/test/resources/Token2.json b/examples/mp-rest-jwt/src/test/resources/Token2.json
index d69f61a..f591863 100644
--- a/examples/mp-rest-jwt/src/test/resources/Token2.json
+++ b/examples/mp-rest-jwt/src/test/resources/Token2.json
@@ -1,12 +1,14 @@
{
"iss": "https://server.example.com",
- "jti": "a-123.2",
- "sub": "24400320#2",
- "upn": "jdoe2@example.com",
- "preferred_username": "jdoe",
- "aud": "s6BhdRkqt3.2",
+ "jti": "a-123",
+ "sub": "24400320",
+ "upn": "alice@example.com",
+ "preferred_username": "alice",
+ "aud": "s6BhdRkqt3",
"exp": 1311281970,
"iat": 1311280970,
"auth_time": 1311280969,
- "groups": ["Echoer2", "Tester", "Token2Role", "group1.2", "group2.2"]
+ "groups": [
+ "read-only"
+ ]
}