cccf06018c1666a5b5e167092af157b0bf595941
[integration.git] /
1 package org.onap.integration.test.mocks.sniroemulator.extension;
2
3 import com.fasterxml.jackson.annotation.JsonCreator;
4 import com.fasterxml.jackson.annotation.JsonIgnore;
5 import com.fasterxml.jackson.annotation.JsonProperty;
6 import com.github.tomakehurst.wiremock.http.Body;
7 import com.github.tomakehurst.wiremock.http.HttpHeader;
8 import com.github.tomakehurst.wiremock.http.HttpHeaders;
9 import com.github.tomakehurst.wiremock.http.RequestMethod;
10
11 import java.net.URI;
12 import java.util.List;
13
14 import static com.google.common.collect.Lists.newArrayList;
15
16 public class WebhookDefinition {
17     
18     private RequestMethod method;
19     private URI url;
20     private List<HttpHeader> headers;
21     private Body body = Body.none();
22
23     @JsonCreator
24     public WebhookDefinition(@JsonProperty("method") RequestMethod method,
25                              @JsonProperty("url") URI url,
26                              @JsonProperty("headers") HttpHeaders headers,
27                              @JsonProperty("body") String body,
28                              @JsonProperty("base64Body") String base64Body) {
29         this.method = method;
30         this.url = url;
31         this.headers = newArrayList(headers.all());
32         this.body = Body.fromOneOf(null, body, null, base64Body);
33     }
34
35     public WebhookDefinition() {
36     }
37
38     public RequestMethod getMethod() {
39         return method;
40     }
41
42     public URI getUrl() {
43         return url;
44     }
45
46     public HttpHeaders getHeaders() {
47         return new HttpHeaders(headers);
48     }
49
50     public String getBase64Body() {
51         return body.isBinary() ? body.asBase64() : null;
52     }
53
54     public String getBody() {
55         return body.isBinary() ? null : body.asString();
56     }
57
58     @JsonIgnore
59     public byte[] getBinaryBody() {
60         return body.asBytes();
61     }
62
63     public WebhookDefinition withMethod(RequestMethod method) {
64         this.method = method;
65         return this;
66     }
67
68     public WebhookDefinition withUrl(URI url) {
69         this.url = url;
70         return this;
71     }
72
73     public WebhookDefinition withUrl(String url) {
74         withUrl(URI.create(url));
75         return this;
76     }
77
78     public WebhookDefinition withHeaders(List<HttpHeader> headers) {
79         this.headers = headers;
80         return this;
81     }
82
83     public WebhookDefinition withHeader(String key, String... values) {
84         if (headers == null) {
85             headers = newArrayList();
86         }
87
88         headers.add(new HttpHeader(key, values));
89         return this;
90     }
91
92     public WebhookDefinition withBody(String body) {
93         this.body = new Body(body);
94         return this;
95     }
96
97     public WebhookDefinition withBinaryBody(byte[] body) {
98         this.body = new Body(body);
99         return this;
100     }
101 }