1 package no.knowit.seam.example.action;
   2 
   3 import java.util.ArrayList;
   4 import java.util.List;
   5 
   6 import javax.naming.NamingException;
   7 
   8 import no.knowit.crud.CrudService;
   9 import no.knowit.seam.example.model.Movie;
  10 import no.knowit.seam.openejb.mock.SeamOpenEjbTest;
  11 
  12 import org.jboss.seam.Component;
  13 import org.jboss.seam.core.Conversation;
  14 import org.jboss.seam.log.LogProvider;
  15 import org.jboss.seam.log.Logging;
  16 import org.jboss.seam.security.Credentials;
  17 import org.jboss.seam.security.Identity;
  18 import org.testng.Assert;
  19 import org.testng.annotations.BeforeClass;
  20 import org.testng.annotations.BeforeSuite;
  21 import org.testng.annotations.Test;
  22 
  23 public class MovieTest extends SeamOpenEjbTest {
  24 
  25   private static final String  DIRECTOR_JOEL_COEN= "Joel Coen";
  26   private static final String  THE_WALL_DIRECTOR = "Alan Parker";
  27   private static final String  THE_WALL_TITLE    = "The Wall";
  28   private static final Integer THE_WALL_YEAR     = 1992;
  29   private static final String  THE_WALL_PLOT     = "A troubled rock star descends into madness in the " +
  30                                                    "midst of his physical and social isolation from everyone.";
  31   private Integer              theWallId;
  32 
  33   private static final LogProvider log = Logging.getLogProvider(MovieTest.class);
  34 
  35   private CrudService lookupCrudService() throws Exception {
  36     try {
  37       Object service = initialContext.lookup("crudService/Local");
  38       assert service != null : "initialContext.lookup(\"crudService/Local\") returned null";
  39       assert service instanceof CrudService : "initialContext.lookup(\"crudService/Local\") returned incorrect type";
  40       return (CrudService) service;
  41     }
  42     catch (NamingException e) {
  43       log.error(e);
  44       throw(e);
  45     }
  46   }
  47 
  48   private boolean mockLogin() {
  49     Credentials credentials = (Credentials)Component.getInstance("org.jboss.seam.security.credentials");
  50     Identity identity = (Identity)Component.getInstance("org.jboss.seam.security.identity");
  51     credentials.setUsername("admin");
  52     identity.addRole("admin");
  53     return identity.login().equals("loggedIn");
  54   }
  55 
  56   @Override
  57   @BeforeSuite
  58   public void beforeSuite() throws Exception {
  59 
  60     //System.out.println("**** MovieTest.beforeSuite()");
  61 
  62 
  63     // Change some logging, INFO|DEBUG|WARN|ERROR|FATAL
  64     contextProperties.put("log4j.category.org.jboss.seam.Component", "DEBUG");
  65     contextProperties.put("log4j.category.org.jboss.seam.transaction", "DEBUG");
  66     contextProperties.put("log4j.category.org.jboss.seam.mock", "DEBUG");
  67 
  68     contextProperties.put("log4j.category.no.knowit.seam.openejb.mock", "DEBUG");
  69 
  70     contextProperties.put("log4j.category.no.knowit.seam.example", "debug");
  71 
  72     super.beforeSuite();
  73   }
  74 
  75   @Override
  76   @BeforeClass
  77   public void setupClass() throws Exception {
  78     super.setupClass();
  79 
  80     // Delete all movies
  81     CrudService crudService = lookupCrudService();
  82     crudService.remove(new Movie(), true);
  83     assert crudService.find(Movie.class).size() == 0 : "List.size():";
  84 
  85     // Persist 3 movies
  86     ArrayList<Movie> movies = new ArrayList<Movie>();
  87     movies.add(new Movie(DIRECTOR_JOEL_COEN, "The Big Lebowski", 1998,
  88       "\"Dude\" Lebowski, mistaken for a millionaire Lebowski, seeks restitution for his " +
  89       "ruined rug and enlists his bowling buddies to help get it."));
  90     movies.add(new Movie("Quentin Tarantino", "Reservoir Dogs", 1992,
  91       "After a simple jewelery heist goes terribly wrong, the surviving criminals begin " +
  92       "to suspect that one of them is a police informant."));
  93     movies.add(new Movie(DIRECTOR_JOEL_COEN, "Fargo", 1996,
  94       "Jerry Lundegaard's inept crime falls apart due to his and his henchmen's bungling " +
  95       "and the persistent police work of pregnant Marge Gunderson."));
  96 
  97     crudService.persist(movies);
  98   }
  99 
 100 
 101   @Test
 102   public void newMovie() throws Exception {
 103 
 104     new NonFacesRequest("/view/example/MovieList.xhtml") {
 105       @Override
 106       protected void renderResponse() throws Exception {
 107         int actual = (Integer)getValue("#{movieList.resultList.size}");
 108         Assert.assertEquals(actual, 3, "movieList.size:");
 109       }
 110     }.run();
 111 
 112     new FacesRequest("/login.xhtml") {
 113       @Override
 114       protected void updateModelValues() throws Exception {
 115         assert !isSessionInvalid() : "Invalid session";
 116         setValue("#{credentials.username}", "admin");
 117       }
 118 
 119       @Override
 120       protected void invokeApplication() throws Exception {
 121         assert invokeMethod("#{authenticator.authenticate}").equals(true) : "Authentication failed";
 122         assert invokeMethod("#{identity.login}").equals("loggedIn") : "Login failed";
 123         setOutcome("/view/example/MovieEdit.xhtml");
 124       }
 125     }.run();
 126 
 127     new FacesRequest("/view/example/MovieEdit.xhtml") {
 128       @Override
 129       protected void updateModelValues() throws Exception {
 130         invokeMethod( "#{movieHome.clearInstance}" );
 131         setValue("#{movieHome.instance.director}", THE_WALL_DIRECTOR);
 132         setValue("#{movieHome.instance.title}",    THE_WALL_TITLE);
 133         setValue("#{movieHome.instance.year}",     THE_WALL_YEAR);
 134       }
 135 
 136       @Override
 137       protected void invokeApplication() {
 138         assert invokeMethod("#{movieHome.persist}").equals("persisted");
 139         theWallId = (Integer)getValue("#{movieHome.movieId}");
 140         setOutcome("/view/example/Movie.xhtml");
 141       }
 142 
 143       @Override
 144       protected void afterRequest() {
 145         assert isInvokeApplicationComplete();
 146         assert !isRenderResponseBegun();
 147       }
 148     }.run();
 149 
 150     new NonFacesRequest("/view/example/Movie.xhtml") {
 151       @Override
 152       protected void renderResponse() {
 153         setValue("#{movieHome.movieId}", theWallId);
 154         invokeMethod("#{movieHome.wire}");
 155         assert getValue("#{movieHome.instance.director}").equals(THE_WALL_DIRECTOR);
 156         assert getValue("#{movieHome.instance.title}").equals(THE_WALL_TITLE);
 157         assert getValue("#{movieHome.instance.year}").equals(THE_WALL_YEAR);
 158       }
 159     }.run();
 160 
 161     new FacesRequest() {
 162       @Override
 163       protected void invokeApplication() throws Exception {
 164         assert getValue("#{identity.loggedIn}").equals(true) : "Not logged in";
 165         assert invokeAction("#{identity.logout}") == null;
 166         assert getValue("#{identity.loggedIn}").equals(false) : "Logout failed";
 167       }
 168     }.run();
 169   }
 170 
 171   @Test(dependsOnMethods={ "newMovie" })
 172   public void editMovie() throws Exception {
 173     new FacesRequest("/view/example/MovieEdit.xhtml") {
 174       @Override
 175       protected void invokeApplication() throws Exception {
 176         Conversation.instance().begin();
 177         assert !isSessionInvalid() : "Invalid session";
 178         assert mockLogin() : "Login failed";
 179         setValue("#{movieHome.movieId}", theWallId);
 180         invokeMethod( "#{movieHome.wire}" );
 181         setValue("#{movieHome.instance.plot}", THE_WALL_PLOT);
 182         Object result = invokeMethod( "#{movieHome.update}" );
 183         Assert.assertEquals(result, "updated", "#{movieHome.update}");
 184         Conversation.instance().end();
 185       }
 186     }.run();
 187   }
 188 
 189   @Test(dependsOnMethods={ "editMovie" })
 190   public void unauthorizedAccess() throws Exception {
 191     new FacesRequest("/view/example/MovieEdit.xhtml") {
 192       @Override
 193       protected void invokeApplication() throws Exception {
 194         Conversation.instance().begin();
 195         assert !isSessionInvalid() : "Invalid session";
 196         setValue("#{movieHome.movieId}", theWallId);
 197         invokeMethod( "#{movieHome.wire}" );
 198         setValue("#{movieHome.instance.plot}", THE_WALL_PLOT);
 199 
 200         try {
 201           invokeMethod( "#{movieHome.update}" );
 202           assert false : "Unauthorized access!";
 203         }
 204         catch (javax.el.ELException e) {
 205           assert e.getCause() instanceof org.jboss.seam.security.NotLoggedInException :
 206             "Expected to fail with: 'org.jboss.seam.security.NotLoggedInException'";
 207 
 208           log.debug("Passed expected unauthorized access failure: " + e.getMessage());
 209         }
 210         finally {
 211           Conversation.instance().end();
 212         }
 213       }
 214     }.run();
 215   }
 216 
 217   @Test(dependsOnMethods={ "editMovie" })
 218   public void persistWithMissingRequiredFieldValue() throws Exception {
 219     new FacesRequest("/view/example/MovieEdit.xhtml") {
 220       @Override
 221       protected void invokeApplication() throws Exception {
 222         Conversation.instance().begin();
 223         assert !isSessionInvalid() : "Invalid session";
 224         assert mockLogin() : "Login failed";
 225         invokeMethod("#{movieHome.clearInstance}");
 226         setValue("#{movieHome.instance.director}", THE_WALL_DIRECTOR);
 227         setValue("#{movieHome.instance.year}",     THE_WALL_YEAR);
 228 
 229         try {
 230           invokeMethod( "#{movieHome.persist}" );
 231           assert false : "Movie with missing field value persisted!";
 232         }
 233         catch (javax.el.ELException e) {
 234           assert e.getCause() instanceof javax.persistence.PersistenceException :
 235             "Expected to fail with: 'javax.persistence.PersistenceException'";
 236 
 237           log.debug("Passed expected missing required field value failure: " + e.getMessage());
 238         }
 239         finally {
 240           Conversation.instance().end();
 241         }
 242       }
 243     }.run();
 244   }
 245 
 246 
 247   @Test(dependsOnMethods={ "editMovie" })
 248   public void duplicateUniqueSecondaryIndex() throws Exception {
 249     new FacesRequest("/view/example/MovieEdit.xhtml") {
 250       @Override
 251       protected void invokeApplication() throws Exception {
 252         Conversation.instance().begin();
 253         assert !isSessionInvalid() : "Invalid session";
 254         assert mockLogin() : "Login failed";
 255         invokeMethod("#{movieHome.clearInstance}");
 256         setValue("#{movieHome.instance.director}", THE_WALL_DIRECTOR);
 257         setValue("#{movieHome.instance.title}",    THE_WALL_TITLE);
 258         setValue("#{movieHome.instance.year}",     THE_WALL_YEAR);
 259 
 260         try {
 261           invokeMethod( "#{movieHome.persist}" );
 262           assert false : "Movie with duplicate title persisted!";
 263         }
 264         catch (javax.el.ELException e) {
 265           assert e.getCause() instanceof javax.persistence.PersistenceException :
 266             "Expected to fail with: 'javax.persistence.PersistenceException'";
 267 
 268           log.debug("Passed expected duplicate unique secondary index failure: " + e.getMessage());
 269         }
 270         finally {
 271           Conversation.instance().end();
 272         }
 273       }
 274     }.run();
 275   }
 276 
 277   @Test(dependsOnMethods={ "duplicateUniqueSecondaryIndex" })
 278   public void deleteMovie() throws Exception {
 279     new FacesRequest("/view/example/MovieEdit.xhtml") {
 280       @Override
 281       protected void invokeApplication() throws Exception {
 282         Conversation.instance().begin();
 283         assert !isSessionInvalid() : "Invalid session";
 284         assert mockLogin() : "Login failed";
 285         setValue("#{movieHome.movieId}", theWallId);
 286         invokeMethod( "#{movieHome.wire}" );
 287         Assert.assertEquals(invokeMethod( "#{movieHome.remove}" ), "removed", "#{movieHome.remove}");
 288         Conversation.instance().end();
 289       }
 290     }.run();
 291   }
 292 
 293   @Test(dependsOnMethods={ "deleteMovie" })
 294   public void findMoviesDirectedByJoelCoen() throws Exception {
 295 
 296     new FacesRequest("/view/example/MovieList.xhtml") {
 297       @Override
 298       protected void updateModelValues() throws Exception {
 299         setValue("#{movieList.movie.director}", DIRECTOR_JOEL_COEN);
 300       }
 301 
 302       @Override
 303       protected void renderResponse() throws Exception {
 304         List<Movie> list= (List<Movie>) invokeMethod( "#{movieList.getResultList()}" );
 305         Assert.assertEquals(list.size(), 2, "#{movieList.getResultList()}");
 306         Movie movie = list.get(0);
 307         Assert.assertEquals(movie.getDirector(), DIRECTOR_JOEL_COEN, "movie.getDirector()");
 308       }
 309     }.run();
 310   }
 311 }