Clover icon

sunshower-sdk

  1. Project Clover database Sat May 12 2018 05:15:40 UTC
  2. Package io.sunshower.sdk.test

File SdkTest.java

 

Coverage histogram

../../../../img/srcFileCovDistChart6.png
26% of files have more coverage

Code metrics

0
34
15
1
197
165
16
0.47
2.27
15
1.07

Classes

Class Line # Actions
SdkTest 78 34 16
0.551020455.1%
 

Contributing tests

This file is covered by 6 tests. .

Source view

1    package io.sunshower.sdk.test;
2   
3    import io.sunshower.core.security.UserService;
4    import io.sunshower.core.security.crypto.EncryptionService;
5    import io.sunshower.jpa.flyway.FlywayConfiguration;
6    import io.sunshower.model.core.auth.Role;
7    import io.sunshower.model.core.auth.User;
8    import io.sunshower.persist.core.DataSourceConfiguration;
9    import io.sunshower.persist.hibernate.HibernateConfiguration;
10    import io.sunshower.sdk.core.ActivationEndpoint;
11    import io.sunshower.sdk.core.model.ActivationElement;
12    import io.sunshower.sdk.v1.SdkConfiguration;
13    import io.sunshower.sdk.v1.model.core.element.PersistentElement;
14    import io.sunshower.sdk.v1.model.core.security.PrincipalElement;
15    import io.sunshower.service.CoreServiceConfiguration;
16    import io.sunshower.service.security.Action;
17    import io.sunshower.service.security.SecurityConfiguration;
18    import io.sunshower.test.common.SerializationAware;
19    import io.sunshower.test.common.SerializationTestCase;
20    import io.sunshower.test.common.TestConfigurationConfiguration;
21    import io.sunshower.test.persist.AuthenticationTestExecutionListener;
22    import io.sunshower.test.persist.Authority;
23    import io.sunshower.test.persist.Principal;
24    import io.sunshower.test.ws.EnableJAXRS;
25    import io.sunshower.test.ws.Remote;
26    import org.junit.jupiter.api.AfterAll;
27    import org.junit.jupiter.api.AfterEach;
28    import org.junit.jupiter.api.BeforeEach;
29    import org.junit.jupiter.api.extension.ExtendWith;
30    import org.junit.platform.runner.JUnitPlatform;
31    import org.junit.runner.RunWith;
32    import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
33    import org.springframework.security.core.context.SecurityContextHolder;
34    import org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener;
35    import org.springframework.test.annotation.Rollback;
36    import org.springframework.test.context.ContextConfiguration;
37    import org.springframework.test.context.TestExecutionListeners;
38    import org.springframework.test.context.junit.jupiter.SpringExtension;
39    import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
40    import org.springframework.test.context.support.DirtiesContextTestExecutionListener;
41    import org.springframework.test.context.transaction.TransactionalTestExecutionListener;
42    import org.springframework.test.context.web.ServletTestExecutionListener;
43    import org.springframework.transaction.annotation.Isolation;
44    import org.springframework.transaction.annotation.Transactional;
45   
46    import javax.inject.Inject;
47    import java.util.Collection;
48    import java.util.Map;
49    import java.util.concurrent.ConcurrentHashMap;
50   
51    @Rollback
52    @EnableJAXRS
53    @ExtendWith(SpringExtension.class)
54    @RunWith(JUnitPlatform.class)
55    @TestExecutionListeners(
56    listeners = {
57    ServletTestExecutionListener.class,
58    DependencyInjectionTestExecutionListener.class,
59    DirtiesContextTestExecutionListener.class,
60    TransactionalTestExecutionListener.class,
61    // AuthenticationTestExecutionListener.class,,
62   
63    WithSecurityContextTestExecutionListener.class
64    },
65    mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS
66    )
67    @ContextConfiguration(classes = {
68    TestConfigurationConfiguration.class,
69    SdkConfiguration.class,
70    CoreServiceConfiguration.class,
71    SecurityConfiguration.class,
72    FlywayConfiguration.class,
73    DataSourceConfiguration.class,
74    HibernateConfiguration.class,
75    SdkTestConfiguration.class,
76    })
77    @Transactional(isolation = Isolation.READ_UNCOMMITTED)
 
