Skip to content

Commit 9cb5c7c

Browse files
author
TanyaEf
committed
Added report's format as String option, IT, unitTests and verification of numbers of pages for RunReportAdapter
1 parent 3dfd2db commit 9cb5c7c

File tree

8 files changed

+303
-27
lines changed

8 files changed

+303
-27
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ OperationResult<InputStream> result = client
217217
.parameter("Country_multi_select", "USA")
218218
.run();
219219
```
220+
Please notice, if you pass zero as number of page, you will get all pages of report.
220221
In this mode you don't need to work in one session. In the above code we specified report URI, format in which we want to get a report and some report parameters. As we a result we got `InputStream` instance. In synchronous mode as a response you get a report itself while in asynchronous you get just a descriptor with report ID which you can use to download report afer it will be ready.
221222

222223
In order to run a report in asynchronous mode, you need firstly build `ReportExecutionRequest` instance and specify all the parameters needed to launch a report. The response from the server is the `ReportExecutionDescriptor` instance which contains the request ID needed to track the execution until completion and others report parameters. Here's the code to run a report:

src/integration-test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/reporting/ReportingServiceIT.java

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,82 @@ public void should_return_proper_entity_if_pass_pdf_report_output_format() {
4949

5050
}
5151

52+
@Test
53+
public void should_return_proper_entity_if_passed_number_of_pages_zero() {
54+
55+
/** When **/
56+
OperationResult<InputStream> result = session
57+
.reportingService()
58+
.report("/organizations/organization_1/adhoc/topics/Cascading_multi_select_topic")
59+
.prepareForRun(ReportOutputFormat.PDF, 0)
60+
.parameter("Cascading_state_multi_select", "CA")
61+
.parameter("Cascading_state_multi_select", "OR", "WA")
62+
.parameter("Cascading_name_single_select", "Adams-Steen Transportation Holdings")
63+
.parameter("Country_multi_select", "USA")
64+
.run();
65+
66+
InputStream entity = result.getEntity();
67+
/** Then **/
68+
Assert.assertNotNull(entity);
69+
70+
}
71+
@Test
72+
public void should_return_proper_entity_if_pass_string_output_format() {
73+
74+
/** When **/
75+
OperationResult<InputStream> result = session
76+
.reportingService()
77+
.report("/organizations/organization_1/adhoc/topics/Cascading_multi_select_topic")
78+
.prepareForRun("PDF", 1)
79+
.parameter("Cascading_state_multi_select", "CA")
80+
.parameter("Cascading_name_single_select", "Adams-Steen Transportation Holdings")
81+
.parameter("Country_multi_select", "USA")
82+
.run();
83+
84+
InputStream entity = result.getEntity();
85+
/** Then **/
86+
Assert.assertNotNull(entity);
87+
88+
}
89+
90+
@Test
91+
public void should_return_proper_entity_if_passed_wrong_number_of_pages() {
92+
93+
/** When **/
94+
OperationResult<InputStream> result = session
95+
.reportingService()
96+
.report("/organizations/organization_1/adhoc/topics/Cascading_multi_select_topic")
97+
.prepareForRun("PDF", 0,-1,1)
98+
.parameter("Cascading_state_multi_select", "CA")
99+
.parameter("Cascading_name_single_select", "Adams-Steen Transportation Holdings")
100+
.parameter("Country_multi_select", "USA")
101+
.run();
102+
103+
InputStream entity = result.getEntity();
104+
/** Then **/
105+
Assert.assertNotNull(entity);
106+
107+
}
108+
109+
@Test
110+
public void should_return_proper_entity_if_passed_all_wrong_number_of_pages() {
111+
112+
/** When **/
113+
OperationResult<InputStream> result = session
114+
.reportingService()
115+
.report("/organizations/organization_1/adhoc/topics/Cascading_multi_select_topic")
116+
.prepareForRun("PDF", 0,-1)
117+
.parameter("Cascading_state_multi_select", "CA")
118+
.parameter("Cascading_name_single_select", "Adams-Steen Transportation Holdings")
119+
.parameter("Country_multi_select", "USA")
120+
.run();
121+
122+
InputStream entity = result.getEntity();
123+
/** Then **/
124+
Assert.assertNotNull(entity);
125+
126+
}
127+
52128
@Test
53129
public void should_return_proper_entity_without_numbers_of_pages() {
54130

@@ -58,7 +134,6 @@ public void should_return_proper_entity_without_numbers_of_pages() {
58134
.report("/organizations/organization_1/adhoc/topics/Cascading_multi_select_topic")
59135
.prepareForRun(ReportOutputFormat.PDF)
60136
.parameter("Cascading_state_multi_select", "CA")
61-
.parameter("Cascading_state_multi_select", "OR", "WA")
62137
.parameter("Cascading_name_single_select", "Adams-Steen Transportation Holdings")
63138
.parameter("Country_multi_select", "USA")
64139
.run();
@@ -88,6 +163,25 @@ public void should_return_proper_entity_in_async_mode() {
88163
Assert.assertNotNull(reportExecutionDescriptor);
89164
}
90165

166+
@Test
167+
public void should_return_proper_entity_in_async_mode_if_format_is_string() {
168+
169+
/** When **/
170+
ReportExecutionRequest request = new ReportExecutionRequest();
171+
request.setReportUnitUri("/organizations/organization_1/adhoc/topics/Cascading_multi_select_topic");
172+
request
173+
.setAsync(true)
174+
.setOutputFormat("html");
175+
176+
OperationResult<ReportExecutionDescriptor> operationResult = session
177+
.reportingService()
178+
.newReportExecutionRequest(request);
179+
180+
ReportExecutionDescriptor reportExecutionDescriptor = operationResult.getEntity();
181+
/** Then **/
182+
Assert.assertNotNull(reportExecutionDescriptor);
183+
}
184+
91185
@AfterMethod
92186
public void after() {
93187
client = null;

src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/reporting/ReportsAdapter.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,31 +35,43 @@ public class ReportsAdapter extends AbstractAdapter {
3535

3636
private final String reportUnitUri;
3737

38-
public ReportsAdapter(SessionStorage sessionStorage, String reportUnitUri){
38+
public ReportsAdapter(SessionStorage sessionStorage, String reportUnitUri) {
3939
super(sessionStorage);
4040
this.reportUnitUri = reportUnitUri;
4141
}
4242

43-
public ReorderingReportParametersAdapter reportParameters(){
43+
public ReorderingReportParametersAdapter reportParameters() {
4444
return new ReorderingReportParametersAdapter(sessionStorage, reportUnitUri);
4545
}
4646

47-
public ReportParametersAdapter reportParameters(String mandatoryId, String... otherIds){
47+
public ReportParametersAdapter reportParameters(String mandatoryId, String... otherIds) {
4848
List<String> ids = new ArrayList<String>(Arrays.asList(otherIds));
4949
ids.add(0, mandatoryId);
5050
return new ReportParametersAdapter(sessionStorage, reportUnitUri, ReportParametersUtils.toPathSegment(ids));
5151
}
5252

53-
public RunReportAdapter prepareForRun(ReportOutputFormat format, Integer... pages){
53+
public RunReportAdapter prepareForRun(ReportOutputFormat format, Integer... pages) {
54+
return new RunReportAdapter(sessionStorage, reportUnitUri, format.toString().toLowerCase(), pages);
55+
}
56+
57+
public RunReportAdapter prepareForRun(ReportOutputFormat format, PageRange range) {
58+
return new RunReportAdapter(sessionStorage, reportUnitUri, format.toString().toLowerCase(), range);
59+
}
60+
61+
public RunReportAdapter prepareForRun(ReportOutputFormat format) {
62+
return new RunReportAdapter(sessionStorage, reportUnitUri, format.toString().toLowerCase());
63+
}
64+
public RunReportAdapter prepareForRun(String format, Integer... pages) {
5465
return new RunReportAdapter(sessionStorage, reportUnitUri, format, pages);
5566
}
5667

57-
public RunReportAdapter prepareForRun(ReportOutputFormat format, PageRange range){
68+
public RunReportAdapter prepareForRun(String format, PageRange range) {
5869
return new RunReportAdapter(sessionStorage, reportUnitUri, format, range);
5970
}
6071

61-
public RunReportAdapter prepareForRun(ReportOutputFormat format){
72+
public RunReportAdapter prepareForRun(String format) {
6273
return new RunReportAdapter(sessionStorage, reportUnitUri, format);
6374
}
6475

76+
6577
}

src/main/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/reporting/RunReportAdapter.java

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -28,38 +28,53 @@
2828
import com.jaspersoft.jasperserver.jaxrs.client.core.SessionStorage;
2929
import com.jaspersoft.jasperserver.jaxrs.client.core.ThreadPoolUtil;
3030
import com.jaspersoft.jasperserver.jaxrs.client.core.operationresult.OperationResult;
31-
32-
import javax.ws.rs.core.MultivaluedHashMap;
33-
import javax.ws.rs.core.MultivaluedMap;
3431
import java.io.InputStream;
3532
import java.util.Arrays;
33+
import java.util.Iterator;
34+
import java.util.LinkedList;
3635
import java.util.List;
3736
import java.util.regex.Matcher;
3837
import java.util.regex.Pattern;
38+
import javax.ws.rs.core.MultivaluedHashMap;
39+
import javax.ws.rs.core.MultivaluedMap;
3940

4041
import static java.util.regex.Pattern.compile;
4142

4243
public class RunReportAdapter extends AbstractAdapter {
4344

4445
private final MultivaluedMap<String, String> params;
4546
private final String reportUnitUri;
46-
private final ReportOutputFormat format;
47-
private String[] pages;
47+
private final String format;
48+
private String[] pages = new String[0];
4849

49-
public RunReportAdapter(SessionStorage sessionStorage, String reportUnitUri, ReportOutputFormat format) {
50+
public RunReportAdapter(SessionStorage sessionStorage, String reportUnitUri, String format) {
5051
super(sessionStorage);
5152
this.params = new MultivaluedHashMap<String, String>();
5253
this.reportUnitUri = reportUnitUri;
53-
this.format = format;
54+
this.format = format.toLowerCase();
5455
}
5556

56-
public RunReportAdapter(SessionStorage sessionStorage, String reportUnitUri, ReportOutputFormat format,
57+
public RunReportAdapter(SessionStorage sessionStorage, String reportUnitUri, String format,
5758
Integer[] pages) {
5859
this(sessionStorage, reportUnitUri, format);
59-
this.pages = toStringArray(pages);
60+
if (pages != null && !(pages.length == 1 && pages[0] == 0)) {
61+
this.pages = toStringArray(validArray(pages));
62+
}
63+
}
64+
65+
private Integer[] validArray (Integer[] pages) {
66+
List<Integer> list = new LinkedList<Integer>(Arrays.asList(pages));
67+
Iterator<Integer> iterator = list.iterator();
68+
while (iterator.hasNext()) {
69+
Integer next = iterator.next();
70+
if (next <= 0) {
71+
iterator.remove();
72+
}
73+
}
74+
return list.toArray(new Integer[list.size()]);
6075
}
6176

62-
public RunReportAdapter(SessionStorage sessionStorage, String reportUnitUri, ReportOutputFormat format,
77+
public RunReportAdapter(SessionStorage sessionStorage, String reportUnitUri, String format,
6378
PageRange range) {
6479
this(sessionStorage, reportUnitUri, format);
6580
this.pages = new String[]{range.getRange()};
@@ -102,12 +117,12 @@ private JerseyRequest<InputStream> prepareRunRequest() {
102117
JerseyRequest<InputStream> request = JerseyRequest.buildRequest(
103118
sessionStorage,
104119
InputStream.class,
105-
new String[]{"/reports", reportUnitUri + "." + format.toString().toLowerCase()},
120+
new String[]{"/reports", reportUnitUri + "." + format},
106121
new RunReportErrorHandler());
107122

108123
request.addParams(params);
109124

110-
if (pages != null && pages.length > 0) {
125+
if (pages.length > 0) {
111126
if (pages.length == 1) {
112127
Pattern pattern = compile("^(\\d+)-(\\d+)$");
113128
Matcher matcher = pattern.matcher(pages[0]);

src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/reports/ExportExecutionOptions.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ public ExportExecutionOptions setOutputFormat(ReportOutputFormat outputFormat) {
6666
return this;
6767
}
6868

69+
public ExportExecutionOptions setOutputFormat(String outputFormat) {
70+
this.outputFormat = outputFormat.toLowerCase();
71+
return this;
72+
}
73+
6974
public String getBaseUrl() {
7075
return baseUrl;
7176
}

src/main/java/com/jaspersoft/jasperserver/jaxrs/client/dto/reports/ReportExecutionRequest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ public ReportExecutionRequest setOutputFormat(ReportOutputFormat outputFormat) {
117117
this.outputFormat = outputFormat.toString().toLowerCase();
118118
return this;
119119
}
120+
public ReportExecutionRequest setOutputFormat(String outputFormat) {
121+
this.outputFormat = outputFormat.toLowerCase();
122+
return this;
123+
}
120124

121125
public String getAttachmentsPrefix() {
122126
return attachmentsPrefix;

src/test/java/com/jaspersoft/jasperserver/jaxrs/client/apiadapters/reporting/ReportsAdapterTest.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,20 @@ public void should_convert_params_into_the_adapter_with_pages() throws Exception
9696
assertEquals(Whitebox.getInternalState(retrieved, "reportUnitUri"), "reportUnitUri");
9797
assertEquals(Whitebox.getInternalState(retrieved, "pages"), new String[]{"1", "2", "3"});
9898
}
99+
@Test
100+
public void should_convert_params_into_the_adapter_with_format() throws Exception {
101+
102+
/* Given */
103+
ReportsAdapter adapterSpy = new ReportsAdapter(sessionStorageMock, "reportUnitUri");
104+
105+
/* When */
106+
RunReportAdapter retrieved = adapterSpy.prepareForRun(ReportOutputFormat.PDF);
107+
108+
/* Then */
109+
assertSame(retrieved.getSessionStorage(), sessionStorageMock);
110+
assertEquals(Whitebox.getInternalState(retrieved, "reportUnitUri"), "reportUnitUri");
111+
assertEquals(Whitebox.getInternalState(retrieved, "format"), new String("pdf"));
112+
}
99113

100114
@Test
101115
public void should_convert_params_into_the_adapter_without_pages() throws Exception {
@@ -110,6 +124,69 @@ public void should_convert_params_into_the_adapter_without_pages() throws Except
110124
assertSame(retrieved.getSessionStorage(), sessionStorageMock);
111125
assertEquals(Whitebox.getInternalState(retrieved, "reportUnitUri"), "reportUnitUri");
112126
assertEquals(Whitebox.getInternalState(retrieved, "pages"), new String[]{"1-10"});
127+
128+
}
129+
130+
@Test
131+
public void should_convert_params_into_the_adapter_with_pages_and_format_as_string() throws Exception {
132+
133+
/* Given */
134+
ReportsAdapter adapterSpy = new ReportsAdapter(sessionStorageMock, "reportUnitUri");
135+
136+
/* When */
137+
RunReportAdapter retrieved = adapterSpy.prepareForRun("PDF", 1, 2, 3);
138+
139+
/* Then */
140+
assertSame(retrieved.getSessionStorage(), sessionStorageMock);
141+
assertEquals(Whitebox.getInternalState(retrieved, "reportUnitUri"), "reportUnitUri");
142+
assertEquals(Whitebox.getInternalState(retrieved, "pages"), new String[]{"1", "2", "3"});
143+
assertEquals(Whitebox.getInternalState(retrieved, "format"), "pdf");
144+
}
145+
@Test
146+
public void should_convert_params_into_the_adapter_with_format_as_string_in_uppercase() throws Exception {
147+
148+
/* Given */
149+
ReportsAdapter adapterSpy = new ReportsAdapter(sessionStorageMock, "reportUnitUri");
150+
151+
/* When */
152+
RunReportAdapter retrieved = adapterSpy.prepareForRun("HTML");
153+
154+
/* Then */
155+
assertSame(retrieved.getSessionStorage(), sessionStorageMock);
156+
assertEquals(Whitebox.getInternalState(retrieved, "reportUnitUri"), "reportUnitUri");
157+
assertEquals(Whitebox.getInternalState(retrieved, "format"), "html");
158+
159+
}
160+
@Test
161+
public void should_convert_params_into_the_adapter_with_format_as_string() throws Exception {
162+
163+
/* Given */
164+
ReportsAdapter adapterSpy = new ReportsAdapter(sessionStorageMock, "reportUnitUri");
165+
166+
/* When */
167+
RunReportAdapter retrieved = adapterSpy.prepareForRun("pdf");
168+
169+
/* Then */
170+
assertSame(retrieved.getSessionStorage(), sessionStorageMock);
171+
assertEquals(Whitebox.getInternalState(retrieved, "reportUnitUri"), "reportUnitUri");
172+
assertEquals(Whitebox.getInternalState(retrieved, "format"), "pdf");
173+
174+
}
175+
176+
@Test
177+
public void should_convert_params_into_the_adapter_without_pages_as_string() throws Exception {
178+
179+
/* Given */
180+
ReportsAdapter adapterSpy = new ReportsAdapter(sessionStorageMock, "reportUnitUri");
181+
182+
/* When */
183+
RunReportAdapter retrieved = adapterSpy.prepareForRun("pdf", new PageRange(1, 10));
184+
185+
/* Then */
186+
assertSame(retrieved.getSessionStorage(), sessionStorageMock);
187+
assertEquals(Whitebox.getInternalState(retrieved, "reportUnitUri"), "reportUnitUri");
188+
assertEquals(Whitebox.getInternalState(retrieved, "pages"), new String[]{"1-10"});
189+
assertEquals(Whitebox.getInternalState(retrieved, "format"), "pdf");
113190
}
114191

115192
@AfterMethod

0 commit comments

Comments
 (0)