List of usage examples for java.net HttpURLConnection HTTP_NOT_FOUND
int HTTP_NOT_FOUND
To view the source code for java.net HttpURLConnection HTTP_NOT_FOUND.
Click Source Link
From source file:net.myrrix.client.ClientRecommender.java
/** * <p>Lists the items that were most influential in recommending a given item to a given user. Exactly how this * is determined is left to the implementation, but, generally this will return items that the user prefers * and that are similar to the given item.</p> * * <p>These values by which the results are ordered are opaque values and have no interpretation * other than that larger means stronger.</p> * * @param userID ID of user who was recommended the item * @param itemID ID of item that was recommended * @param howMany maximum number of items to return * @return {@link List} of {@link RecommendedItem}, ordered from most influential in recommended the given * item to least/*from ww w .j ava 2 s . c om*/ * @throws NoSuchUserException if the user is not known in the model * @throws NoSuchItemException if the item is not known in the model * @throws NotReadyException if the recommender has no model available yet * @throws TasteException if another error occurs */ @Override public List<RecommendedItem> recommendedBecause(long userID, long itemID, int howMany) throws TasteException { String urlPath = "/because/" + userID + '/' + itemID + "?howMany=" + howMany; TasteException savedException = null; for (HostAndPort replica : choosePartitionAndReplicas(userID)) { HttpURLConnection connection = null; try { connection = buildConnectionToReplica(replica, urlPath, "GET"); switch (connection.getResponseCode()) { case HttpURLConnection.HTTP_OK: return consumeItems(connection); case HttpURLConnection.HTTP_NOT_FOUND: String connectionMessage = connection.getResponseMessage(); if (connectionMessage != null && connectionMessage.contains(NoSuchUserException.class.getSimpleName())) { throw new NoSuchUserException(userID); } else { throw new NoSuchItemException(itemID); } case HttpURLConnection.HTTP_UNAVAILABLE: throw new NotReadyException(); default: throw new TasteException(connection.getResponseCode() + " " + connection.getResponseMessage()); } } catch (TasteException te) { log.info("Can't access {} at {}: ({})", urlPath, replica, te.toString()); savedException = te; } catch (IOException ioe) { log.info("Can't access {} at {}: ({})", urlPath, replica, ioe.toString()); savedException = new TasteException(ioe); } finally { if (connection != null) { connection.disconnect(); } } } throw savedException; }
From source file:org.eclipse.rdf4j.http.client.SPARQLProtocolSession.java
protected HttpResponse execute(HttpUriRequest method) throws IOException, RDF4JException { boolean consume = true; if (params != null) { method.setParams(params);/*www .jav a2s.co m*/ } HttpResponse response = httpClient.execute(method, httpContext); try { int httpCode = response.getStatusLine().getStatusCode(); if (httpCode >= 200 && httpCode < 300 || httpCode == HttpURLConnection.HTTP_NOT_FOUND) { consume = false; return response; // everything OK, control flow can continue } else { switch (httpCode) { case HttpURLConnection.HTTP_UNAUTHORIZED: // 401 throw new UnauthorizedException(); case HttpURLConnection.HTTP_UNAVAILABLE: // 503 throw new QueryInterruptedException(); default: ErrorInfo errInfo = getErrorInfo(response); // Throw appropriate exception if (errInfo.getErrorType() == ErrorType.MALFORMED_DATA) { throw new RDFParseException(errInfo.getErrorMessage()); } else if (errInfo.getErrorType() == ErrorType.UNSUPPORTED_FILE_FORMAT) { throw new UnsupportedRDFormatException(errInfo.getErrorMessage()); } else if (errInfo.getErrorType() == ErrorType.MALFORMED_QUERY) { throw new MalformedQueryException(errInfo.getErrorMessage()); } else if (errInfo.getErrorType() == ErrorType.UNSUPPORTED_QUERY_LANGUAGE) { throw new UnsupportedQueryLanguageException(errInfo.getErrorMessage()); } else if (errInfo.toString().length() > 0) { throw new RepositoryException(errInfo.toString()); } else { throw new RepositoryException(response.getStatusLine().getReasonPhrase()); } } } } finally { if (consume) { EntityUtils.consumeQuietly(response.getEntity()); } } }
From source file:com.mobicage.rogerthat.Api.java
@SuppressWarnings("unchecked") private Object wireRequest(final JSONObject request, final int attempt) throws RogerthatAPIException { final URL url; try {//from w w w . j a v a 2 s . c om url = new URL(apiLocation); } catch (MalformedURLException e) { // Will never come here throw new RogerthatAPIException(e); } HttpURLConnection connection; try { if (proxyHost != null && proxyPort != 0) { // Adapt proxy settings Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); connection = (HttpURLConnection) url.openConnection(proxy); } else { connection = (HttpURLConnection) url.openConnection(); } } catch (IOException e) { throw new RogerthatAPIException(e); } try { connection.setDoOutput(true); try { connection.setRequestMethod("POST"); } catch (ProtocolException e) { // Will never come here throw new RogerthatAPIException(e); } connection.setRequestProperty("X-Nuntiuz-API-Key", apiKey); connection.setRequestProperty("Content-type", "application/json-rpc; charset=utf-8"); connection.setReadTimeout(30000); Writer writer; try { writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); try { String jsonString = request.toJSONString(); if (logging) log.info("Sending request to Rogerthat:\n" + jsonString); writer.write(jsonString); } finally { writer.close(); } switch (connection.getResponseCode()) { case HttpURLConnection.HTTP_OK: InputStream is = connection.getInputStream(); try { JSONObject result = (JSONObject) JSONValue .parse(new BufferedReader(new InputStreamReader(is, "UTF-8"))); if (logging) log.info("Result received from Rogerthat:\n" + result.toJSONString()); JSONObject error = (JSONObject) result.get("error"); if (error != null) throw new RogerthatAPIException(Integer.parseInt(error.get("code").toString()), (String) error.get("message"), error); else return result.get("result"); } finally { is.close(); } case HttpURLConnection.HTTP_UNAUTHORIZED: throw new RogerthatAPIException(1000, "Could not authenticate against Rogerth.at Messenger API! Check your api key.", null); case HttpURLConnection.HTTP_NOT_FOUND: throw new RogerthatAPIException(1000, "Rogerth.at Messenger API method not found!", null); default: if (attempt < 5) return wireRequest(request, attempt + 1); else throw new RogerthatAPIException(1000, "Could not send call to Rogerth.at Messenger!", null); } } catch (IOException e) { // Will never come here throw new RogerthatAPIException(e); } } finally { connection.disconnect(); } }
From source file:org.betaconceptframework.astroboa.resourceapi.resource.ContentObjectResource.java
private Response saveContentObjectString(String contentSource, String httpMethod, boolean entityIsNew, boolean updateLastModificationTime) { try {/*from w ww .j av a 2s . c om*/ //Must determine whether a single or a collection of objects is saved if (contentSource == null) { throw new WebApplicationException(HttpURLConnection.HTTP_BAD_REQUEST); } if (contentSource.contains(CmsConstants.RESOURCE_RESPONSE_PREFIXED_NAME) || contentSource.contains(CmsConstants.RESOURCE_COLLECTION)) { List<ContentObject> contentObjects = astroboaClient.getContentService() .saveContentObjectResourceCollection(contentSource, false, updateLastModificationTime, null); //TODO : Improve response details. ResponseBuilder responseBuilder = Response.status(Status.OK); responseBuilder.header("Content-Disposition", "inline"); responseBuilder.type(MediaType.TEXT_PLAIN + "; charset=utf-8"); return responseBuilder.build(); } else { ContentObject contentObject = astroboaClient.getContentService().save(contentSource, false, updateLastModificationTime, null); return ContentApiUtils.createResponseForPutOrPostOfACmsEntity(contentObject, httpMethod, contentSource, entityIsNew); } } catch (CmsUnauthorizedAccessException e) { throw new WebApplicationException(HttpURLConnection.HTTP_UNAUTHORIZED); } catch (Exception e) { logger.error("", e); throw new WebApplicationException(HttpURLConnection.HTTP_NOT_FOUND); } }
From source file:org.opendaylight.vtn.manager.it.northbound.VtnNorthboundIT.java
/** * Test case for VTN APIs.//from w ww . ja v a2 s . co m * * <p> * This calls {@link #testVBridgeAPI(String, String)}. * </p> * * @throws Exception An error occurred. */ @Test public void testVTNAPI() throws Exception { LOG.info("Starting VTN JAX-RS client."); String baseURL = VTN_BASE_URL; String tname1 = "testVtn1"; String tname2 = "testVtn_2"; String tname3 = "testVtn3"; String tname4 = "4"; String tname5 = "testVtnf"; String tname6 = "testVtn6"; String tname7 = "testVtn007testVtn007testVtn0007"; String tname8 = "testVtn8"; String tname = "testVtn"; String tname32 = "testVtn032testVtn032testVtn032te"; String desc1 = "testDescription1"; String desc2 = "2"; String desc3 = String.format("%01000d", 1); String itimeout1 = "100"; String itimeout2 = "250"; String htimeout1 = "200"; String htimeout2 = "400"; String timeout0 = "0"; String timeoutNegative = "-10"; String timeoutMax = "65535"; String timeoutOver = "65540"; // Test GET vtn in default container, expecting no results String result = getJsonResult(baseURL + "default/vtns"); assertResponse(HTTP_OK); JSONTokener jt = new JSONTokener(result); JSONObject json = new JSONObject(jt); JSONArray vtnArray = json.getJSONArray("vtn"); Assert.assertEquals(0, vtnArray.length()); // Test POST vtn1 String requestBody = "{}"; String requestUri = baseURL + "default/vtns/" + tname1; result = getJsonResult(requestUri, HTTP_POST, requestBody); assertResponse(HTTP_CREATED); Assert.assertEquals(requestUri, httpLocation); // Test POST vtn1, expecting 409 requestUri = baseURL + "default/vtns/" + tname1; result = getJsonResult(requestUri, HTTP_POST, requestBody); assertResponse(HTTP_CONFLICT); // Test GET vtn in default container, expecting one result result = getJsonResult(baseURL + "default/vtns"); assertResponse(HTTP_OK); jt = new JSONTokener(result); json = new JSONObject(jt); vtnArray = json.getJSONArray("vtn"); Assert.assertEquals(1, vtnArray.length()); // Test POST vtn2, setting "_" to vBridgeName requestBody = "{\"description\":\"" + desc1 + "\", \"idleTimeout\":\"" + itimeout1 + "\", \"hardTimeout\":\"" + htimeout1 + "\"}"; requestUri = baseURL + "default/vtns/" + tname2; result = getJsonResult(requestUri, HTTP_POST, requestBody); assertResponse(HTTP_CREATED); Assert.assertEquals(requestUri, httpLocation); // Test GET all vtns in default container result = getJsonResult(baseURL + "default/vtns"); jt = new JSONTokener(result); json = new JSONObject(jt); vtnArray = json.getJSONArray("vtn"); JSONObject vtn; assertResponse(HTTP_OK); Assert.assertEquals(2, vtnArray.length()); for (int i = 0; i < vtnArray.length(); i++) { vtn = vtnArray.getJSONObject(i); if (vtn.getString("name").equals(tname1)) { Assert.assertFalse(vtn.has("description")); Assert.assertEquals("300", vtn.getString("idleTimeout")); Assert.assertEquals("0", vtn.getString("hardTimeout")); } else if (vtn.getString("name").equals(tname2)) { Assert.assertEquals(desc1, vtn.getString("description")); Assert.assertEquals(itimeout1, vtn.getString("idleTimeout")); Assert.assertEquals(htimeout1, vtn.getString("hardTimeout")); } else { // Unexpected vtn name Assert.assertTrue(false); } } // Test POST vtn3. testing long description requestBody = "{\"idleTimeout\":\"" + itimeout1 + "\", \"hardTimeout\":\"" + htimeout1 + "\"}"; requestUri = baseURL + "default/vtns/" + tname3; result = getJsonResult(requestUri, HTTP_POST, requestBody); assertResponse(HTTP_CREATED); Assert.assertEquals(requestUri, httpLocation); // Test POST vtn4, setting idle timeout of negative value and one character numeric of vtn name requestBody = "{\"description\":\"" + desc3 + "\", \"idleTimeout\":\"" + timeoutNegative + "\"}"; requestUri = baseURL + "default/vtns/" + tname4; result = getJsonResult(requestUri, HTTP_POST, requestBody); assertResponse(HTTP_CREATED); Assert.assertEquals(requestUri, httpLocation); // Test GET vtn result = getJsonResult(baseURL + "default/vtns/" + tname4); jt = new JSONTokener(result); json = new JSONObject(jt); Assert.assertEquals("300", json.getString("idleTimeout")); Assert.assertEquals(timeout0, json.getString("hardTimeout")); // Test POST vtn5, testing hard timeout of negative value requestBody = "{\"description\":\"" + desc1 + "\", \"hardTimeout\":\"" + timeoutNegative + "\"}"; requestUri = baseURL + "default/vtns/" + tname5; result = getJsonResult(requestUri, HTTP_POST, requestBody); assertResponse(HTTP_CREATED); Assert.assertEquals(requestUri, httpLocation); // Test GET vtn result = getJsonResult(baseURL + "default/vtns/" + tname5); jt = new JSONTokener(result); json = new JSONObject(jt); Assert.assertEquals("300", json.getString("idleTimeout")); Assert.assertEquals(timeout0, json.getString("hardTimeout")); // Test POST vtn6, setting idle timeout of 0 and hard timeout of 65535 requestBody = "{\"description\":\"" + desc1 + "\", \"idleTimeout\":\"" + timeoutMax + "\", \"hardTimeout\":\"" + timeout0 + "\"}"; requestUri = baseURL + "default/vtns/" + tname6; result = getJsonResult(requestUri, HTTP_POST, requestBody); assertResponse(HTTP_CREATED); Assert.assertEquals(requestUri, httpLocation); // Test POST vtn7, setting idle timeout of 65535, hard timeout of 0 // and vtn name of 31 characters requestBody = "{\"description\":\"" + desc2 + "\", \"idleTimeout\":\"" + timeout0 + "\", \"hardTimeout\":\"" + timeoutMax + "\"}"; requestUri = baseURL + "default/vtns/" + tname7; result = getJsonResult(requestUri, HTTP_POST, requestBody); assertResponse(HTTP_CREATED); Assert.assertEquals(requestUri, httpLocation); // Test POST vtn8, setting invalid value after description to // requestBody requestBody = "{\"description\":\"" + desc1 + "\", \"Timeout\":\"" + timeout0 + "\", \"hard\":\"" + timeout0 + "\"}"; requestUri = baseURL + "default/vtns/" + tname8; // Ensure that query parameters are eliminated from Location. result = getJsonResult(requestUri + "?param1=1¶m2=2", HTTP_POST, requestBody); assertResponse(HTTP_CREATED); Assert.assertEquals(requestUri, httpLocation); // Test GET vtn result = getJsonResult(baseURL + "default/vtns/" + tname8); jt = new JSONTokener(result); json = new JSONObject(jt); Assert.assertEquals(desc1, json.getString("description")); Assert.assertEquals("300", json.getString("idleTimeout")); Assert.assertEquals(timeout0, json.getString("hardTimeout")); // Test POST vtn, expecting 400 requestBody = "{\"enabled\":\"true\"" + "\"description\":\"" + desc1 + "\"}"; result = getJsonResult(baseURL + "default/vtns/" + tname, HTTP_POST, requestBody); assertResponse(HTTP_BAD_REQUEST); // Test POST vtn, expecting 400, setting invalid value to idle timeout requestBody = "{\"description\":\"" + desc1 + "\", \"idleTimeout\":\"idletimeout\", \"hardTimeout\":\"" + htimeout1 + "\"}"; result = getJsonResult(baseURL + "default/vtns/" + tname, HTTP_POST, requestBody); assertResponse(HTTP_BAD_REQUEST); // Test POST vtn, expecting 400, setting invalid value to hard timeout requestBody = "{\"description\":\"" + desc1 + "\", \"idleTimeout\":\"" + itimeout1 + "\", \"hardTimeout\":\"hardtimeout\"}"; result = getJsonResult(baseURL + "default/vtns/" + tname, HTTP_POST, requestBody); assertResponse(HTTP_BAD_REQUEST); // Test POST vtn, expecting 400, setting invalid value to requestBody requestBody = "{\"description\":\"" + desc3 + "\", \"didleTimeout\":\"rdTimeout\":\"}"; result = getJsonResult(baseURL + "default/vtns/" + tname, HTTP_POST, requestBody); assertResponse(HTTP_BAD_REQUEST); // Test POST vtn expecting 105, test when vtn name is "" requestBody = "{}"; result = getJsonResult(baseURL + "default/vtns/" + "", HTTP_POST, requestBody); assertResponse(HTTP_BAD_METHOD); // Test POST vtn expecting 400, specifying invalid tenant name. requestUri = baseURL + "default/vtns/" + desc3; result = getJsonResult(requestUri, HTTP_POST, requestBody); assertResponse(HTTP_BAD_REQUEST); // Test POST vtn expecting 400, specifying invalid tenant name // which starts with "_". requestUri = baseURL + "default/vtns/" + "_testVtn"; result = getJsonResult(requestUri, HTTP_POST, requestBody); assertResponse(HTTP_BAD_REQUEST); // Test POST vtn expecting 400, specifying invalid tenant name // including symbol "@". requestUri = baseURL + "default/vtns/" + "test@Vtn"; result = getJsonResult(requestUri, HTTP_POST, requestBody); assertResponse(HTTP_BAD_REQUEST); // Test POST vtn expecting 400, specifying 65536 as idle timeout. requestBody = "{\"description\":\"" + desc1 + "\", \"idleTimeout\":\"65536\", \"hardTimeout\":\"" + htimeout1 + "\"}"; requestUri = baseURL + "default/vtns/" + tname; result = getJsonResult(requestUri, HTTP_POST, requestBody); assertResponse(HTTP_BAD_REQUEST); // Test POST vtn expecting 400, specifying 65536 as hard timeout. requestBody = "{\"description\":\"" + desc1 + "\", \"idleTimeout\":\"" + itimeout1 + "\", \"hardTimeout\":\"65536\"}"; requestUri = baseURL + "default/vtns/" + tname; result = getJsonResult(requestUri, HTTP_POST, requestBody); assertResponse(HTTP_BAD_REQUEST); // Test POST vtn expecting 400, specifying idle timeout value greater // than hard timeout. requestBody = "{\"description\":\"" + desc1 + "\", \"idleTimeout\":\"" + itimeout2 + "\", \"hardTimeout\":\"" + htimeout1 + "\"}"; requestUri = baseURL + "default/vtns/" + tname; result = getJsonResult(requestUri, HTTP_POST, requestBody); assertResponse(HTTP_BAD_REQUEST); // Test POST vtn expecting 400, specifying too long tenant name. requestBody = "{}"; requestUri = baseURL + "default/vtns/" + tname32; result = getJsonResult(requestUri, HTTP_POST, requestBody); assertResponse(HTTP_BAD_REQUEST); testVBridgeAPI(tname1, tname2); // Test GET vtn expecting 404, setting the vtn that don't exist result = getJsonResult(baseURL + "default/vtns/" + tname); assertResponse(HTTP_NOT_FOUND); // Test GET vtn result = getJsonResult(baseURL + "default/vtns/" + tname2); jt = new JSONTokener(result); json = new JSONObject(jt); assertResponse(HTTP_OK); Assert.assertEquals(tname2, json.getString("name")); Assert.assertEquals(desc1, json.getString("description")); Assert.assertEquals(itimeout1, json.getString("idleTimeout")); Assert.assertEquals(htimeout1, json.getString("hardTimeout")); // Test PUT vtn1, expecting all elements change requestBody = "{\"description\":\"" + desc3 + "\", \"idleTimeout\":\"" + timeout0 + "\", \"hardTimeout\":\"" + timeout0 + "\"}"; String queryParameter = new QueryParameter("all", "true").getString(); result = getJsonResult(baseURL + "default/vtns/" + tname1 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_OK); // Test GET vtn result = getJsonResult(baseURL + "default/vtns/" + tname1); jt = new JSONTokener(result); json = new JSONObject(jt); Assert.assertEquals(desc3, json.getString("description")); Assert.assertEquals(timeout0, json.getString("idleTimeout")); Assert.assertEquals(timeout0, json.getString("hardTimeout")); // Test PUT vtn1, abbreviate idle timeout and hard timeout requestBody = "{\"description\":\"" + desc2 + "\"}"; result = getJsonResult(baseURL + "default/vtns/" + tname1 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_OK); // Test GET vtn result = getJsonResult(baseURL + "default/vtns/" + tname1); jt = new JSONTokener(result); json = new JSONObject(jt); Assert.assertEquals(desc2, json.getString("description")); Assert.assertEquals("300", json.getString("idleTimeout")); Assert.assertEquals("0", json.getString("hardTimeout")); // Test PUT vtn1, abbreviate description, testing idle timeout of // 65535 and hard timeout of 0 requestBody = "{\"idleTimeout\":\"" + timeoutMax + "\", \"hardTimeout\":\"" + timeout0 + "\"}"; result = getJsonResult(baseURL + "default/vtns/" + tname1 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_OK); // Test GET vtn result = getJsonResult(baseURL + "default/vtns/" + tname1); jt = new JSONTokener(result); json = new JSONObject(jt); try { Assert.assertEquals("", json.getString("description")); } catch (JSONException expected) { assertThat(expected.getMessage(), is("JSONObject[\"description\"] not found.")); } Assert.assertEquals(timeoutMax, json.getString("idleTimeout")); Assert.assertEquals(timeout0, json.getString("hardTimeout")); // Test PUT vtn1, abbreviate description and idle timeout, testing // hard timeout of 65535 requestBody = "{\"hardTimeout\":\"" + timeoutMax + "\"}"; result = getJsonResult(baseURL + "default/vtns/" + tname1 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_OK); // Test GET vtn result = getJsonResult(baseURL + "default/vtns/" + tname1); jt = new JSONTokener(result); json = new JSONObject(jt); Assert.assertFalse(json.has("description")); Assert.assertEquals("300", json.getString("idleTimeout")); Assert.assertEquals(timeoutMax, json.getString("hardTimeout")); // Test PUT vtn1, abbreviate all elements requestBody = "{}"; result = getJsonResult(baseURL + "default/vtns/" + tname1 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_OK); // Test GET vtn result = getJsonResult(baseURL + "default/vtns/" + tname1); jt = new JSONTokener(result); json = new JSONObject(jt); Assert.assertFalse(json.has("description")); Assert.assertEquals("300", json.getString("idleTimeout")); Assert.assertEquals("0", json.getString("hardTimeout")); // Test PUT vtn2, expecting all elements not change queryParameter = new QueryParameter("all", "false").getString(); result = getJsonResult(baseURL + "default/vtns/" + tname2 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_OK); // Test GET vtn result = getJsonResult(baseURL + "default/vtns/" + tname2); jt = new JSONTokener(result); json = new JSONObject(jt); Assert.assertEquals(desc1, json.getString("description")); Assert.assertEquals(itimeout1, json.getString("idleTimeout")); Assert.assertEquals(htimeout1, json.getString("hardTimeout")); // Test PUT vtn2, setting 0 to idle timoeut and hard timeout requestBody = "{\"idleTimeout\":\"" + timeout0 + "\", \"hardTimeout\":\"" + timeout0 + "\"}"; result = getJsonResult(baseURL + "default/vtns/" + tname2 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_OK); // Test GET vtn result = getJsonResult(baseURL + "default/vtns/" + tname2); jt = new JSONTokener(result); json = new JSONObject(jt); Assert.assertEquals(desc1, json.getString("description")); Assert.assertEquals(timeout0, json.getString("idleTimeout")); Assert.assertEquals(timeout0, json.getString("hardTimeout")); // Test PUT vtn2, expecting all elements change requestBody = "{\"description\":\"" + desc2 + "\", \"idleTimeout\":\"" + itimeout1 + "\", \"hardTimeout\":\"" + htimeout1 + "\"}"; result = getJsonResult(baseURL + "default/vtns/" + tname2 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_OK); // Test GET vtn result = getJsonResult(baseURL + "default/vtns/" + tname2); jt = new JSONTokener(result); json = new JSONObject(jt); Assert.assertEquals(desc2, json.getString("description")); Assert.assertEquals(itimeout1, json.getString("idleTimeout")); Assert.assertEquals(htimeout1, json.getString("hardTimeout")); // Test PUT vtn2, expecting all elements not change requestBody = "{\"idleTimeout\":\"" + timeoutNegative + "\", \"hardTimeout\":\"" + timeoutNegative + "\"}"; result = getJsonResult(baseURL + "default/vtns/" + tname2 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_OK); // Test GET vtn result = getJsonResult(baseURL + "default/vtns/" + tname2); jt = new JSONTokener(result); json = new JSONObject(jt); Assert.assertEquals(desc2, json.getString("description")); Assert.assertEquals(itimeout1, json.getString("idleTimeout")); Assert.assertEquals(htimeout1, json.getString("hardTimeout")); // Test PUT vtn8, setting invalid value after description to requestBody requestBody = "{\"description\":\"" + desc2 + "\", \"Timeout\":\"" + itimeout1 + "\", \"hard\":\"" + htimeout2 + "\"}"; result = getJsonResult(baseURL + "default/vtns/" + tname8 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_OK); // Test GET vtn result = getJsonResult(baseURL + "default/vtns/" + tname8); jt = new JSONTokener(result); json = new JSONObject(jt); Assert.assertEquals(desc2, json.getString("description")); Assert.assertEquals("300", json.getString("idleTimeout")); Assert.assertEquals(timeout0, json.getString("hardTimeout")); // Test PUT vtn2, description not change queryParameter = new QueryParameter("all", "true").getString(); requestBody = "{\"idleTimeout\":\"" + timeoutNegative + "\", \"hardTimeout\":\"" + timeoutNegative + "\"}"; result = getJsonResult(baseURL + "default/vtns/" + tname2 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_OK); // Test GET vtn result = getJsonResult(baseURL + "default/vtns/" + tname2); jt = new JSONTokener(result); json = new JSONObject(jt); Assert.assertFalse(json.has("description")); Assert.assertEquals("300", json.getString("idleTimeout")); Assert.assertEquals(timeout0, json.getString("hardTimeout")); // Test PUT vtn, expecting 400, setting the invalid value to idle timeout requestBody = "{\"idleTimeout\":\"" + desc1 + "\", \"hardTimeout\":\"" + htimeout1 + "\"}"; result = getJsonResult(baseURL + "default/vtns/" + tname1 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_BAD_REQUEST); // Test PUT vtn, expecting 400, setting the invalid value to hard timeout requestBody = "{\"idleTimeout\":\"" + itimeout1 + "\", \"hardTimeout\":\"" + desc1 + "\"}"; result = getJsonResult(baseURL + "default/vtns/" + tname1 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_BAD_REQUEST); // Test PUT vtn, expecting 400, setting invalid value to requestBody requestBody = "{\"description\":\"" + desc3 + "\", \"didleTimeout\":\"rdTimeout\":\"}"; result = getJsonResult(baseURL + "default/vtns/" + tname1 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_BAD_REQUEST); // Test PUT vtn expecting 404, setting the vtn that don't exist requestBody = "{\"description\":\"" + desc2 + "\", \"idleTimeout\":\"" + itimeout1 + "\", \"hardTimeout\":\"" + htimeout1 + "\"}"; result = getJsonResult(baseURL + "default/vtns/" + tname + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_NOT_FOUND); // Test PUT vtn expecting 400, setting invalid value to requestBody requestBody = "{\"Test\"}"; result = getJsonResult(baseURL + "default/vtns/" + tname1 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_BAD_REQUEST); // Test PUT vtn expecting 400, specifying 65540 as idle timeout. requestBody = "{\"description\":\"" + desc2 + "\", \"idleTimeout\":\"" + timeoutOver + "\", \"hardTimeout\":\"" + htimeout2 + "\"}"; result = getJsonResult(baseURL + "default/vtns/" + tname1 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_BAD_REQUEST); // Test PUT vtn expecting 400, specifying 65540 as hard timeout. requestBody = "{\"description\":\"" + desc2 + "\", \"idleTimeout\":\"" + itimeout1 + "\", \"hardTimeout\":\"" + timeoutOver + "\"}"; result = getJsonResult(baseURL + "default/vtns/" + tname1 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_BAD_REQUEST); // Test PUT vtn expecting 400, specifying idle timeout value greater // than hard timeout. requestBody = "{\"description\":\"" + desc1 + "\", \"idleTimeout\":\"" + itimeout2 + "\", \"hardTimeout\":\"" + htimeout1 + "\"}"; result = getJsonResult(baseURL + "default/vtns/" + tname1 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_BAD_REQUEST); // Test PUT vtn requestBody = "{\"description\":\"" + desc2 + "\", \"idleTimeout\":\"" + itimeout2 + "\", \"hardTimeout\":\"" + timeoutNegative + "\"}"; result = getJsonResult(baseURL + "default/vtns/" + tname1 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_OK); // Test GET all vtns in default container result = getJsonResult(baseURL + "default/vtns"); jt = new JSONTokener(result); json = new JSONObject(jt); vtnArray = json.getJSONArray("vtn"); assertResponse(HTTP_OK); Assert.assertEquals(8, vtnArray.length()); // Test DELETE vtn expecting 404 result = getJsonResult(baseURL + "default/vtns/" + tname, HTTP_DELETE); assertResponse(HTTP_NOT_FOUND); // set not supported "Content-type". expect to return 415. requestBody = "{}"; result = getJsonResult(baseURL + "default/vtns/" + tname, HTTP_POST, requestBody, "text/plain"); assertResponse(HTTP_UNSUPPORTED_TYPE); requestBody = "{\"description\":\"desc\"}"; result = getJsonResult(baseURL + "default/vtns/" + tname1 + queryParameter, HTTP_PUT, requestBody, "text/plain"); assertResponse(HTTP_UNSUPPORTED_TYPE); // Authentication failure. checkVTNError(GlobalConstants.DEFAULT.toString(), tname1, false, HttpURLConnection.HTTP_UNAUTHORIZED); // Invalid container name. String[] invalid = { "bad_container", "version" }; for (String inv : invalid) { checkVTNError(inv, tname1, true, HttpURLConnection.HTTP_NOT_FOUND); checkVTNError(inv, tname1, false, HttpURLConnection.HTTP_UNAUTHORIZED); } // Test DELETE vtn result = getJsonResult(baseURL + "default/vtns/" + tname1, HTTP_DELETE); assertResponse(HTTP_OK); // Test DELETE vtn2 result = getJsonResult(baseURL + "default/vtns/" + tname2, HTTP_DELETE); assertResponse(HTTP_OK); // Test DELETE vtn3 result = getJsonResult(baseURL + "default/vtns/" + tname3, HTTP_DELETE); assertResponse(HTTP_OK); // Test DELETE vtn4 result = getJsonResult(baseURL + "default/vtns/" + tname4, HTTP_DELETE); assertResponse(HTTP_OK); // Test DELETE vtn5 result = getJsonResult(baseURL + "default/vtns/" + tname5, HTTP_DELETE); assertResponse(HTTP_OK); // Test DELETE vtn6 result = getJsonResult(baseURL + "default/vtns/" + tname6, HTTP_DELETE); assertResponse(HTTP_OK); // Test DELETE vtn7 result = getJsonResult(baseURL + "default/vtns/" + tname7, HTTP_DELETE); assertResponse(HTTP_OK); // Test DELETE vtn8 result = getJsonResult(baseURL + "default/vtns/" + tname8, HTTP_DELETE); assertResponse(HTTP_OK); // Test DELETE vtn expecting 404 result = getJsonResult(baseURL + "default/vtns/" + tname1, HTTP_DELETE); assertResponse(HTTP_NOT_FOUND); // Test GET vtn in default container, expecting no results result = getJsonResult(baseURL + "default/vtns"); assertResponse(HTTP_OK); jt = new JSONTokener(result); json = new JSONObject(jt); vtnArray = json.getJSONArray("vtn"); Assert.assertEquals(0, vtnArray.length()); testVtnGlobal(baseURL); }
From source file:org.openecomp.sdnc.sli.aai.AAIService.java
private boolean deleteAAIEntity(URL url, String caller) throws AAIServiceException { if (url == null) { throw new NullPointerException(); }/* ww w . j a va 2 s .co m*/ boolean response = false; InputStream inputStream = null; try { URL http_req_url = url; HttpURLConnection con = getConfiguredConnection(http_req_url, HttpMethod.DELETE); // SSLSocketFactory sockFact = CTX.getSocketFactory(); // con.setSSLSocketFactory( sockFact ); LOGwriteFirstTrace("DELETE", http_req_url.toString()); // Check for errors int responseCode = con.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_NO_CONTENT) { inputStream = con.getInputStream(); } else { inputStream = con.getErrorStream(); } // Process the response LOG.debug("HttpURLConnection result:" + responseCode); if (inputStream == null) inputStream = new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8)); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line = null; ObjectMapper mapper = getObjectMapper(); if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_NO_CONTENT) { StringBuilder stringBuilder = new StringBuilder(); while ((line = reader.readLine()) != null) { stringBuilder.append(line); } LOGwriteEndingTrace(responseCode, "SUCCESS", stringBuilder.toString()); response = true; } else if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) { LOGwriteEndingTrace(responseCode, "HTTP_NOT_FOUND", "Entry does not exist."); response = false; } else { ErrorResponse errorresponse = mapper.readValue(reader, ErrorResponse.class); LOGwriteEndingTrace(responseCode, "FAILURE", mapper.writeValueAsString(errorresponse)); throw new AAIServiceException(responseCode, errorresponse); } } catch (AAIServiceException aaiexc) { throw aaiexc; } catch (Exception exc) { LOG.warn(caller, exc); throw new AAIServiceException(exc); } finally { if (inputStream != null) { try { inputStream.close(); } catch (Exception exc) { } } } return response; }
From source file:org.openecomp.sdnc.sli.aai.AAIService.java
@Override public VnfImage requestVnfImageDataByVendorModelVersion(String vendor, String model, String version) throws AAIServiceException { List<VnfImage> responseList = new ArrayList<VnfImage>(); VnfImage response = null;/*from w ww . j a v a2 s.c o m*/ InputStream inputStream = null; try { String request_url = target_uri + vnf_image_query_path + (version == null ? "" : "&application-version={application_version}"); request_url = request_url.replace("{application_vendor}", encodeQuery(vendor)); request_url = request_url.replace("{application_model}", encodeQuery(model)); if (version != null) { request_url = request_url.replace("{application_version}", encodeQuery(version)); } URL http_req_url = new URL(request_url); HttpURLConnection con = getConfiguredConnection(http_req_url, HttpMethod.GET); LOGwriteFirstTrace(HttpMethod.GET, http_req_url.toString()); LOGwriteDateTrace("application_vendor", vendor); LOGwriteDateTrace("application_model", model); if (version != null) { LOGwriteDateTrace("application_version", version); } // Check for errors int responseCode = con.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { inputStream = con.getInputStream(); } else { inputStream = con.getErrorStream(); } // Process the response LOG.debug("HttpURLConnection result:" + responseCode); if (inputStream == null) inputStream = new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8)); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); ObjectMapper mapper = getObjectMapper(); if (responseCode == HttpURLConnection.HTTP_OK) { // StringBuilder stringBuilder = new StringBuilder("\n"); // String line = null; // while( ( line = reader.readLine() ) != null ) { // stringBuilder.append("\n").append( line ); // } // LOG.info(stringBuilder.toString()); response = mapper.readValue(reader, VnfImage.class); String original_buffer = mapper.writeValueAsString(response); LOGwriteEndingTrace(HttpURLConnection.HTTP_OK, "SUCCESS", original_buffer); if (response .getApplicationVendor() == null /*&& response.getAdditionalProperties() != null && !response.getAdditionalProperties().isEmpty()*/) { LOG.warn("A List of multiple VNF-IMAGE entries has been returned"); VnfImages listOfObjects = mapper.readValue(original_buffer, VnfImages.class); if (!listOfObjects.getVnfImage().isEmpty()) { response = listOfObjects.getVnfImage().get(0); } } } else if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) { LOGwriteEndingTrace(responseCode, "HTTP_NOT_FOUND", "Entry does not exist."); return response; } else { ErrorResponse errorresponse = mapper.readValue(reader, ErrorResponse.class); LOGwriteEndingTrace(responseCode, "FAILURE", mapper.writeValueAsString(errorresponse)); throw new AAIServiceException(responseCode, errorresponse); } } catch (AAIServiceException aaiexc) { throw aaiexc; } catch (Exception exc) { LOG.warn("requestVnfImageData", exc); throw new AAIServiceException(exc); } finally { if (inputStream != null) { try { inputStream.close(); } catch (Exception exc) { } } } return response; }
From source file:org.opendaylight.vtn.manager.it.northbound.VtnNorthboundIT.java
/** * test case for VBridge APIs.// w w w . j av a2s.c om * * This method is called by {@link #testVTNAPI()}. * * This calls {@link #testVLANMappingAPI(String, String)}, * {@link #testVBridgeInterfaceAPI(String, String, String)}, * {@link #testVBridgeInterfaceDeleteAPI(String, String)}, * {@link #testVLANMappingDeleteAPI(String, String)}, * {@link #testMacMappingAPI(String, String, String)}, * {@link #testMacAddressAPI(String, String)}. * * @param tname1 A tenant name. * Specified tenant is necessary to be configured * before method is called. * @param tname2 A tenant name. * This tenant is also necessary to be configured * before method is called. * @throws Exception An error occurred. */ private void testVBridgeAPI(String tname1, String tname2) throws Exception { LOG.info("Starting vBridge JAX-RS client."); // A list of host entries for MAC mapping are unordered. jsonComparator.addUnordered("allow", "machost").addUnordered("deny", "machost") .addUnordered("mapped", "machost").addUnordered("machost"); String url = VTN_BASE_URL; StringBuilder baseURL = new StringBuilder(); baseURL.append(url); baseURL.append("default/vtns/"); String tnameDummy = "tenant_dummy"; String desc1 = "testDescription1"; String desc2 = "d"; String desc3 = String.format("%01000d", 1); String bname1 = "n"; String bname2 = "vbridge_Name2"; String bname3 = "33333"; String bname4 = "vbridge_name4vbridge_name4vbrid"; String ebname = "vbridge_for_error"; String bname32 = "vbridge_namevbridge_namevbridge_"; String ageinter0 = "0"; String ageinter1 = "10"; String ageinter2 = "100"; String ageinter3 = "-100"; String ageinter4 = "1000000"; String ageinterOver = "1000001"; String fault = ""; // Test GET vBridges in default container expecting 404, setting dummy tenant String result = getJsonResult(baseURL + tnameDummy + "/vbridges"); assertResponse(HTTP_NOT_FOUND); // Test GET vBridges in default container, expecting no results result = getJsonResult(baseURL + tname1 + "/vbridges"); assertResponse(HTTP_OK); JSONTokener jt = new JSONTokener(result); JSONObject json = new JSONObject(jt); JSONArray vBridgeArray = json.getJSONArray("vbridge"); Assert.assertEquals(0, vBridgeArray.length()); // Test POST vBridge1 expecting 404, setting dummy tenant String requestBody = "{}"; result = getJsonResult(baseURL + tnameDummy + "/vbridges/" + bname1, HTTP_POST, requestBody); assertResponse(HTTP_NOT_FOUND); // Test POST vBridge1 expecting 400, specifying too small ageInterval. requestBody = "{\"description\":\"" + desc1 + "\", \"ageInterval\":\"" + ageinter0 + "\"}"; result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname1, HTTP_POST, requestBody); assertResponse(HTTP_BAD_REQUEST); // Test POST vBridge1 requestBody = "{}"; String requestUri = baseURL + tname1 + "/vbridges/" + bname1; result = getJsonResult(requestUri, HTTP_POST, requestBody); assertResponse(HTTP_CREATED); Assert.assertEquals(requestUri, httpLocation); // Test GET vBridges in default container, expecting one result result = getJsonResult(baseURL + tname1 + "/vbridges"); assertResponse(HTTP_OK); jt = new JSONTokener(result); json = new JSONObject(jt); vBridgeArray = json.getJSONArray("vbridge"); Assert.assertEquals(1, vBridgeArray.length()); // Test POST vBridge1 expecting 409 requestBody = "{}"; result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname1, HTTP_POST, requestBody); assertResponse(HTTP_CONFLICT); // Test POST vBridge2 requestBody = "{\"description\":\"" + desc2 + "\", \"ageInterval\":\"" + ageinter1 + "\"}"; requestUri = baseURL + tname1 + "/vbridges/" + bname2; result = getJsonResult(requestUri, HTTP_POST, requestBody); assertResponse(HTTP_CREATED); Assert.assertEquals(requestUri, httpLocation); // Test POST vBridge2 for other tenant requestBody = "{\"ageInterval\":\"" + ageinter2 + "\"}"; requestUri = baseURL + tname2 + "/vbridges/" + bname2; result = getJsonResult(requestUri, HTTP_POST, requestBody); assertResponse(HTTP_CREATED); Assert.assertEquals(requestUri, httpLocation); // Test GET all vBridges in default container result = getJsonResult(baseURL + tname1 + "/vbridges"); jt = new JSONTokener(result); json = new JSONObject(jt); vBridgeArray = json.getJSONArray("vbridge"); JSONObject vBridge; assertResponse(HTTP_OK); Assert.assertEquals(2, vBridgeArray.length()); for (int i = 0; i < vBridgeArray.length(); i++) { vBridge = vBridgeArray.getJSONObject(i); if (vBridge.getString("name").equals(bname1)) { Assert.assertFalse(vBridge.has("description")); try { Assert.assertEquals("600", json.getString("ageInterval")); } catch (JSONException expected) { assertThat(expected.getMessage(), is("JSONObject[\"ageInterval\"] not found.")); } } else if (vBridge.getString("name").equals(bname2)) { Assert.assertEquals(desc2, vBridge.getString("description")); Assert.assertEquals(ageinter1, vBridge.getString("ageInterval")); fault = vBridge.getString("faults"); } else { // Unexpected vBridge name Assert.assertTrue(false); } } testVLANMappingAPI(tname1, bname1); testMacMappingAPI(tname1, bname1, bname2); testVBridgeInterfaceAPI(tname1, bname1, bname2); // Test POST vBridge3 requestBody = "{\"description\":\"" + desc3 + "\", \"ageInterval\":\"" + ageinter3 + "\"}"; requestUri = baseURL + tname1 + "/vbridges/" + bname3; result = getJsonResult(requestUri, HTTP_POST, requestBody); assertResponse(HTTP_CREATED); Assert.assertEquals(requestUri, httpLocation); // Test GET vBridge3 result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname3); assertResponse(HTTP_OK); jt = new JSONTokener(result); json = new JSONObject(jt); Assert.assertEquals(desc3, json.getString("description")); Assert.assertEquals("600", json.getString("ageInterval")); // Test POST vBridge4 requestBody = "{\"description\":\"" + desc1 + "\", \"ageInterval\":\"" + ageinter4 + "\"}"; requestUri = baseURL + tname1 + "/vbridges/" + bname4; // Ensure that query parameters are eliminated from Location. result = getJsonResult(requestUri + "?param1=1¶m2=2", HTTP_POST, requestBody); assertResponse(HTTP_CREATED); Assert.assertEquals(requestUri, httpLocation); // Test POST vBridge expecting 400 requestBody = "{\"description\":\"" + desc1 + "\", \"ageInterval\":\"" + "ageInterval" + "\"}"; requestUri = baseURL + tname1 + "/vbridges/" + ebname; result = getJsonResult(requestUri, HTTP_POST, requestBody); assertResponse(HTTP_BAD_REQUEST); // Test POST vBridge expecting 400, specifying too long vBridge name. requestBody = "{}"; requestUri = baseURL + tname1 + "/vbridges/" + bname32; result = getJsonResult(requestUri, HTTP_POST, requestBody); assertResponse(HTTP_BAD_REQUEST); // Test POST vBridge expecting 405, setting "" to vBridge name result = getJsonResult(baseURL + tname1 + "/vbridges/", HTTP_POST, requestBody); assertResponse(HTTP_BAD_METHOD); // Test POST vBridge expecting 400, specifying invalid vBridge name // which starts with "_". result = getJsonResult(baseURL + tname1 + "/vbridges/" + "_vbridgename", HTTP_POST, requestBody); assertResponse(HTTP_BAD_REQUEST); // Test POST vBridge expecting 400, specifying invalid vBridge name // including symbol "@". result = getJsonResult(baseURL + tname1 + "/vbridges/" + "vbridge@name", HTTP_POST, requestBody); assertResponse(HTTP_BAD_REQUEST); // Test POST vBridge expecting 400, specifying too large ageInterval. requestBody = "{\"ageInterval\":\"" + ageinterOver + "\"}"; result = getJsonResult(baseURL + tname1 + "/vbridges/" + ebname, HTTP_POST, requestBody); assertResponse(HTTP_BAD_REQUEST); // Test GET vBridges in default container, expecting 4 results result = getJsonResult(baseURL + tname1 + "/vbridges"); assertResponse(HTTP_OK); jt = new JSONTokener(result); json = new JSONObject(jt); vBridgeArray = json.getJSONArray("vbridge"); Assert.assertEquals(4, vBridgeArray.length()); // Test GET vBridges in default container, expecting 1 result result = getJsonResult(baseURL + tname2 + "/vbridges"); assertResponse(HTTP_OK); jt = new JSONTokener(result); json = new JSONObject(jt); vBridgeArray = json.getJSONArray("vbridge"); Assert.assertEquals(1, vBridgeArray.length()); // Test GET vBridge expecting 404 setting dummy vtn result = getJsonResult(baseURL + tnameDummy + "/vbridges/" + bname1); assertResponse(HTTP_NOT_FOUND); // Test GET vBridge expecting 404 setting dummy vBridge result = getJsonResult(baseURL + tname1 + "/vbridges/" + ebname); assertResponse(HTTP_NOT_FOUND); // Test GET vBridge result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname2); jt = new JSONTokener(result); json = new JSONObject(jt); assertResponse(HTTP_OK); Assert.assertEquals(bname2, json.getString("name")); Assert.assertEquals(desc2, json.getString("description")); Assert.assertEquals(ageinter1, json.getString("ageInterval")); Assert.assertEquals(fault, json.getString("faults")); Assert.assertEquals("1", json.getString("state")); // Test GET vBridge, get from other tenant result = getJsonResult(baseURL + tname2 + "/vbridges/" + bname2); assertResponse(HTTP_OK); // Test PUT vBridge1, setting only description (queryparameter is true) String queryParameter = new QueryParameter("all", "true").getString(); requestBody = "{\"description\":\"" + desc1 + "\"}"; result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname1 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_OK); // Test GET vBridge1 result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname1); assertResponse(HTTP_OK); jt = new JSONTokener(result); json = new JSONObject(jt); Assert.assertEquals(desc1, json.getString("description")); Assert.assertEquals("600", json.getString("ageInterval")); // Test PUT vBridge1, setting only ageInter (queryparameter is true) requestBody = "{\"ageInterval\":\"" + ageinter1 + "\"}"; result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname1 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_OK); // Test GET vBridge1 result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname1); assertResponse(HTTP_OK); jt = new JSONTokener(result); json = new JSONObject(jt); Assert.assertFalse(json.has("description")); Assert.assertEquals(ageinter1, json.getString("ageInterval")); // Test PUT vBridge1, setting description and ageInter (queryparameter is true) requestBody = "{\"description\":\"" + desc2 + "\", \"ageInterval\":\"" + ageinter4 + "\"}"; result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname1 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_OK); // Test GET vBridge1 result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname1); assertResponse(HTTP_OK); jt = new JSONTokener(result); json = new JSONObject(jt); Assert.assertEquals(desc2, json.getString("description")); Assert.assertEquals(ageinter4, json.getString("ageInterval")); // Test PUT vBridge1, setting {} (queryparameter is true) requestBody = "{}"; result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname1 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_OK); // Test GET vBridge1 result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname1); assertResponse(HTTP_OK); jt = new JSONTokener(result); json = new JSONObject(jt); Assert.assertFalse(json.has("description")); Assert.assertEquals("600", json.getString("ageInterval")); // Test PUT vBridge2 expecting not change (query parameter is false and requestBody is {}) queryParameter = new QueryParameter("all", "false").getString(); result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname2 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_OK); // Test GET vBridge2 result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname2); assertResponse(HTTP_OK); jt = new JSONTokener(result); json = new JSONObject(jt); Assert.assertEquals(desc2, json.getString("description")); Assert.assertEquals(ageinter1, json.getString("ageInterval")); // Test PUT vBridge2, setting description (query parameter is false) requestBody = "{\"description\":\"" + desc1 + "\"}"; result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname2 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_OK); // Test GET vBridge2 result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname2); assertResponse(HTTP_OK); jt = new JSONTokener(result); json = new JSONObject(jt); Assert.assertEquals(desc1, json.getString("description")); Assert.assertEquals(ageinter1, json.getString("ageInterval")); // Test PUT vBridge2, setting ageInter (query parameter is false) requestBody = "{\"ageInterval\":\"" + ageinter2 + "\"}"; result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname2 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_OK); // Test GET vBridge2 result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname2); assertResponse(HTTP_OK); jt = new JSONTokener(result); json = new JSONObject(jt); Assert.assertEquals(desc1, json.getString("description")); Assert.assertEquals(ageinter2, json.getString("ageInterval")); // Test PUT vBridge2, setting description and ageInter (query parameter is false) requestBody = "{\"description\":\"" + desc3 + "\", \"ageInterval\":\"" + ageinter3 + "\"}"; result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname2 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_OK); // Test GET vBridge2 result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname2); assertResponse(HTTP_OK); jt = new JSONTokener(result); json = new JSONObject(jt); Assert.assertEquals(desc3, json.getString("description")); Assert.assertEquals("100", json.getString("ageInterval")); // Test PUT vBridge2, setting description and ageInter (query parameter is true) queryParameter = new QueryParameter("all", "true").getString(); requestBody = "{\"description\":\"" + desc3 + "\", \"ageInterval\":\"" + ageinter3 + "\"}"; result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname2 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_OK); // Test GET vBridge2 result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname2); assertResponse(HTTP_OK); jt = new JSONTokener(result); json = new JSONObject(jt); Assert.assertEquals(desc3, json.getString("description")); Assert.assertEquals("600", json.getString("ageInterval")); // Test PUT vBridge1 expecting 400 requestBody = "{\"ageInterval\":\"" + "ageinter" + "\"}"; result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname1 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_BAD_REQUEST); // Test PUT vBridge1 expecting 404, setting dummy vtn requestBody = "{}"; result = getJsonResult(baseURL + tnameDummy + "/vbridges/" + bname1 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_NOT_FOUND); // Test PUT vBridge1 expecting 404, setting dummy vbridge result = getJsonResult(baseURL + tname2 + "/vbridges/" + bname1 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_NOT_FOUND); // Test PUT vBridge1 expecting 400, specifying too small ageInterval. queryParameter = new QueryParameter("all", "false").getString(); requestBody = "{\"description\":\"" + desc2 + "\", \"ageInterval\":\"" + ageinter0 + "\"}"; result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname1 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_BAD_REQUEST); // Test PUT vBridge1 expecting 400, specifying too small ageInterval // ("all=true" is specified as query paramter). queryParameter = new QueryParameter("all", "true").getString(); requestBody = "{\"description\":\"" + desc2 + "\", \"ageInterval\":\"" + ageinter0 + "\"}"; result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname1 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_BAD_REQUEST); // Test PUT vBridge1 expecting 400, specifying too large ageInterval // ("all=true" is specified as query paramter). requestBody = "{\"description\":\"" + desc1 + "\", \"ageInterval\":\"" + ageinterOver + "\"}"; result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname1 + queryParameter, HTTP_PUT, requestBody); assertResponse(HTTP_BAD_REQUEST); testVLANMappingDeleteAPI(tname1, bname1); testMacAddressAPI(tname1, bname1); testVBridgeInterfaceDeleteAPI(tname1, bname1); // Test DELETE vBridge expecting 404, setting dummy tenant result = getJsonResult(baseURL + tnameDummy + "/vbridges/" + bname1, HTTP_DELETE); assertResponse(HTTP_NOT_FOUND); // Test DELETE vBridge1 result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname1, HTTP_DELETE); assertResponse(HTTP_OK); // specify not supported Content-Type requestBody = "{}"; result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname1, HTTP_POST, requestBody, "text/plain"); assertResponse(HTTP_UNSUPPORTED_TYPE); requestBody = "{\"description\":\"test\"}"; result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname2 + queryParameter, HTTP_PUT, requestBody, "text/plain"); assertResponse(HTTP_UNSUPPORTED_TYPE); // Authentication failure. checkVBridgeError(GlobalConstants.DEFAULT.toString(), tname1, tname2, bname1, bname2, false, HttpURLConnection.HTTP_UNAUTHORIZED); // Invalid container name. String[] invalid = { "bad_container", "version" }; for (String inv : invalid) { checkVBridgeError(inv, tname1, tname2, bname1, bname2, true, HttpURLConnection.HTTP_NOT_FOUND); checkVBridgeError(inv, tname1, tname2, bname1, bname2, false, HttpURLConnection.HTTP_UNAUTHORIZED); } // Test DELETE vBridge2 result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname2, HTTP_DELETE); assertResponse(HTTP_OK); // Test DELETE vBridge3 result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname3, HTTP_DELETE); assertResponse(HTTP_OK); // Test DELETE vBridge4 result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname4, HTTP_DELETE); assertResponse(HTTP_OK); // Test DELETE vBridge2 on other tenant result = getJsonResult(baseURL + tname2 + "/vbridges/" + bname2, HTTP_DELETE); assertResponse(HTTP_OK); // Test DELETE vBridge expecting 404 result = getJsonResult(baseURL + tname1 + "/vbridges/" + bname1, HTTP_DELETE); assertResponse(HTTP_NOT_FOUND); // Test DELETE vBridge expecting 404, setting dummy tenant result = getJsonResult(baseURL + tname1 + "/vbridges/" + ebname, HTTP_DELETE); assertResponse(HTTP_NOT_FOUND); // Test GET vBridges in default container, expecting no results (tname1) result = getJsonResult(baseURL + tname1 + "/vbridges"); assertResponse(HTTP_OK); jt = new JSONTokener(result); json = new JSONObject(jt); vBridgeArray = json.getJSONArray("vbridge"); Assert.assertEquals(0, vBridgeArray.length()); // Test GET vBridges in default container, expecting no results (tname2) result = getJsonResult(baseURL + tname2 + "/vbridges"); assertResponse(HTTP_OK); jt = new JSONTokener(result); json = new JSONObject(jt); vBridgeArray = json.getJSONArray("vbridge"); Assert.assertEquals(0, vBridgeArray.length()); }
From source file:org.openecomp.sdnc.sli.aai.AAIService.java
@Override public SearchResults requestNodeQuery(String node_type, String entityIdentifier, String entityName) throws AAIServiceException { SearchResults response = null;//from w w w. j a v a2s .c o m InputStream inputStream = null; try { String request_url = target_uri + query_nodes_path; request_url = request_url.replace("{node-type}", encodeQuery(node_type)); request_url = request_url.replace("{entity-identifier}", entityIdentifier); request_url = request_url.replace("{entity-name}", encodeQuery(entityName)); URL http_req_url = new URL(request_url); HttpURLConnection con = getConfiguredConnection(http_req_url, HttpMethod.GET); LOGwriteFirstTrace(HttpMethod.GET, http_req_url.toString()); LOGwriteDateTrace("node_type", node_type); LOGwriteDateTrace("vnf_name", entityName); // Check for errors int responseCode = con.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { inputStream = con.getInputStream(); } else { inputStream = con.getErrorStream(); } // Process the response LOG.debug("HttpURLConnection result:" + responseCode); if (inputStream == null) inputStream = new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8)); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); ObjectMapper mapper = getObjectMapper(); if (responseCode == HttpURLConnection.HTTP_OK) { response = mapper.readValue(reader, SearchResults.class); LOGwriteEndingTrace(HttpURLConnection.HTTP_OK, "SUCCESS", mapper.writeValueAsString(response)); } else if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) { LOGwriteEndingTrace(responseCode, "HTTP_NOT_FOUND", "Entry does not exist."); return response; } else { ErrorResponse errorresponse = mapper.readValue(reader, ErrorResponse.class); LOGwriteEndingTrace(responseCode, "FAILURE", mapper.writeValueAsString(errorresponse)); throw new AAIServiceException(responseCode, errorresponse); } } catch (AAIServiceException aaiexc) { throw aaiexc; } catch (Exception exc) { LOG.warn("requestNodeQuery", exc); throw new AAIServiceException(exc); } finally { if (inputStream != null) { try { inputStream.close(); } catch (Exception exc) { } } } return response; }
From source file:org.openecomp.sdnc.sli.aai.AAIService.java
@Override public String requestDataByURL(URL url) throws AAIServiceException { if (url == null) { throw new NullPointerException(); }// ww w . j a v a 2 s .co m String response = null; InputStream inputStream = null; try { URL http_req_url = url; HttpURLConnection con = getConfiguredConnection(http_req_url, HttpMethod.GET); LOGwriteFirstTrace(HttpMethod.GET, http_req_url.toString()); // Check for errors int responseCode = con.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { inputStream = con.getInputStream(); } else { inputStream = con.getErrorStream(); } // Process the response LOG.debug("HttpURLConnection result:" + responseCode); if (inputStream == null) inputStream = new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8)); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); ObjectMapper mapper = getObjectMapper(); if (responseCode == HttpURLConnection.HTTP_OK) { StringBuilder stringBuilder = new StringBuilder("\n"); String line = null; while ((line = reader.readLine()) != null) { stringBuilder.append(line); } LOG.info(stringBuilder.toString()); // response = mapper.readValue(reader, String.class); response = stringBuilder.toString(); LOGwriteEndingTrace(HttpURLConnection.HTTP_OK, "SUCCESS", mapper.writeValueAsString(response)); } else if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) { LOGwriteEndingTrace(responseCode, "HTTP_NOT_FOUND", "Entry does not exist."); response = null; } else { ErrorResponse errorresponse = mapper.readValue(reader, ErrorResponse.class); LOGwriteEndingTrace(responseCode, "FAILURE", mapper.writeValueAsString(errorresponse)); throw new AAIServiceException(responseCode, errorresponse); } } catch (AAIServiceException aaiexc) { throw aaiexc; } catch (Exception exc) { LOG.warn("requestNetworkVceData", exc); throw new AAIServiceException(exc); } finally { if (inputStream != null) { try { inputStream.close(); } catch (Exception exc) { } } } return response; }