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 pathMagicStringExample.java
More file actions
118 lines (100 loc) · 4.42 KB
/
MagicStringExample.java
File metadata and controls
118 lines (100 loc) · 4.42 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/******************************************************************************
*
* 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.protocols.http.fluent.HttpFluentInterface.forGet;
import static com.ca.codesv.protocols.http.fluent.HttpFluentInterface.isEqualIgnoringCaseTo;
import static com.ca.codesv.protocols.http.fluent.HttpFluentInterface.okMessage;
import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertTrue;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
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.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.Rule;
import org.junit.Test;
/**
* Example showing magic strings possibilities.
*
* @author CA
*/
public class MagicStringExample {
private static final String BASE_URL = "http://www.ca.com/portfolio";
private static final String QUERY = "?year=2016&tokenQuery=X4sPhj15WQE";
// This will be create this URL- > http://www.ca.com/portfolio/{id}?year=2016&tokenQuery=X4sPhj15WQE
private static final String URL = BASE_URL + "/{id}" + QUERY;
private static final String JSON_EXAMPLES_PORTFOLIO = "{"
+ "\"portfolio\": {\n"
+ " \"id\": \"${attribute.id}\",\n"
+ " \"year\": \"${argument.year}\",\n"
+ " \"language\": \"${metadata.Language}\",\n"
+ " \"productNamesList\": [\n"
+ " \"CA Server Automation\",\n"
+ " \"CA Service Catalog\",\n"
+ " \"CA Service Desk Manager\",\n"
+ " \"CA Service Management\",\n"
+ " \"CA Service Operations Insight\",\n"
+ " \"CA Service Virtualization\"\n"
+ " ]\n"
+ "}}";
@Rule
public VirtualServerRule vs = new VirtualServerRule();
@Test
public void testMagicString() throws Exception {
forGet(URL)
.matchesHeader("Language", is("en_us"))
.matchesQuery("tokenQuery", isEqualIgnoringCaseTo("x4sphj15wqe"))
.matchesQuery("year", "2016")
.doReturn(
okMessage()
.withJsonBody(JSON_EXAMPLES_PORTFOLIO)
);
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(BASE_URL + "/1" + QUERY);
// 2 headers with the same name and different values
request.addHeader("Language", "en_us");
request.addHeader("Language", "en_uk");
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().replaceAll("\\s+", "");
assertEquals(200, response.getStatusLine().getStatusCode());
assertNotNull(body);
assertTrue(body.contains("\"year\":\"2016\""));
assertFalse(body.contains("${argument.year}"));
assertTrue(body.contains("\"id\":\"1\""));
assertFalse(body.contains("${attribute.id}"));
// Only the first header value will be replace
assertTrue(body.contains("\"language\":\"en_us\""));
assertFalse(body.contains("${metadata.Language}"));
}
}