mirror of
https://github.com/apache/zeppelin
synced 2026-05-24 09:38:26 +00:00
adding more mock test
This commit is contained in:
parent
8c4b983abb
commit
5f9be7352c
3 changed files with 125 additions and 12 deletions
|
|
@ -57,7 +57,7 @@ public class LivyHelper {
|
|||
this.property = property;
|
||||
}
|
||||
|
||||
protected Integer createSession(InterpreterContext context, String kind) throws Exception {
|
||||
public Integer createSession(InterpreterContext context, String kind) throws Exception {
|
||||
try {
|
||||
String json = executeHTTP(property.getProperty("zeppelin.livy.url") + "/sessions",
|
||||
"POST",
|
||||
|
|
@ -185,7 +185,7 @@ public class LivyHelper {
|
|||
}
|
||||
}
|
||||
|
||||
protected InterpreterResult interpret(String stringLines,
|
||||
public InterpreterResult interpret(String stringLines,
|
||||
final InterpreterContext context,
|
||||
final Map<String, Integer> userSessionMap)
|
||||
throws Exception {
|
||||
|
|
@ -298,7 +298,7 @@ public class LivyHelper {
|
|||
}
|
||||
}
|
||||
|
||||
public String executeHTTP(String targetURL, String method, String jsonData, String paragraphId)
|
||||
protected String executeHTTP(String targetURL, String method, String jsonData, String paragraphId)
|
||||
throws Exception {
|
||||
HttpClient client = HttpClientBuilder.create().build();
|
||||
HttpResponse response = null;
|
||||
|
|
@ -349,7 +349,7 @@ public class LivyHelper {
|
|||
paragraphHttpMap.put(paragraphId, null);
|
||||
}
|
||||
|
||||
protected void closeSession(Map<String, Integer> userSessionMap) {
|
||||
public void closeSession(Map<String, Integer> userSessionMap) {
|
||||
for (Map.Entry<String, Integer> entry : userSessionMap.entrySet()) {
|
||||
try {
|
||||
executeHTTP(property.getProperty("zeppelin.livy.url") + "/sessions/"
|
||||
|
|
|
|||
114
livy/src/main/test/org/apache/zeppelin/livy/LivyHelperTest.java
Normal file
114
livy/src/main/test/org/apache/zeppelin/livy/LivyHelperTest.java
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.zeppelin.livy;
|
||||
|
||||
import com.google.gson.GsonBuilder;
|
||||
import org.apache.zeppelin.interpreter.InterpreterContext;
|
||||
import org.apache.zeppelin.interpreter.InterpreterResult;
|
||||
import org.hamcrest.CoreMatchers;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ErrorCollector;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Answers;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
|
||||
/**
|
||||
* Created for org.apache.zeppelin.livy on 22/04/16.
|
||||
*/
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class LivyHelperTest {
|
||||
|
||||
@Rule
|
||||
public ErrorCollector collector = new ErrorCollector();
|
||||
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private static LivyPySparkInterpreter interpreter;
|
||||
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private InterpreterContext interpreterContext;
|
||||
|
||||
@Mock(answer = Answers.CALLS_REAL_METHODS)
|
||||
private LivyHelper livyHelper;
|
||||
|
||||
@Before
|
||||
public void prepareContext() throws Exception {
|
||||
interpreter.userSessionMap = new HashMap<>();
|
||||
interpreter.userSessionMap.put(null, 1);
|
||||
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("zeppelin.livy.url", "http://localhost:8998");
|
||||
livyHelper.property = properties;
|
||||
livyHelper.paragraphHttpMap = new HashMap<>();
|
||||
livyHelper.gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
|
||||
|
||||
doReturn("{\"id\":1,\"state\":\"idle\",\"kind\":\"spark\",\"proxyUser\":\"null\",\"log\":[]}")
|
||||
.when(livyHelper)
|
||||
.executeHTTP(
|
||||
livyHelper.property.getProperty("zeppelin.livy.url") + "/sessions",
|
||||
"POST",
|
||||
"{\"kind\": \"spark\", \"proxyUser\": \"null\"}",
|
||||
null
|
||||
);
|
||||
|
||||
doReturn("{\"id\":1,\"state\":\"available\",\"output\":{\"status\":\"ok\"," +
|
||||
"\"execution_count\":1,\"data\":{\"text/plain\":\"1\"}}}")
|
||||
.when(livyHelper)
|
||||
.executeHTTP(
|
||||
livyHelper.property.getProperty("zeppelin.livy.url") + "/sessions/1/statements",
|
||||
"POST",
|
||||
"{\"code\": \"print(1)\" }",
|
||||
null
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void checkCreateSession() {
|
||||
try {
|
||||
Integer sessionId = livyHelper.createSession(interpreterContext, "spark");
|
||||
|
||||
collector.checkThat("check sessionId", 1, CoreMatchers.equalTo(sessionId));
|
||||
|
||||
} catch (Exception e) {
|
||||
collector.addError(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void checkInterpret() {
|
||||
try {
|
||||
InterpreterResult result = livyHelper.interpret("print(1)", interpreterContext, interpreter.userSessionMap);
|
||||
|
||||
collector.checkThat("check sessionId", InterpreterResult.Code.SUCCESS, CoreMatchers.equalTo(result.code()));
|
||||
|
||||
} catch (Exception e) {
|
||||
collector.addError(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -32,18 +32,17 @@ import org.mockito.Mockito;
|
|||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Properties;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class LivyPySparkInterpreterTest {
|
||||
public class LivyInterpreterTest {
|
||||
|
||||
@Rule
|
||||
public ErrorCollector collector = new ErrorCollector();
|
||||
|
||||
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
private static LivyPySparkInterpreter interpreter;
|
||||
|
||||
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
|
||||
|
|
@ -56,24 +55,24 @@ public class LivyPySparkInterpreterTest {
|
|||
|
||||
@Before
|
||||
public void prepareContext() throws Exception {
|
||||
interpreter = Mockito.mock(LivyPySparkInterpreter.class);
|
||||
interpreter = new LivyPySparkInterpreter(new Properties());
|
||||
interpreter.userSessionMap = new HashMap<>();
|
||||
interpreter.userSessionMap.put(null, 0);
|
||||
interpreter.livyHelper = Mockito.mock(LivyHelper.class);
|
||||
interpreter.open();
|
||||
|
||||
doReturn(0).when(interpreter.livyHelper).createSession(interpreterContext, "");
|
||||
doReturn(new InterpreterResult(InterpreterResult.Code.SUCCESS)).when(interpreter).interpret("print \"x is 1.\"", interpreterContext);
|
||||
doReturn(new InterpreterResult(InterpreterResult.Code.SUCCESS)).when(interpreter.livyHelper)
|
||||
.interpret("print \"x is 1.\"", interpreterContext, interpreter.userSessionMap);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void check_init_variables() throws Exception {
|
||||
public void checkInitVariables() throws Exception {
|
||||
collector.checkThat("Check that, if userSessionMap is made: ",
|
||||
interpreter.userSessionMap, CoreMatchers.notNullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void check_basic_pyspark() throws Exception {
|
||||
public void checkBasicInterpreter() throws Exception {
|
||||
|
||||
String paragraphString = "print \"x is 1.\"";
|
||||
|
||||
Loading…
Reference in a new issue