Search in sources :

Example 11 with Gson

use of com.google.gson.Gson in project zeppelin by apache.

the class DistributedResourcePoolTest method testRemoteDistributedResourcePool.

@Test
public void testRemoteDistributedResourcePool() {
    Gson gson = new Gson();
    InterpreterResult ret;
    intp1.interpret("put key1 value1", context);
    intp2.interpret("put key2 value2", context);
    ret = intp1.interpret("getAll", context);
    assertEquals(2, gson.fromJson(ret.message().get(0).getData(), ResourceSet.class).size());
    ret = intp2.interpret("getAll", context);
    assertEquals(2, gson.fromJson(ret.message().get(0).getData(), ResourceSet.class).size());
    ret = intp1.interpret("get key1", context);
    assertEquals("value1", gson.fromJson(ret.message().get(0).getData(), String.class));
    ret = intp1.interpret("get key2", context);
    assertEquals("value2", gson.fromJson(ret.message().get(0).getData(), String.class));
}
Also used : Gson(com.google.gson.Gson) Test(org.junit.Test)

Example 12 with Gson

use of com.google.gson.Gson in project zeppelin by apache.

the class InterpreterRestApiTest method testCreatedInterpreterDependencies.

@Test
public void testCreatedInterpreterDependencies() throws IOException {
    // when: Create 2 interpreter settings `md1` and `md2` which have different dep.
    String md1Name = "md1";
    String md2Name = "md2";
    String md1Dep = "org.apache.drill.exec:drill-jdbc:jar:1.7.0";
    String md2Dep = "org.apache.drill.exec:drill-jdbc:jar:1.6.0";
    String reqBody1 = "{\"name\":\"" + md1Name + "\",\"group\":\"md\",\"properties\":{\"propname\":\"propvalue\"}," + "\"interpreterGroup\":[{\"class\":\"org.apache.zeppelin.markdown.Markdown\",\"name\":\"md\"}]," + "\"dependencies\":[ {\n" + "      \"groupArtifactVersion\": \"" + md1Dep + "\",\n" + "      \"exclusions\":[]\n" + "    }]," + "\"option\": { \"remote\": true, \"session\": false }}";
    PostMethod post = httpPost("/interpreter/setting", reqBody1);
    assertThat("test create method:", post, isAllowed());
    post.releaseConnection();
    String reqBody2 = "{\"name\":\"" + md2Name + "\",\"group\":\"md\",\"properties\":{\"propname\":\"propvalue\"}," + "\"interpreterGroup\":[{\"class\":\"org.apache.zeppelin.markdown.Markdown\",\"name\":\"md\"}]," + "\"dependencies\":[ {\n" + "      \"groupArtifactVersion\": \"" + md2Dep + "\",\n" + "      \"exclusions\":[]\n" + "    }]," + "\"option\": { \"remote\": true, \"session\": false }}";
    post = httpPost("/interpreter/setting", reqBody2);
    assertThat("test create method:", post, isAllowed());
    post.releaseConnection();
    // 1. Call settings API
    GetMethod get = httpGet("/interpreter/setting");
    String rawResponse = get.getResponseBodyAsString();
    get.releaseConnection();
    // 2. Parsing to List<InterpreterSettings>
    JsonObject responseJson = gson.fromJson(rawResponse, JsonElement.class).getAsJsonObject();
    JsonArray bodyArr = responseJson.getAsJsonArray("body");
    List<InterpreterSetting> settings = new Gson().fromJson(bodyArr, new TypeToken<ArrayList<InterpreterSetting>>() {
    }.getType());
    // 3. Filter interpreters out we have just created
    InterpreterSetting md1 = null;
    InterpreterSetting md2 = null;
    for (InterpreterSetting setting : settings) {
        if (md1Name.equals(setting.getName())) {
            md1 = setting;
        } else if (md2Name.equals(setting.getName())) {
            md2 = setting;
        }
    }
    // then: should get created interpreters which have different dependencies
    // 4. Validate each md interpreter has its own dependencies
    assertEquals(1, md1.getDependencies().size());
    assertEquals(1, md2.getDependencies().size());
    assertEquals(md1Dep, md1.getDependencies().get(0).getGroupArtifactVersion());
    assertEquals(md2Dep, md2.getDependencies().get(0).getGroupArtifactVersion());
}
Also used : JsonArray(com.google.gson.JsonArray) PostMethod(org.apache.commons.httpclient.methods.PostMethod) JsonElement(com.google.gson.JsonElement) TypeToken(com.google.gson.reflect.TypeToken) InterpreterSetting(org.apache.zeppelin.interpreter.InterpreterSetting) GetMethod(org.apache.commons.httpclient.methods.GetMethod) JsonObject(com.google.gson.JsonObject) Gson(com.google.gson.Gson) Test(org.junit.Test)

Example 13 with Gson

use of com.google.gson.Gson in project zeppelin by apache.

the class Note method addCloneParagraph.

/**
   * Clone paragraph and add it to note.
   *
   * @param srcParagraph source paragraph
   */
