Skip to content

Commit 40efe33

Browse files
committed
Add test for SparkRInterpreter
1 parent 09bb458 commit 40efe33

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.zeppelin.spark;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertNotNull;
21+
import static org.junit.Assert.fail;
22+
import static org.mockito.Matchers.any;
23+
import static org.mockito.Matchers.anyString;
24+
import static org.mockito.Mockito.when;
25+
import static org.powermock.api.mockito.PowerMockito.*;
26+
27+
import org.apache.zeppelin.display.AngularObjectRegistry;
28+
import org.apache.zeppelin.display.GUI;
29+
import org.apache.zeppelin.interpreter.*;
30+
import org.junit.*;
31+
import org.junit.runner.RunWith;
32+
import org.mockito.Mockito;
33+
import org.slf4j.Logger;
34+
import org.slf4j.LoggerFactory;
35+
36+
import java.util.HashMap;
37+
import java.util.LinkedList;
38+
import java.util.Properties;
39+
40+
import org.powermock.core.classloader.annotations.PrepareForTest;
41+
import org.powermock.modules.junit4.PowerMockRunner;
42+
43+
@RunWith(PowerMockRunner.class)
44+
@PrepareForTest(SparkRInterpreter.ZeppelinRFactory.class)
45+
public class SparkRInterpreterTest {
46+
private static final Logger LOGGER = LoggerFactory.getLogger(SparkRInterpreterTest.class);
47+
48+
private static final String MOCK_RSCALA_RESULT = "<body> Mock R Result </body>";
49+
private static final String MOCK_R_INTERPRETER_RESULT = MOCK_RSCALA_RESULT.substring(MOCK_RSCALA_RESULT.indexOf("<body>") + 7, MOCK_RSCALA_RESULT.indexOf("</body>") - 1)
50+
.replaceAll("<code>", "").replaceAll("</code>", "")
51+
.replaceAll("\n\n", "")
52+
.replaceAll("\n", "<br>")
53+
.replaceAll("<pre>", "<p class='text'>").replaceAll("</pre>", "</p>");
54+
55+
private static InterpreterContext context;
56+
private static InterpreterGroup intpGroup;
57+
private static SparkInterpreter sparkInterpreter;
58+
private static SparkRInterpreter.ZeppelinRFactory zeppelinRFactory;
59+
private static SparkRInterpreter sparkRInterpreter;
60+
61+
@BeforeClass
62+
public static void beforeClass() {
63+
initInterpreters();
64+
}
65+
66+
@AfterClass
67+
public static void afterClass() {
68+
// Nothing to do here...
69+
}
70+
71+
@Before
72+
public void before() {
73+
// Nothing to do here...
74+
}
75+
76+
@After
77+
public void after() {
78+
// Nothing to do here...
79+
}
80+
81+
@Test
82+
public void testSuccess() throws Exception {
83+
InterpreterResult ret = sparkRInterpreter.interpret("print(1)", context);
84+
assertEquals(InterpreterResult.Code.SUCCESS, ret.code());
85+
assertEquals(MOCK_R_INTERPRETER_RESULT, ret.message());
86+
assertEquals(InterpreterResult.Type.HTML, ret.type());
87+
}
88+
89+
@Test
90+
public void testNullContext() throws Exception {
91+
try {
92+
sparkRInterpreter.interpret("print(1)", null);
93+
fail("NullPointerException not catched");
94+
} catch (NullPointerException e) {
95+
LOGGER.info("Exception in SparkSqlInterpreterTest while test ", e);
96+
}
97+
}
98+
99+
@Test
100+
public void testNullCommand() throws Exception {
101+
// assertNotNull(sparkInterpreter.getInterpreterGroup());
102+
}
103+
104+
@Test
105+
public void testInterpreterGroup() throws Exception {
106+
InterpreterResult ret = sparkRInterpreter.interpret(null, context);
107+
assertEquals(InterpreterResult.Code.ERROR, ret.code());
108+
}
109+
110+
private static void initInterpreters() {
111+
112+
Properties p = new Properties();
113+
114+
sparkInterpreter = new SparkInterpreter(p);
115+
intpGroup = new InterpreterGroup();
116+
intpGroup.add(sparkInterpreter);
117+
118+
zeppelinRFactory = mock(SparkRInterpreter.ZeppelinRFactory.class);
119+
doNothing().when(zeppelinRFactory).open(Mockito.anyString(), Mockito.anyString(), any(SparkInterpreter.class));
120+
when(zeppelinRFactory.getS0(anyString())).thenReturn(MOCK_RSCALA_RESULT);
121+
122+
mockStatic(SparkRInterpreter.ZeppelinRFactory.class);
123+
when(SparkRInterpreter.ZeppelinRFactory.instance()).thenReturn(zeppelinRFactory);
124+
125+
intpGroup.add(sparkRInterpreter);
126+
sparkRInterpreter = new SparkRInterpreter(p);
127+
sparkRInterpreter.setInterpreterGroup(intpGroup);
128+
sparkRInterpreter.open();
129+
130+
context = new InterpreterContext("note", "id", "title", "text", new HashMap<String, Object>(), new GUI(),
131+
new AngularObjectRegistry(intpGroup.getId(), null),
132+
null,
133+
new LinkedList<InterpreterContextRunner>(), new InterpreterOutput(new InterpreterOutputListener() {
134+
@Override
135+
public void onAppend(InterpreterOutput out, byte[] line) {
136+
}
137+
@Override
138+
public void onUpdate(InterpreterOutput out, byte[] output) {
139+
}
140+
}));
141+
142+
}
143+
144+
}

0 commit comments

Comments
 (0)