1   package org.apache.turbine.testcontainer;
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
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  
52  
53  
54  
55  
56  
57  
58  
59  
60  @TestMethodOrder(OrderAnnotation.class)
61  @Testcontainers
62  @Tag("docker")
63  
64  
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 );   
83        tc = new TurbineConfig(".",
84                "/conf/test/docker-manager/CompleteTurbineResources.properties");
85        try {
86           
87           
88           
89  
90           String jdbcConnectionString = BuildContainerWithDockerfileTest.generateJdbcUrl();
91           String customUrl = "torque.dsfactory.default.connection.url="+ jdbcConnectionString;
92           
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 
109 
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       
126       ( (ExtendedUser) user ).setFirstName( user.getName() );
127       ( (ExtendedUser) user ).setLastName( user.getName() );
128       turbineSecurityService.addUser( user, "fakepassword" );
129       assertTrue( turbineSecurityService.accountExists( user ) );
130       
131    }
132 
133    @Test
134    @Order(2)
135    @Tag("docker")
136    
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             
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 }