void addCloneParagraph(Paragraph srcParagraph) {
    // Keep paragraph original ID
    final Paragraph newParagraph = new Paragraph(srcParagraph.getId(), this, this, factory, interpreterSettingManager);
    Map<String, Object> config = new HashMap<>(srcParagraph.getConfig());
    Map<String, Object> param = new HashMap<>(srcParagraph.settings.getParams());
    Map<String, Input> form = new HashMap<>(srcParagraph.settings.getForms());
    newParagraph.setConfig(config);
    newParagraph.settings.setParams(param);
    newParagraph.settings.setForms(form);
    newParagraph.setText(srcParagraph.getText());
    newParagraph.setTitle(srcParagraph.getTitle());
    try {
        Gson gson = new Gson();
        String resultJson = gson.toJson(srcParagraph.getReturn());
        InterpreterResult result = gson.fromJson(resultJson, InterpreterResult.class);
        newParagraph.setReturn(result, null);
    } catch (Exception e) {
        // 'result' part of Note consists of exception, instead of actual interpreter results
        logger.warn("Paragraph " + srcParagraph.getId() + " has a result with exception. " + e.getMessage());
    }
    synchronized (paragraphs) {
        paragraphs.add(newParagraph);
    }
    if (noteEventListener != null) {
        noteEventListener.onParagraphCreate(newParagraph);
    }
}
Also used : Input(org.apache.zeppelin.display.Input) HashMap(java.util.HashMap) Gson(com.google.gson.Gson) AngularObject(org.apache.zeppelin.display.AngularObject) IOException(java.io.IOException)

Example 14 with Gson

use of com.google.gson.Gson in project zeppelin by apache.

the class Notebook method exportNote.

/**
   * Export existing note.
   *
   * @param noteId - the note ID to clone
   * @return Note JSON
   * @throws IOException, IllegalArgumentException
   */
public String exportNote(String noteId) throws IOException, IllegalArgumentException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setPrettyPrinting();
    Gson gson = gsonBuilder.create();
    Note note = getNote(noteId);
    if (note == null) {
        throw new IllegalArgumentException(noteId + " not found");
    }
    return gson.toJson(note);
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson)

Example 15 with Gson

use of com.google.gson.Gson in project zeppelin by apache.

the class Notebook method importNote.

/**
   * import JSON as a new note.
   *
   * @param sourceJson - the note JSON to import
   * @param noteName   - the name of the new note
   * @return note ID
   * @throws IOException
   */
public Note importNote(String sourceJson, String noteName, AuthenticationInfo subject) throws IOException {
    GsonBuilder gsonBuilder = new GsonBuilder();
    gsonBuilder.setPrettyPrinting();
    Gson gson = gsonBuilder.registerTypeAdapter(Date.class, new NotebookImportDeserializer()).create();
    JsonReader reader = new JsonReader(new StringReader(sourceJson));
    reader.setLenient(true);
    Note newNote;
    try {
        Note oldNote = gson.fromJson(reader, Note.class);
        convertFromSingleResultToMultipleResultsFormat(oldNote);
        newNote = createNote(subject);
        if (noteName != null)
            newNote.setName(noteName);
        else
            newNote.setName(oldNote.getName());
        List<Paragraph> paragraphs = oldNote.getParagraphs();
        for (Paragraph p : paragraphs) {
            newNote.addCloneParagraph(p);
        }
        notebookAuthorization.setNewNotePermissions(newNote.getId(), subject);
        newNote.persist(subject);
    } catch (IOException e) {
        logger.error(e.toString(), e);
        throw e;
    }
    return newNote;
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) StringReader(java.io.StringReader) Gson(com.google.gson.Gson) JsonReader(com.google.gson.stream.JsonReader) IOException(java.io.IOException) Date(java.util.Date)

Aggregations

Gson (com.google.gson.Gson)1060 Test (org.junit.Test)236 HashMap (java.util.HashMap)200 JsonObject (com.google.gson.JsonObject)138 GsonBuilder (com.google.gson.GsonBuilder)135 CommandWrapper (ClientServerApi.CommandWrapper)123 CommandExecuter (CommandHandler.CommandExecuter)119 CriticalError (SQLDatabase.SQLDatabaseException.CriticalError)118 IOException (java.io.IOException)116 JsonSyntaxException (com.google.gson.JsonSyntaxException)100 ClientNotConnected (SQLDatabase.SQLDatabaseException.ClientNotConnected)96 ArrayList (java.util.ArrayList)94 Type (java.lang.reflect.Type)65 JsonElement (com.google.gson.JsonElement)57 ProductNotExistInCatalog (SQLDatabase.SQLDatabaseException.ProductNotExistInCatalog)53 SmartCode (BasicCommonClasses.SmartCode)50 Map (java.util.Map)45 InputStreamReader (java.io.InputStreamReader)41 BroadcastClientMessage (org.bigbluebutton.red5.client.messaging.BroadcastClientMessage)41 Location (BasicCommonClasses.Location)40