60592b3f0921ff6a6aa72bd4c49f6cddd96d408f
[integration.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.integration
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.integration.test.mocks.sniroemulator.extension;
21
22 import com.fasterxml.jackson.annotation.JsonCreator;
23 import com.fasterxml.jackson.annotation.JsonIgnore;
24 import com.fasterxml.jackson.annotation.JsonProperty;
25 import com.github.tomakehurst.wiremock.http.Body;
26 import com.github.tomakehurst.wiremock.http.HttpHeader;
27 import com.github.tomakehurst.wiremock.http.HttpHeaders;
28 import com.github.tomakehurst.wiremock.http.RequestMethod;
29
30 import java.net.URI;
31 import java.util.List;
32
33 import static com.google.common.collect.Lists.newArrayList;
34
35 public class WebhookDefinition {
36     
37     private RequestMethod method;
38     private URI url;
39     private List<HttpHeader> headers;
40     private Body body = Body.none();
41
42     @JsonCreator
43     public WebhookDefinition(@JsonProperty("method") RequestMethod method,
44                              @JsonProperty("url") URI url,
45                              @JsonProperty("headers") HttpHeaders headers,
46                              @JsonProperty("body") String body,
47                              @JsonProperty("base64Body") String base64Body) {
48         this.method = method;
49         this.url = url;
50         this.headers = newArrayList(headers.all());
51         this.body = Body.fromOneOf(null, body, null, base64Body);
52     }
53
54     public WebhookDefinition() {
55     }
56
57     public RequestMethod getMethod() {
58         return method;
59     }
60
61     public URI getUrl() {
62         return url;
63     }
64
65     public HttpHeaders getHeaders() {
66         return new HttpHeaders(headers);
67     }
68
69     public String getBase64Body() {
70         return body.isBinary() ? body.asBase64() : null;
71     }
72
73     public String getBody() {
74         return body.isBinary() ? null : body.asString();
75     }
76
77     @JsonIgnore
78     public byte[] getBinaryBody() {
79         return body.asBytes();
80     }
81
82     public WebhookDefinition withMethod(RequestMethod method) {
83         this.method = method;
84         return this;
85     }
86
87     public WebhookDefinition withUrl(URI url) {
88         this.url = url;
89         return this;
90     }
91
92     public WebhookDefinition withUrl(String url) {
93         withUrl(URI.create(url));
94         return this;
95     }
96
97     public WebhookDefinition withHeaders(List<HttpHeader> headers) {
98         this.headers = headers;
99         return this;
100     }
101
102     public WebhookDefinition withHeader(String key, String... values) {
103         if (headers == null) {
104             headers = newArrayList();
105         }
106
107         headers.add(new HttpHeader(key, values));
108         return this;
109     }
110
111     public WebhookDefinition withBody(String body) {
112         this.body = new Body(body);
113         return this;
114     }
115
116     public WebhookDefinition withBinaryBody(byte[] body) {
117         this.body = new Body(body);
118         return this;
119     }
120 }