78    public abstract class SdkTest extends SerializationTestCase {
79   
80    static final Map<String, String> passwords = new ConcurrentHashMap<>();
81   
82    @Remote
83    private ActivationEndpoint activationEndpoint;
84   
 
85  1 toggle static {
86  1 TestSecurityContextHolder.initialize();
87    }
88   
89   
90   
91   
92   
 
93  29 toggle public SdkTest() {
94  29 this(SerializationAware.Format.JSON, new Class[]{
95    PersistentElement.class
96    });
97    }
98   
 
99  29 toggle public SdkTest(SerializationAware.Format format, Class[] bound) {
100  29 super(format, bound);
101    }
102   
 
103  0 toggle public SdkTest(SerializationAware.Format format, boolean includeRoot, Class[] bound) {
104  0 super(format, includeRoot, bound);
105    }
106   
 
107  29 toggle @BeforeEach
108    public void setUp() {
109  29 TestSecurityContextHolder.clear();
110    }
111   
 
112  29 toggle @AfterEach
113    public void tearDown() {
114  29 TestSecurityContextHolder.clear();
115    }
116   
117   
118    @Inject
119    protected UserService userService;
120   
121    @Inject
122    protected EncryptionService encryptionService;
123   
124   
 
125  0 toggle @Authority("admin")
126    public Role createAdminAuthority() {
127  0 return new Role("admin");
128    }
129   
 
130  0 toggle @Authority("tenant:user")
131    public Role createTenantRole() {
132  0 return new Role("tenant:user");
133    }
134   
 
135  0 toggle @Principal
136    @SuppressWarnings("all")
137    public User createPrincipal(
138    @Authority("admin") Role role,
139    @Authority("tenant:user") Role user
140    ) {
141  0 final User administrator = new User();
142  0 administrator.setUsername("administrator");
143  0 administrator.setPassword(password("administrator", "frapadap"));
144  0 administrator.setActive(true);
145  0 role.addChild(user);
146  0 administrator.addRole(role);
147  0 administrator.getDetails().setEmailAddress("administrator");
148  0 return administrator;
149    }
150   
 
151  0 toggle @Principal
152    public User noRoles() {
153  0 final User noRoles = new User();
154  0 noRoles.setUsername("no-roles");
155  0 noRoles.setPassword(password("no-roles", "frapadap"));
156  0 noRoles.getDetails().setEmailAddress("dfasdfasdf");
157  0 noRoles.setActive(true);
158  0 return noRoles;
159    }
160   
 
161  6 toggle protected TestAction withPrincipals(PrincipalElement... principles) {
162  6 final PrincipalAction action = new PrincipalAction(activationEndpoint);
163  6 for(PrincipalElement principal : principles) {
164  6 try {
165  6 ActivationElement activate = activationEndpoint.activate(principal);
166  3 action.addPrincipal(activate, principal.getPassword());
167    } catch(Exception ex) {
168   
169    }
170    }
171  6 return action;
172    }
173   
 
174  4 toggle protected synchronized void changeSession(String username) {
175  4 User u = userService.findByUsername(username);
176  4 changeSession(username, passwords.get(username));
177    }
178   
179   
 
180  4 toggle protected synchronized static void changeSession(String userId, String unencryptedPassword) {
181  4 SecurityContextHolder.getContext()
182    .setAuthentication(
183    new UsernamePasswordAuthenticationToken(
184    userId, unencryptedPassword)
185    );
186    }
187   
 
188  1 toggle protected String password(String username, String password) {
189  1 passwords.put(username, password);
190  1 return encryptionService.encrypt(password);
191    }
192   
 
193  7 toggle @AfterAll
194    public static void tearDownClass() {
195  7 passwords.clear();
196    }
197    }