-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAbstractTechnicalTest.java
More file actions
83 lines (69 loc) · 2.81 KB
/
AbstractTechnicalTest.java
File metadata and controls
83 lines (69 loc) · 2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package org.marvelution.jji;
import java.util.Optional;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
import org.marvelution.jji.configuration.JiraSite;
import org.marvelution.jji.configuration.JiraSitesConfiguration;
import org.marvelution.jji.rest.HttpClientProvider;
import org.marvelution.jji.rest.ObjectMapperProvider;
import org.marvelution.jji.synctoken.CanonicalHttpServletRequest;
import org.marvelution.jji.synctoken.SyncTokenBuilder;
import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import org.junit.jupiter.api.BeforeEach;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.MockAuthorizationStrategy;
@WithJenkins
public abstract class AbstractTechnicalTest
{
public static final String ALICE = "alice";
protected final MockAuthorizationStrategy authorizationStrategy = new MockAuthorizationStrategy();
protected JenkinsRule jenkins;
protected JiraSitesConfiguration sitesConfiguration;
protected OkHttpClient httpClient;
protected ObjectMapper objectMapper;
@BeforeEach
void setUp(JenkinsRule jenkins)
{
this.jenkins = jenkins;
jenkins.getInstance()
.setSecurityRealm(jenkins.createDummySecurityRealm());
jenkins.getInstance()
.setAuthorizationStrategy(authorizationStrategy);
sitesConfiguration = jenkins.getInstance()
.getDescriptorByType(JiraSitesConfiguration.class);
httpClient = new HttpClientProvider().httpClient();
objectMapper = new ObjectMapperProvider().objectMapper();
}
protected void injectSite(JiraSite site)
{
sitesConfiguration.registerSite(new JiraSite(site.getUri()).withIdentifier(site.getIdentifier())
.withName(site.getName())
.withSharedSecret(site.getSharedSecret())
.withPostJson(site.isPostJson()));
}
protected Request signTokenAuth(
Request request,
JiraSite site,
String contextPath)
{
return signTokenAuth(request, site.getIdentifier(), site.getSharedSecret(), contextPath);
}
protected Request signTokenAuth(
Request request,
String identifier,
String sharedSecret,
String contextPath)
{
CanonicalHttpServletRequest canonicalHttpRequest = new CanonicalHttpServletRequest(request.method(),
request.url()
.uri(),
Optional.ofNullable(contextPath));
Request.Builder builder = request.newBuilder();
new SyncTokenBuilder().identifier(identifier)
.sharedSecret(sharedSecret)
.request(canonicalHttpRequest)
.generateTokenAndAddHeaders(builder::addHeader);
return builder.build();
}
}