View Javadoc
1   package org.apache.turbine.testcontainer;
2   
3   import static junit.framework.TestCase.assertEquals;
4   import static junit.framework.TestCase.assertTrue;
5   import static junit.framework.TestCase.fail;
6   
7   import java.io.File;
8   import java.io.FileOutputStream;
9   
10  import org.apache.fulcrum.security.entity.ExtendedUser;
11  import org.apache.fulcrum.security.util.UnknownEntityException;
12  import org.apache.logging.log4j.LogManager;
13  import org.apache.logging.log4j.Logger;
14  import org.apache.torque.ConstraintViolationException;
15  import org.apache.turbine.annotation.AnnotationProcessor;
16  import org.apache.turbine.annotation.TurbineService;
17  import org.apache.turbine.om.security.User;
18  import org.apache.turbine.services.security.SecurityService;
19  import org.apache.turbine.util.TurbineConfig;
20  import org.junit.jupiter.api.BeforeAll;
21  import org.junit.jupiter.api.BeforeEach;
22  import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
23  import org.junit.jupiter.api.Order;
24  import org.junit.jupiter.api.Tag;
25  import org.junit.jupiter.api.Test;
26  import org.junit.jupiter.api.TestMethodOrder;
27  import org.testcontainers.containers.GenericContainer;
28  import org.testcontainers.junit.jupiter.Container;
29  import org.testcontainers.junit.jupiter.Testcontainers;
30  
31  /**
32   * Steps to run this @see {@link BuildContainerWithDockerfileTest}
33   *
34   * TODO
35   * This test might be more useful in a running turbine environment,
36   * e.g. created by archetypes or in torque-test testing databases.
37   *
38   * @author gkallidis
39   *
40   */
41  @TestMethodOrder(OrderAnnotation.class)
42  @Testcontainers
43  @Tag("docker")
44  // requires manual port mapping in docker-manager/TorqueTest.properties,
45  //@Disabled
46  class UserManagerWithContainerTest {
47  
48     @TurbineService
49     SecurityService turbineSecurityService;
50  
51     static TurbineConfig tc;
52  
53     boolean onDeleteCascade = true;
54  
55     private static Logger log = LogManager.getLogger();
56  
57     @Container
58     private static GenericContainer MY_SQL_CONTAINER = BuildContainerWithDockerfileTest.MY_SQL_CONTAINER;
59  
60     @BeforeAll
61     public static void init() {
62  
63        MY_SQL_CONTAINER.setStartupAttempts( 3 );   // see MySQLContainer
64        tc = new TurbineConfig(".",
65                "/conf/test/docker-manager/CompleteTurbineResources.properties");
66        try {
67           // get Torque component configuration and override torque.dsfactory.default.connection.url with url containing mapped port.
68           //Connection c = BuildContainerWithDockerfileTest.getConnection();
69           //MY_SQL_CONTAINER.getMappedPort( BuildContainerWithDockerfileTest.SERVICE_PORT );
70  
71           String jdbcConnectionString = BuildContainerWithDockerfileTest.generateJdbcUrl();
72           String customUrl = "torque.dsfactory.default.connection.url="+ jdbcConnectionString;
73           // override and set mapped port in url, which is known only at runtime.
74           File file = new File("./conf/test/docker-manager/torque.usersettings.properties");
75           try (FileOutputStream fop = new FileOutputStream(file )) {
76               if (!file.exists()) {
77                   file.createNewFile();
78               }
79               fop.write( customUrl.getBytes() );
80               fop.flush();
81           }
82           tc.initialize();
83        } catch (Exception e) {
84           fail();
85        }
86     }
87  
88     /**
89      * executes as designed even if tests are disabled
90      * @throws Exception
91      */
92     @BeforeEach
93     public void before() throws Exception {
94        AnnotationProcessor.process(this);
95     }
96  
97     @Test
98     @Order(1)
99     @Tag("docker")
100    public void testCreateManagedUser()
101            throws Exception
102    {
103       User user = turbineSecurityService.getUserInstance();
104       user.setAccessCounter( 5 );
105       user.setName( "ringo" );
106       // required not null constraint
107       ( (ExtendedUser) user ).setFirstName( user.getName() );
108       ( (ExtendedUser) user ).setLastName( user.getName() );
109       turbineSecurityService.addUser( user, "fakepassword" );
110       assertTrue( turbineSecurityService.accountExists( user ) );
111       //assertTrue( turbineSecurityService.getUserManager().checkExists( user ) );
112    }
113 
114    @Test
115    @Order(2)
116    @Tag("docker")
117    //@Disabled
118    void selectNewUser() {
119       User ringo;
120       try {
121          ringo = turbineSecurityService.getUser("ringo");
122          assertEquals("ringo", ringo.getFirstName());
123 
124          deleteUser(ringo);
125 
126       } catch (Exception sqle) {
127           log.error( "new user error",sqle);
128           fail();
129       }
130 
131       try {
132          ringo = turbineSecurityService.getUser("ringo");
133          fail("Should throw UnknownEntity");
134       } catch (UnknownEntityException sqle) {
135          log.info( "correct entity unknown",sqle);
136       } catch (Exception sqle) {
137          log.error( "new user error",sqle);
138          fail();
139       }
140    }
141 
142    private void deleteUser( User user )
143    {
144       if ( onDeleteCascade )
145       {
146          try
147          {
148             // revokeAll is called before user delete
149             turbineSecurityService.removeUser( user );
150             log.info( "try to delete user " + user.getName() );
151          }
152          catch ( Exception e )
153          {
154             log.error( "deleting user " + user.getName() + " failed. "
155                     + e.getMessage() );
156             if ( e.getCause() != null &&
157                     e.getCause() instanceof ConstraintViolationException)
158             {
159                log.info( "error due to " + e.getCause().getMessage() );
160             }
161             else
162             {
163                log.info( "error due to " + e.getMessage() );
164             }
165          }
166       }
167       else
168       {
169          log.info( "onDeleteCascade false, user " + user.getName()
170                  + " not deleted!" );
171       }
172    }
173 }