This repository was archived by the owner on Oct 9, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathTemplateReferenceExample.java
More file actions
104 lines (88 loc) · 3.94 KB
/
TemplateReferenceExample.java
File metadata and controls
104 lines (88 loc) · 3.94 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/******************************************************************************
*
* Copyright (c) 2017 CA. All rights reserved.
*
* This software and all information contained therein is confidential and
* proprietary and shall not be duplicated, used, disclosed or disseminated
* in any way except as authorized by the applicable license agreement,
* without the express written permission of CA. All authorized reproductions
* must be marked with this language.
*
* EXCEPT AS SET FORTH IN THE APPLICABLE LICENSE AGREEMENT, TO THE EXTENT
* PERMITTED BY APPLICABLE LAW, CA PROVIDES THIS SOFTWARE WITHOUT
* WARRANTY OF ANY KIND, INCLUDING WITHOUT LIMITATION, ANY IMPLIED
* WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. IN
* NO EVENT WILL CA BE LIABLE TO THE END USER OR ANY THIRD PARTY FOR ANY
* LOSS OR DAMAGE, DIRECT OR INDIRECT, FROM THE USE OF THIS SOFTWARE,
* INCLUDING WITHOUT LIMITATION, LOST PROFITS, BUSINESS INTERRUPTION,
* GOODWILL, OR LOST DATA, EVEN IF CA IS EXPRESSLY ADVISED OF SUCH LOSS OR
* DAMAGE.
*
******************************************************************************/
package com.ca.codesv.example;
import static com.ca.codesv.api.matchers.GenericMatchers.matchesTemplate;
import static com.ca.codesv.protocols.http.fluent.HttpFluentInterface.forPost;
import static com.ca.codesv.protocols.http.fluent.HttpFluentInterface.okMessage;
import static junit.framework.TestCase.assertNotNull;
import static org.junit.Assert.assertEquals;
import com.ca.codesv.engine.junit4.VirtualServerRule;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.Rule;
import org.junit.Test;
/**
* Example showing usage of template matcher.
*
* @author CA
*/
public class TemplateReferenceExample {
private static final String BASE_URL = "http://www.ca.com/portfolio";
private static final String TEMPLATE = "Hello, {computer}. Do you read me, {computer2}?;"
+ "Affirmative, {user}. I read you.;"
+ "Open the pod bay doors! {plead}!";
private static final String TEXT = "Hello, HAL. Do you read me, HAL?;"
+ "Affirmative, Dave. I read you.;"
+ "Open the pod bay doors! PLEASE!";
private static final String REFERENCE =
"Hello, ${template.computer}. Do you read me, ${template.computer2}?;"
+ "Affirmative, ${template.user}. I read you.;"
+ "Open the pod bay doors! ${template.plead}!";
@Rule
public VirtualServerRule vs = new VirtualServerRule();
@Test
public void testTemplateMagicString() throws Exception {
forPost(BASE_URL)
.matchesHeader("Language", matchesTemplate("Language of Text: {language}"))
.matchesBody(matchesTemplate(TEMPLATE))
.doReturn(
okMessage()
.withStringBody(REFERENCE)
.withHeader("Language", "${template.language}")
// By default magic strings are disabled
.enableMagicStrings()
);
HttpClient client = HttpClientBuilder.create().build();
HttpPost request = new HttpPost(BASE_URL);
// 2 headers with the same name and different values
request.addHeader("Language", "Language of Text: en_us");
request.setEntity(new StringEntity(TEXT));
HttpResponse response = client.execute(request);
BufferedReader reader = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
String body = result.toString();
assertEquals(200, response.getStatusLine().getStatusCode());
assertNotNull(body);
assertEquals(TEXT, body);
assertEquals("en_us", response.getFirstHeader("Language").getValue());
}
}