Add tests :: API and REST API

This commit is contained in:
Anthony Corbacho 2016-10-25 13:55:25 +09:00
parent 23ad18e11a
commit 6f17b18639
2 changed files with 146 additions and 4 deletions

View file

@ -0,0 +1,120 @@
/*
* 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.rest;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.lang.StringUtils;
import org.apache.zeppelin.user.AuthenticationInfo;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
/**
* NotebookRepo rest api test.
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class NotebookRepoRestApiTest extends AbstractTestRestApi {
Gson gson = new Gson();
AuthenticationInfo anonymous;
@BeforeClass
public static void init() throws Exception {
AbstractTestRestApi.startUp();
}
@AfterClass
public static void destroy() throws Exception {
AbstractTestRestApi.shutDown();
}
@Before
public void setUp() {
anonymous = new AuthenticationInfo("anonymous");
}
private List<Map<String, Object>> getListOfReposotiry() throws IOException {
GetMethod get = httpGet("/notebook-repositories");
Map<String, Object> responce = gson.fromJson(get.getResponseBodyAsString(), new TypeToken<Map<String, Object>>() {}.getType());
get.releaseConnection();
return (List<Map<String, Object>>) responce.get("body");
}
private void updateNotebookRepoWithNewSetting(String payload) throws IOException {
PutMethod put = httpPut("/notebook-repositories", payload);
int status = put.getStatusCode();
put.releaseConnection();
assertThat(status, is(200));
}
@Test public void ThatCanGetNotebookRepositoiesSettings() throws IOException {
List<Map<String, Object>> listOfRepositories = getListOfReposotiry();
assertThat(listOfRepositories.size(), is(not(0)));
}
@Test public void setNewDirectoryForLocalDirectory() throws IOException {
List<Map<String, Object>> listOfRepositories = getListOfReposotiry();
String localVfs = StringUtils.EMPTY;
String className = StringUtils.EMPTY;
for (int i = 0; i < listOfRepositories.size(); i++) {
if (listOfRepositories.get(i).get("name").equals("VFSNotebookRepo")) {
localVfs = (String) ((List<Map<String, Object>>)listOfRepositories.get(i).get("settings")).get(0).get("selected");
className = (String) listOfRepositories.get(i).get("className");
break;
}
}
if (StringUtils.isBlank(localVfs)) {
// no loval VFS set...
return;
}
String payload = "{ \"name\": \"" + className + "\", \"settings\" : { \"Notebook Path\" : \"/tmp/newDir\" } }";
updateNotebookRepoWithNewSetting(payload);
// Verify
listOfRepositories = getListOfReposotiry();
String updatedPath = StringUtils.EMPTY;
for (int i = 0; i < listOfRepositories.size(); i++) {
if (listOfRepositories.get(i).get("name").equals("VFSNotebookRepo")) {
updatedPath = (String) ((List<Map<String, Object>>)listOfRepositories.get(i).get("settings")).get(0).get("selected");
break;
}
}
assertThat(updatedPath, is("/tmp/newDir"));
// go back to normal
payload = "{ \"name\": \"" + className + "\", \"settings\" : { \"Notebook Path\" : \"" + localVfs + "\" } }";
updateNotebookRepoWithNewSetting(payload);
}
}

View file

@ -22,6 +22,7 @@ import static org.mockito.Mockito.mock;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.FileUtils;
@ -31,12 +32,13 @@ import org.apache.zeppelin.dep.DependencyResolver;
import org.apache.zeppelin.interpreter.InterpreterFactory;
import org.apache.zeppelin.interpreter.InterpreterOption;
import org.apache.zeppelin.interpreter.mock.MockInterpreter1;
import org.apache.zeppelin.notebook.*;
import org.apache.zeppelin.notebook.repo.zeppelinhub.security.Authentication;
import org.apache.zeppelin.scheduler.JobListener;
import org.apache.zeppelin.notebook.JobListenerFactory;
import org.apache.zeppelin.notebook.Note;
import org.apache.zeppelin.notebook.Notebook;
import org.apache.zeppelin.notebook.Paragraph;
import org.apache.zeppelin.notebook.ParagraphJobListener;
import org.apache.zeppelin.scheduler.SchedulerFactory;
import org.apache.zeppelin.search.SearchService;
import org.apache.zeppelin.search.LuceneSearch;
import org.apache.zeppelin.user.AuthenticationInfo;
import org.junit.After;
import org.junit.Before;
@ -44,6 +46,8 @@ import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.collect.ImmutableMap;
public class VFSNotebookRepoTest implements JobListenerFactory {
private static final Logger LOG = LoggerFactory.getLogger(VFSNotebookRepoTest.class);
private ZeppelinConfiguration conf;
@ -141,6 +145,24 @@ public class VFSNotebookRepoTest implements JobListenerFactory {
assertEquals(note.getName(), "SaveTest");
notebookRepo.remove(note.getId(), null);
}
@Test
public void testUpdateSettings() throws IOException {
AuthenticationInfo subject = new AuthenticationInfo("anonymous");
File tmpDir = File.createTempFile("temp", Long.toString(System.nanoTime()));
Map<String, String> settings = ImmutableMap.of("Notebook Path", tmpDir.getAbsolutePath());
List<NotebookRepoSettingsInfo> repoSettings = notebookRepo.getSettings(subject);
String originalDir = repoSettings.get(0).selected;
notebookRepo.updateSettings(settings, subject);
repoSettings = notebookRepo.getSettings(subject);
assertEquals(repoSettings.get(0).selected, tmpDir.getAbsolutePath());
// restaure
notebookRepo.updateSettings(ImmutableMap.of("Notebook Path", originalDir), subject);
FileUtils.deleteQuietly(tmpDir);
}
class NotebookWriter implements Runnable {
Note note;