public List<CIBuild> getBuilds(CIJob job) throws PhrescoException { if (debugEnabled) { S_LOGGER.debug("Entering Method CIManagerImpl.getCIBuilds(CIJob job)"); } List<CIBuild> ciBuilds = null; try { if (debugEnabled) { S_LOGGER.debug("getCIBuilds() JobName = " + job.getName()); } JsonArray jsonArray = getBuildsArray(job); ciBuilds = new ArrayList<CIBuild>(jsonArray.size()); Gson gson = new Gson(); CIBuild ciBuild = null; for (int i = 0; i < jsonArray.size(); i++) { ciBuild = gson.fromJson(jsonArray.get(i), CIBuild.class); setBuildStatus(ciBuild, job); String buildUrl = ciBuild.getUrl(); String jenkinUrl = job.getJenkinsUrl() + ":" + job.getJenkinsPort(); buildUrl = buildUrl.replaceAll( "localhost:" + job.getJenkinsPort(), jenkinUrl); // when displaying url it should display setup machine ip ciBuild.setUrl(buildUrl); ciBuilds.add(ciBuild); } } catch (Exception e) { if (debugEnabled) { S_LOGGER.debug( "Entering Method CIManagerImpl.getCIBuilds(CIJob job) " + e.getLocalizedMessage()); } } return ciBuilds; }
private int getTotalBuilds(CIJob job) throws PhrescoException { try { S_LOGGER.debug("Entering Method CIManagerImpl.getTotalBuilds(CIJob job)"); S_LOGGER.debug("getCIBuilds() JobName = " + job.getName()); JsonArray jsonArray = getBuildsArray(job); Gson gson = new Gson(); CIBuild ciBuild = null; if (jsonArray.size() > 0) { ciBuild = gson.fromJson(jsonArray.get(0), CIBuild.class); String buildUrl = ciBuild.getUrl(); String jenkinsUrl = job.getJenkinsUrl() + ":" + job.getJenkinsPort(); // display the jenkins running url in ci buildUrl = buildUrl.replaceAll("localhost:" + job.getJenkinsPort(), jenkinsUrl); // list String response = getJsonResponse(buildUrl + API_JSON); JsonParser parser = new JsonParser(); JsonElement jsonElement = parser.parse(response); JsonObject jsonObject = jsonElement.getAsJsonObject(); JsonElement resultJson = jsonObject.get(FrameworkConstants.CI_JOB_BUILD_RESULT); JsonArray asJsonArray = jsonObject.getAsJsonArray(FrameworkConstants.CI_JOB_BUILD_ARTIFACTS); // when build result is not known if (jsonObject.get(FrameworkConstants.CI_JOB_BUILD_RESULT).toString().equals(STRING_NULL)) { // it indicates the job is in progress and not yet completed return -1; // when build is success and build zip relative path is unknown } else if (resultJson.getAsString().equals(CI_SUCCESS_FLAG) && asJsonArray.size() < 1) { return -1; } else { return jsonArray.size(); } } else { return -1; // When the project is build first time, } } catch (ClientHandlerException ex) { S_LOGGER.error(ex.getLocalizedMessage()); throw new PhrescoException(ex); } }
@Test public void testCreateFailures() { // Delayed invalid JSON JsonArray array = null; try { array = JsonParser.parseArray("[{]", true); } catch (JsonParseException e) { fail("This shouldn't happen!"); } // get(int) try { array.get(0); fail("Didn't throw an exception on delayed invalid JSON"); } catch (JsonParseException e) { } catch (Exception e) { e.printStackTrace(); fail("Wrong exception!"); } // getObject(int) try { array.getObject(0); fail("Didn't throw an exception on delayed invalid JSON"); } catch (JsonParseException e) { } catch (Exception e) { fail("Wrong exception!"); } // getArray(int) try { array.getArray(0); fail("Didn't throw an exception on delayed invalid JSON"); } catch (JsonParseException e) { } catch (Exception e) { fail("Wrong exception!"); } // getString(int) try { array.getString(0); fail("Didn't throw an exception on delayed invalid JSON"); } catch (JsonParseException e) { } catch (Exception e) { fail("Wrong exception!"); } // getLong(int) try { array.getLong(0); fail("Didn't throw an exception on delayed invalid JSON"); } catch (JsonParseException e) { } catch (Exception e) { fail("Wrong exception!"); } // getDouble(int) try { array.getDouble(0); fail("Didn't throw an exception on delayed invalid JSON"); } catch (JsonParseException e) { } catch (Exception e) { fail("Wrong exception!"); } // getBoolean(int) try { array.getBoolean(0); fail("Didn't throw an exception on delayed invalid JSON"); } catch (JsonParseException e) { } catch (Exception e) { fail("Wrong exception!"); } }
/** ************* Value tests * ************* */ @Test public void testValues() { // Longs String[] objectsJson = {"[{}]", "[{}, {}]", "[{}, {}, {}]"}; JsonObject[][] objectsEx = { {new JsonObject()}, {new JsonObject(), new JsonObject()}, {new JsonObject(), new JsonObject(), new JsonObject()} }; for (int objectsJsoff = 0; objectsJsoff < objectsJson.length; objectsJsoff++) { String json = objectsJson[objectsJsoff]; JsonObject[] ex = objectsEx[objectsJsoff]; JsonArray parsed = new TestArray(json, false); assertEquals(ex.length, parsed.size()); for (int e = 0; e < ex.length; e++) { // Valid assertEquals(ex[e], ((JsonObject) parsed.get(e))); assertEquals(ex[e], parsed.getObject(e)); // Invalid invalidValueHelper(parsed, "getArray", e); invalidValueHelper(parsed, "getString", e); invalidValueHelper(parsed, "getLong", e); invalidValueHelper(parsed, "getInt", e); invalidValueHelper(parsed, "getDouble", e); invalidValueHelper(parsed, "getFloat", e); invalidValueHelper(parsed, "getBoolean", e); } } // Strings String[] stringsJson = { "[\"test1\"]", "[\"test1\", \"test2\"]", "[\"test1\", \"test2\", \"test3\"]" }; String[][] stringsEx = {{"test1"}, {"test1", "test2"}, {"test1", "test2", "test3"}}; for (int stringsJsoff = 0; stringsJsoff < stringsJson.length; stringsJsoff++) { String json = stringsJson[stringsJsoff]; String[] ex = stringsEx[stringsJsoff]; JsonArray parsed = new TestArray(json, false); assertEquals(ex.length, parsed.size()); for (int e = 0; e < ex.length; e++) { // Valid assertEquals(ex[e], (String) parsed.get(e)); assertEquals(ex[e], parsed.getString(e)); // Invalid invalidValueHelper(parsed, "getObject", e); invalidValueHelper(parsed, "getArray", e); invalidValueHelper(parsed, "getLong", e); invalidValueHelper(parsed, "getInt", e); invalidValueHelper(parsed, "getDouble", e); invalidValueHelper(parsed, "getFloat", e); invalidValueHelper(parsed, "getBoolean", e); } } // Longs String[] longsJson = {"[1]", "[1, 2]", "[1, 2, 3]"}; long[][] longsEx = {{1}, {1, 2}, {1, 2, 3}}; for (int intsJsoff = 0; intsJsoff < longsJson.length; intsJsoff++) { String json = longsJson[intsJsoff]; long[] ex = longsEx[intsJsoff]; JsonArray parsed = new TestArray(json, false); assertEquals(ex.length, parsed.size()); for (int e = 0; e < ex.length; e++) { // Valid assertEquals(ex[e], ((Long) parsed.get(e)).longValue()); assertEquals(ex[e], parsed.getLong(e).longValue()); assertEquals(ex[e], parsed.getInt(e).intValue()); // Invalid invalidValueHelper(parsed, "getObject", e); invalidValueHelper(parsed, "getArray", e); invalidValueHelper(parsed, "getString", e); invalidValueHelper(parsed, "getDouble", e); invalidValueHelper(parsed, "getFloat", e); invalidValueHelper(parsed, "getBoolean", e); } } // Floats String[] floatsJson = {"[1.1]", "[1.1, 2.2f]", "[1.1, 2.2f, 3.3d]"}; float[][] floatsEx = {{1.1f}, {1.1f, 2.2f}, {1.1f, 2.2f, 3.3f}}; for (int floatsJsoff = 0; floatsJsoff < floatsJson.length; floatsJsoff++) { String json = floatsJson[floatsJsoff]; float[] ex = floatsEx[floatsJsoff]; JsonArray parsed = new TestArray(json, false); assertEquals(ex.length, parsed.size()); for (int e = 0; e < ex.length; e++) { // Valid assertEquals(ex[e], ((Double) parsed.get(e)).doubleValue(), 0.01); assertEquals(ex[e], parsed.getDouble(e).doubleValue(), 0.01); assertEquals(ex[e], parsed.getFloat(e).floatValue(), 0.01f); // Invalid invalidValueHelper(parsed, "getObject", e); invalidValueHelper(parsed, "getArray", e); invalidValueHelper(parsed, "getString", e); invalidValueHelper(parsed, "getLong", e); invalidValueHelper(parsed, "getInt", e); invalidValueHelper(parsed, "getBoolean", e); } } // Booleans String[] boolJson = {"[true]", "[true, false]", "[true, false, true]"}; boolean[][] boolEx = {{true}, {true, false}, {true, false, true}}; for (int boolsJsoff = 0; boolsJsoff < boolJson.length; boolsJsoff++) { String json = boolJson[boolsJsoff]; boolean[] ex = boolEx[boolsJsoff]; JsonArray parsed = new TestArray(json, false); assertEquals(ex.length, parsed.size()); for (int e = 0; e < ex.length; e++) { // Valid assertEquals(ex[e], ((Boolean) parsed.get(e)).booleanValue()); assertEquals(ex[e], parsed.getBoolean(e).booleanValue()); // Invalid invalidValueHelper(parsed, "getObject", e); invalidValueHelper(parsed, "getArray", e); invalidValueHelper(parsed, "getString", e); invalidValueHelper(parsed, "getLong", e); invalidValueHelper(parsed, "getInt", e); invalidValueHelper(parsed, "getDouble", e); invalidValueHelper(parsed, "getFloat", e); } } // Nulls String[] nullJson = {"[null]", "[null, null]", "[null, null, null]"}; Object[][] nullEx = { {null}, {null, null}, {null, null, null} }; // Should I really be expecting anything else? for (int nullJsoff = 0; nullJsoff < nullJson.length; nullJsoff++) { String json = nullJson[nullJsoff]; Object[] ex = nullEx[nullJsoff]; JsonArray parsed = new TestArray(json, false); assertEquals(ex.length, parsed.size()); for (int e = 0; e < ex.length; e++) { // Valid assertNull(parsed.get(e)); assertNull(parsed.getObject(e)); assertNull(parsed.getArray(e)); assertNull(parsed.getString(e)); assertNull(parsed.getLong(e)); assertNull(parsed.getInt(e)); assertNull(parsed.getDouble(e)); assertNull(parsed.getFloat(e)); assertNull(parsed.getBoolean(e)); } } }
/** **************** Creation tests * **************** */ @Test public void testCreate() { try { // Constructors JsonArray array = new JsonArray(); assertEquals(0, array.size()); JsonArray array2 = new JsonArray(57); assertEquals(0, array2.size()); // add(Object) + get(int) Object addObj = new Object(); array.add(addObj); assertEquals(1, array.size()); assertEquals(addObj, array.get(0)); Object addObj2 = new Object(); array.add(addObj2); assertEquals(2, array.size()); assertEquals(addObj2, array.get(1)); // add(JsonObject) + getObject(int) JsonObject addJObj = new JsonObject(); array.add(addJObj); assertEquals(3, array.size()); assertEquals(addJObj, array.getObject(2)); // add(JsonArray) + getArray(int) JsonArray addJArray = new JsonArray(); array.add(addJArray); assertEquals(4, array.size()); assertEquals(addJArray, array.getArray(3)); // add(String) + getString(int) String addStr = "Kawaii desu!"; array.add(addStr); assertEquals(5, array.size()); assertEquals(addStr, array.getString(4)); // add(int) + getLong(int) int addInt = 1_2_4_8; array.add(addInt); assertEquals(6, array.size()); assertEquals(addInt, array.getLong(5).longValue()); // add(long) + getLong(int) long addLong = 1_2_4_8_16_32_64; array.add(addLong); assertEquals(7, array.size()); assertEquals(addLong, array.getLong(6).longValue()); // add(float) + getDouble(int) float addFloat = 1_2_4_8.0f; array.add(addFloat); assertEquals(8, array.size()); assertEquals(addFloat, array.getDouble(7).doubleValue(), 0.01); // add(double) + getDouble(int) double addDouble = 1_2_4_8.16_32_64; array.add(addDouble); assertEquals(9, array.size()); assertEquals(addDouble, array.getDouble(8).doubleValue(), 0.0000001); // add(boolean) + getBoolean(int) boolean addBoolean = true; array.add(addBoolean); assertEquals(10, array.size()); assertTrue(array.getBoolean(9)); // Clone constructor JsonArray array3 = new JsonArray(array); assertEquals(10, array3.size()); assertEquals(addObj, array3.get(0)); assertEquals(addObj2, array3.get(1)); assertEquals(addJObj, array3.getObject(2)); assertEquals(addJArray, array3.getArray(3)); assertEquals(addStr, array3.getString(4)); assertEquals(addInt, array3.getLong(5).longValue()); assertEquals(addLong, array3.getLong(6).longValue()); assertEquals(addFloat, array3.getDouble(7).doubleValue(), 0.01); assertEquals(addDouble, array3.getDouble(8).doubleValue(), 0.0000001); assertTrue(array3.getBoolean(9)); } catch (JsonException e) { fail("This shouldn't happen!"); } }