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