Skip to content

Commit c03db46

Browse files
committed
fix OLAP instantiation
1 parent 8aa4da0 commit c03db46

4 files changed

Lines changed: 149 additions & 3 deletions

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package it.eng.knowage.engine.api.export.dashboard.excel;
2+
3+
import org.json.JSONException;
4+
import org.json.JSONObject;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertFalse;
8+
import static org.junit.Assert.assertTrue;
9+
10+
public class DashboardExcelExporterTest {
11+
12+
@Test
13+
public void shouldKeepXlsxStyleEnabledByDefaultForSingleWidgetExports() {
14+
assertTrue(DashboardExcelExporter.isXlsxStyleEnabled(new JSONObject(), true));
15+
}
16+
17+
@Test
18+
public void shouldReadXlsxStyleEnabledFromSingleWidgetRoot() throws JSONException {
19+
JSONObject body = new JSONObject().put("xlsxStyleEnabled", false);
20+
21+
assertFalse(DashboardExcelExporter.isXlsxStyleEnabled(body, true));
22+
}
23+
24+
@Test
25+
public void shouldKeepXlsxStyleEnabledByDefaultForDashboardExports() {
26+
assertTrue(DashboardExcelExporter.isXlsxStyleEnabled(new JSONObject(), false));
27+
}
28+
29+
@Test
30+
public void shouldReadXlsxStyleEnabledFromDashboardMenuWidgetsConfiguration() throws JSONException {
31+
JSONObject body = new JSONObject()
32+
.put("configuration", new JSONObject()
33+
.put("menuWidgets", new JSONObject()
34+
.put("xlsxStyleEnabled", false)));
35+
36+
assertFalse(DashboardExcelExporter.isXlsxStyleEnabled(body, false));
37+
}
38+
}

knowageutils/src/main/java/it/eng/spagobi/utilities/engines/rest/AbstractEngineRestService.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
package it.eng.spagobi.utilities.engines.rest;
2020

21+
import java.io.StringReader;
22+
import java.nio.charset.StandardCharsets;
2123
import java.util.HashMap;
2224

2325
import javax.xml.bind.DatatypeConverter;
@@ -26,6 +28,7 @@
2628
import org.apache.logging.log4j.Logger;
2729
import org.json.JSONException;
2830
import org.json.JSONObject;
31+
import org.xml.sax.InputSource;
2932

3033
import it.eng.spago.base.SourceBean;
3134
import it.eng.spago.base.SourceBeanException;
@@ -97,7 +100,7 @@ public abstract class AbstractEngineRestService extends AbstractRestService {
97100
public SourceBean getTemplateAsSourceBean() {
98101
SourceBean templateSB = null;
99102
try {
100-
templateSB = SourceBean.fromXMLString(getTemplateAsString());
103+
templateSB = parseTemplateAsSourceBean(getTemplateAsString());
101104
} catch (SourceBeanException e) {
102105
SpagoBIEngineStartupException engineException = new SpagoBIEngineStartupException(getEngineName(),
103106
"Impossible to parse template's content", e);
@@ -109,10 +112,33 @@ public SourceBean getTemplateAsSourceBean() {
109112
return templateSB;
110113
}
111114

115+
private static SourceBean parseTemplateAsSourceBean(String templateContent) throws SourceBeanException {
116+
if (templateContent == null) {
117+
return null;
118+
}
119+
String sanitizedTemplateContent = sanitizeTemplateContent(templateContent);
120+
if (sanitizedTemplateContent.isEmpty()) {
121+
throw new SourceBeanException("xmlSourceBean non valido");
122+
}
123+
return SourceBean.fromXMLStream(new InputSource(new StringReader(sanitizedTemplateContent)));
124+
}
125+
126+
private static String sanitizeTemplateContent(String templateContent) {
127+
String sanitizedTemplateContent = removeUtf8Bom(templateContent);
128+
return sanitizedTemplateContent != null ? sanitizedTemplateContent.trim() : null;
129+
}
130+
131+
private static String removeUtf8Bom(String templateContent) {
132+
if (templateContent != null && !templateContent.isEmpty() && templateContent.charAt(0) == '\uFEFF') {
133+
return templateContent.substring(1);
134+
}
135+
return templateContent;
136+
}
137+
112138
public String getTemplateAsString() {
113139
byte[] temp = getTemplate();
114140
if (temp != null) {
115-
return new String(temp);
141+
return new String(temp, StandardCharsets.UTF_8);
116142
} else {
117143
return "";
118144
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Knowage, Open Source Business Intelligence suite
3+
* Copyright (C) 2016 Engineering Ingegneria Informatica S.p.A.
4+
*
5+
* Knowage is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Affero General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* Knowage is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Affero General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Affero General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
package it.eng.spagobi.utilities.engines.rest;
19+
20+
import javax.servlet.http.HttpServletRequest;
21+
22+
import org.junit.Assert;
23+
import org.junit.Test;
24+
25+
import it.eng.spago.base.SourceBean;
26+
import it.eng.spagobi.utilities.engines.SpagoBIEngineStartupException;
27+
28+
public class AbstractEngineRestServiceTest {
29+
30+
@Test
31+
public void testGetTemplateAsSourceBeanParsesXmlWithoutDeclaration() {
32+
SourceBean template = new TestEngineRestService("<olap><cube reference=\"FoodMartMySQL\"/></olap>")
33+
.getTemplateAsSourceBean();
34+
35+
Assert.assertEquals("OLAP", template.getName());
36+
Assert.assertNotNull(template.getAttribute("CUBE"));
37+
}
38+
39+
@Test
40+
public void testGetTemplateAsSourceBeanStripsUtf8Bom() {
41+
SourceBean template = new TestEngineRestService("\uFEFF<olap><cube reference=\"FoodMartMySQL\"/></olap>")
42+
.getTemplateAsSourceBean();
43+
44+
Assert.assertEquals("OLAP", template.getName());
45+
Assert.assertNotNull(template.getAttribute("CUBE"));
46+
}
47+
48+
@Test
49+
public void testGetTemplateAsSourceBeanWrapsInvalidTemplateErrors() {
50+
try {
51+
new TestEngineRestService(" ").getTemplateAsSourceBean();
52+
Assert.fail("Expected SpagoBIEngineStartupException");
53+
} catch (SpagoBIEngineStartupException e) {
54+
Assert.assertEquals("Impossible to parse template's content", e.getMessage());
55+
Assert.assertNotNull(e.getCause());
56+
}
57+
}
58+
59+
private static final class TestEngineRestService extends AbstractEngineRestService {
60+
61+
private final String templateAsString;
62+
63+
private TestEngineRestService(String templateAsString) {
64+
this.templateAsString = templateAsString;
65+
}
66+
67+
@Override
68+
public String getEngineName() {
69+
return "TestEngine";
70+
}
71+
72+
@Override
73+
public HttpServletRequest getServletRequest() {
74+
return null;
75+
}
76+
77+
@Override
78+
public String getTemplateAsString() {
79+
return templateAsString;
80+
}
81+
}
82+
}

knowageutils/src/test/java/it/eng/spagobi/utilities/engines/rest/AllTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import org.junit.runners.Suite.SuiteClasses;
2323

2424
@RunWith(Suite.class)
25-
@SuiteClasses({ SimpleRestClientTest.class })
25+
@SuiteClasses({ SimpleRestClientTest.class, AbstractEngineRestServiceTest.class })
2626
public class AllTests {
2727

2828
}

0 commit comments

Comments
 (0)