Example usage for java.net HttpURLConnection HTTP_UNAUTHORIZED

List of usage examples for java.net HttpURLConnection HTTP_UNAUTHORIZED

Introduction

In this page you can find the example usage for java.net HttpURLConnection HTTP_UNAUTHORIZED.

Prototype

int HTTP_UNAUTHORIZED

To view the source code for java.net HttpURLConnection HTTP_UNAUTHORIZED.

Click Source Link

Document

HTTP Status-Code 401: Unauthorized.

Usage

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);/*w w  w .j a  va  2  s .c om*/
    }
    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:org.openrdf.http.client.HTTPClient.java

protected void getRDF(HttpMethod method, RDFHandler handler, boolean requireContext)
        throws IOException, RDFHandlerException, RepositoryException, MalformedQueryException,
        UnauthorizedException, QueryInterruptedException {
    // Specify which formats we support using Accept headers
    Set<RDFFormat> rdfFormats = RDFParserRegistry.getInstance().getKeys();
    if (rdfFormats.isEmpty()) {
        throw new RepositoryException("No tuple RDF parsers have been registered");
    }//from   ww w. j  a  va2  s.co  m

    List<String> acceptParams = RDFFormat.getAcceptParams(rdfFormats, requireContext, preferredRDFFormat);
    for (String acceptParam : acceptParams) {
        method.addRequestHeader(ACCEPT_PARAM_NAME, acceptParam);
    }

    int httpCode = httpClient.executeMethod(method);

    if (httpCode == HttpURLConnection.HTTP_OK) {
        String mimeType = getResponseMIMEType(method);
        try {
            RDFFormat format = RDFFormat.matchMIMEType(mimeType, rdfFormats);
            RDFParser parser = Rio.createParser(format, getValueFactory());
            parser.setParserConfig(getParserConfig());
            parser.setParseErrorListener(new ParseErrorLogger());
            parser.setRDFHandler(handler);
            parser.parse(method.getResponseBodyAsStream(), method.getURI().getURI());
        } catch (UnsupportedRDFormatException e) {
            throw new RepositoryException("Server responded with an unsupported file format: " + mimeType);
        } catch (RDFParseException e) {
            throw new RepositoryException("Malformed query result from server", e);
        }
    } else if (httpCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
        throw new UnauthorizedException();
    } else if (httpCode == HttpURLConnection.HTTP_UNAVAILABLE) {
        throw new QueryInterruptedException();
    } else {
        ErrorInfo errInfo = getErrorInfo(method);

        // Throw appropriate exception
        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 {
            throw new RepositoryException(errInfo.toString());
        }
    }
}

From source file:org.betaconceptframework.astroboa.resourceapi.resource.ContentObjectResource.java

private Response saveContentFromMultipartRelatedRequest(String contentObjectIdOrName,
        MultipartRelatedInput multipartRelatedInput, String httpMethod, boolean updateLastModificationTime) {

    try {//from w  w  w  .j a v  a  2s.c om

        //Obtain the part which contains the object's JSON/XML
        InputPart partWhichContainsObjectSource = getMessagePartWithObjectSource(multipartRelatedInput);

        //Check that mime type of the content is a valid one
        ResourceRepresentationType resourceRepresentationType = checkMediaTypeIsValid(
                partWhichContainsObjectSource);

        ContentObject contentObjectToBeSaved = retrieveObjectSourceFromMessageAndImportWithoutSave(
                multipartRelatedInput, partWhichContainsObjectSource);

        boolean entityIsNew = objectIsNew(contentObjectIdOrName, httpMethod, contentObjectToBeSaved);

        //Save content object
        try {
            contentObjectToBeSaved = astroboaClient.getContentService().save(contentObjectToBeSaved, false,
                    updateLastModificationTime, null);

            return ContentApiUtils.createSuccessfulResponseForPUTOrPOST(contentObjectToBeSaved, httpMethod,
                    resourceRepresentationType, entityIsNew);

        } catch (CmsUnauthorizedAccessException e) {
            throw new WebApplicationException(HttpURLConnection.HTTP_UNAUTHORIZED);
        } catch (Exception e) {
            logger.error("", e);
            throw new WebApplicationException(e, HttpURLConnection.HTTP_INTERNAL_ERROR);
        }

    } catch (WebApplicationException e) {
        throw e;
    } catch (Exception e) {
        throw new WebApplicationException(
                ContentApiUtils.createResponseForException(Status.INTERNAL_SERVER_ERROR, e, true,
                        "A problem occured while saving form data for object with id or system name: "
                                + contentObjectIdOrName));
    }
}

From source file:org.openrdf.http.client.HTTPClient.java

protected boolean getBoolean(HttpMethod method) throws IOException, RepositoryException,
        MalformedQueryException, UnauthorizedException, QueryInterruptedException {
    // Specify which formats we support using Accept headers
    Set<BooleanQueryResultFormat> booleanFormats = BooleanQueryResultParserRegistry.getInstance().getKeys();
    if (booleanFormats.isEmpty()) {
        throw new RepositoryException("No boolean query result parsers have been registered");
    }/*from   w  w w .j a  va  2  s.  c  o m*/

    for (BooleanQueryResultFormat format : booleanFormats) {
        // Determine a q-value that reflects the user specified preference
        int qValue = 10;

        if (preferredBQRFormat != null && !preferredBQRFormat.equals(format)) {
            // Prefer specified format over other formats
            qValue -= 2;
        }

        for (String mimeType : format.getMIMETypes()) {
            String acceptParam = mimeType;

            if (qValue < 10) {
                acceptParam += ";q=0." + qValue;
            }

            method.addRequestHeader(ACCEPT_PARAM_NAME, acceptParam);
        }
    }

    int httpCode = httpClient.executeMethod(method);

    if (httpCode == HttpURLConnection.HTTP_OK) {
        String mimeType = getResponseMIMEType(method);
        try {
            BooleanQueryResultFormat format = BooleanQueryResultFormat.matchMIMEType(mimeType, booleanFormats);
            BooleanQueryResultParser parser = QueryResultIO.createParser(format);
            return parser.parse(method.getResponseBodyAsStream());
        } catch (UnsupportedQueryResultFormatException e) {
            throw new RepositoryException("Server responded with an unsupported file format: " + mimeType);
        } catch (QueryResultParseException e) {
            throw new RepositoryException("Malformed query result from server", e);
        }
    } else if (httpCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
        throw new UnauthorizedException();
    } else if (httpCode == HttpURLConnection.HTTP_UNAVAILABLE) {
        throw new QueryInterruptedException();
    } else {
        ErrorInfo errInfo = getErrorInfo(method);

        // Throw appropriate exception
        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 {
            throw new RepositoryException(method.getStatusText());
        }
    }
}

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 www  .j av  a2s .co  m*/
        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 {//w  w  w . j  ava 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.betaconceptframework.astroboa.resourceapi.resource.ContentObjectResource.java

private Response saveContentObject(ContentObject contentObject, String httpMethod, String requestContent,
        boolean entityIsNew, boolean updateLastModificationTime) {

    try {//from   w  w  w  .  ja  v  a 2s .co m
        contentObject = astroboaClient.getContentService().save(contentObject, false,
                updateLastModificationTime, null);

        return ContentApiUtils.createResponseForPutOrPostOfACmsEntity(contentObject, httpMethod, requestContent,
                entityIsNew);

    } catch (CmsUnauthorizedAccessException e) {
        throw new WebApplicationException(HttpURLConnection.HTTP_UNAUTHORIZED);
    } catch (Exception e) {
        logger.error("", e);
        throw new WebApplicationException(e, HttpURLConnection.HTTP_INTERNAL_ERROR);
    }
}

From source file:org.opendaylight.vtn.manager.it.northbound.VtnNorthboundIT.java

/**
 * Test case for VTN APIs.//from   www . j  a  v  a  2s . c  o 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&param2=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.opendaylight.vtn.manager.it.northbound.VtnNorthboundIT.java

/**
 * test case for VBridge APIs.//from   w w  w .  j  av a2  s .com
 *
 * 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&param2=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.opendaylight.vtn.manager.it.northbound.VtnNorthboundIT.java

/**
 * Test case for VBridgeInterface APIs./*  w  w w .j  av  a  2s . co m*/
 *
 * This method called by {@code testVBridge}.
 * This calls {@code testPortMappingAPI}.
 *
 * @param tname1    A tenant name.
 *                  Specified tenant is necessary to be configured
 *                  before method is called.
 * @param bname1    A vBridge name.
 *                  Specified vBridge is necessary to be configured
 *                  before method is called.
 * @param bname2    A vBridge name.
 *                  This vBridge is also necessary to be configured
 *                  before method is called.
 * @throws JSONException  An error occurred.
 */
private void testVBridgeInterfaceAPI(String tname1, String bname1, String bname2) throws JSONException {
    LOG.info("Starting vBridge Intergace JAX-RS client.");
    String url = VTN_BASE_URL;
    StringBuilder baseURL = new StringBuilder();
    baseURL.append(url);
    baseURL.append("default/vtns/");
    baseURL.append(tname1);
    baseURL.append("/vbridges/");

    StringBuilder baseURL2 = new StringBuilder();
    baseURL2.append(url);
    baseURL2.append("default/vtns/");
    baseURL2.append("testVtn_2");
    baseURL2.append("/vbridges/");

    String ifname = "testInterface0";
    String ifname1 = "testInterface";
    String ifname2 = "test_Interface2";
    String ifname3 = "testInterface3";
    String ifname4 = "4";
    String ifname5 = "testInterface5testinterface5tes";
    String ifname32 = "testInterface5testinterface5test";

    String bnameDummy = "bname_dummy";
    String tnameDummy = "tenant_dummy";

    String desc1 = "testDescription1";
    String desc2 = "t";
    String desc3 = String.format("%01000d", 1);

    // Test GET vBridge Interfaces in default container, expecting no
    // results
    String result = getJsonResult(baseURL + bname1 + "/interfaces");
    assertResponse(HTTP_OK);
    JSONTokener jt = new JSONTokener(result);
    JSONObject json = new JSONObject(jt);
    JSONArray vbridgeifArray = json.getJSONArray("interface");
    Assert.assertEquals(0, vbridgeifArray.length());

    // Test POST vBridge Interface1 expecting 404
    String requestBody = "{}";
    result = getJsonResult(baseURL + bnameDummy + "/interfaces/" + ifname1, HTTP_POST, requestBody);
    assertResponse(HTTP_NOT_FOUND);

    // Test POST vbridge interface expecting 201
    // setting vbridge Interface1
    requestBody = "{}";
    String requestUri = baseURL + bname1 + "/interfaces/" + ifname1;
    result = getJsonResult(requestUri, HTTP_POST, requestBody);
    assertResponse(HTTP_CREATED);
    Assert.assertEquals(requestUri, httpLocation);

    // Test POST vbridge interface expecting 409
    // setting vbridge Interface1
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname1, HTTP_POST, requestBody);
    assertResponse(HTTP_CONFLICT);

    // Test GET vbridge interface expecitng 404
    // setting dummy vtn
    result = getJsonResult(url + "default/vtns/" + tnameDummy + "/vbridges/" + bname1 + "/interfaces");
    assertResponse(HTTP_NOT_FOUND);

    // setting dummy vbridge
    result = getJsonResult(baseURL + bnameDummy + "/interfaces");
    assertResponse(HTTP_NOT_FOUND);

    // Test GET vBridge Interfaces in default container, expecting one
    // result
    result = getJsonResult(baseURL + bname1 + "/interfaces");
    assertResponse(HTTP_OK);
    jt = new JSONTokener(result);
    json = new JSONObject(jt);
    vbridgeifArray = json.getJSONArray("interface");
    Assert.assertEquals(1, vbridgeifArray.length());

    // Test POST vbridge interface expecting 200
    // setting vbridge Interface2
    requestBody = "{\"description\":\"" + desc1 + "\", \"enabled\":true}";
    requestUri = baseURL + bname1 + "/interfaces/" + ifname2;
    result = getJsonResult(requestUri, HTTP_POST, requestBody);
    assertResponse(HTTP_CREATED);
    Assert.assertEquals(requestUri, httpLocation);

    // setting vbridge Interface3
    requestBody = "{\"description\":\"" + desc2 + "\", \"enabled\":true}";
    requestUri = baseURL + bname2 + "/interfaces/" + ifname3;
    result = getJsonResult(requestUri, HTTP_POST, requestBody);
    assertResponse(HTTP_CREATED);
    Assert.assertEquals(requestUri, httpLocation);

    testPortMappingAPI(tname1, bname1, bname2, ifname2, ifname3);

    // Test POST vBridge Interface2, for other tenant
    requestBody = "{\"description\":\"" + desc1 + "\", \"enabled\":true}";
    requestUri = baseURL2 + bname2 + "/interfaces/" + ifname2;
    result = getJsonResult(requestUri, HTTP_POST, requestBody);
    assertResponse(HTTP_CREATED);
    Assert.assertEquals(requestUri, httpLocation);

    // Test GET all vBridge Interfaces in default container
    result = getJsonResult(baseURL + bname1 + "/interfaces");
    jt = new JSONTokener(result);
    json = new JSONObject(jt);
    vbridgeifArray = json.getJSONArray("interface");
    JSONObject vbridgeif;

    assertResponse(HTTP_OK);
    Assert.assertEquals(2, vbridgeifArray.length());

    for (int i = 0; i < vbridgeifArray.length(); i++) {
        vbridgeif = vbridgeifArray.getJSONObject(i);
        if (vbridgeif.getString("name").equals(ifname1)) {
            Assert.assertFalse(vbridgeif.has("description"));

            try {
                Assert.assertEquals("true", vbridgeif.getString("enabled"));
            } catch (JSONException expected) {
                assertThat(expected.getMessage(), is("JSONObject[\"enabled\"] not found."));
            }
            Assert.assertEquals("-1", vbridgeif.getString("state"));
            Assert.assertEquals("-1", vbridgeif.getString("entityState"));
        } else if (vbridgeif.getString("name").equals(ifname2)) {
            Assert.assertEquals(desc1, vbridgeif.getString("description"));
            Assert.assertEquals("true", vbridgeif.getString("enabled"));
            Assert.assertEquals("0", vbridgeif.getString("state"));
            Assert.assertEquals("-1", vbridgeif.getString("entityState"));
        } else if (vbridgeif.getString("name").equals(ifname3)) {
            Assert.assertEquals(desc2, vbridgeif.getString("description"));
            Assert.assertEquals("true", vbridgeif.getString("enabled"));
            Assert.assertEquals("-1", vbridgeif.getString("state"));
            Assert.assertEquals("-1", vbridgeif.getString("entityState"));
        } else {
            // Unexpected vBridge Interface name
            Assert.assertTrue(false);
        }
    }

    // Test POST vBridge Interface4
    requestBody = "{\"enabled\":false}";
    requestUri = baseURL + bname1 + "/interfaces/" + ifname4;
    result = getJsonResult(requestUri, HTTP_POST, requestBody);
    assertResponse(HTTP_CREATED);
    Assert.assertEquals(requestUri, httpLocation);

    // Test GET vBridge Interface4
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname4);
    assertResponse(HTTP_OK);

    jt = new JSONTokener(result);
    json = new JSONObject(jt);

    Assert.assertFalse(json.has("description"));
    Assert.assertEquals("false", json.getString("enabled"));

    // Test POST vBridge Interface5
    requestBody = "{\"description\":\"" + desc3 + "\"}";
    requestUri = baseURL + bname1 + "/interfaces/" + ifname5;

    // Ensure that query parameters are eliminated from Location.
    result = getJsonResult(requestUri + "?param1=1&param2=2", HTTP_POST, requestBody);
    assertResponse(HTTP_CREATED);
    Assert.assertEquals(requestUri, httpLocation);

    // Test POST vBridge Interface expecting 400, setting invalid value
    // for ageInterval
    requestBody = "{\"description\":\"" + desc2 + "\", \"enabled\":enabled}";
    requestUri = baseURL + bname1 + "/interfaces/" + ifname;
    result = getJsonResult(requestUri, HTTP_POST, requestBody);
    assertResponse(HTTP_BAD_REQUEST);

    // Test POST vBridge Interface expecting 404, setting dummy tenant
    requestBody = "{}";
    result = getJsonResult(url + "default/vtns/" + tnameDummy + "/vbridges/" + bname1 + "/interfaces/" + ifname,
            HTTP_POST, requestBody);
    assertResponse(HTTP_NOT_FOUND);

    // Test POST vBridge Interface expecting 404, setting vbridge that
    // don't exist
    result = getJsonResult(baseURL + bnameDummy + "/interfaces/" + ifname, HTTP_POST, requestBody);
    assertResponse(HTTP_NOT_FOUND);

    // Test POST vBridge Interface expecting 405, setting "" to vbridgeIF
    // name
    result = getJsonResult(baseURL + bnameDummy + "/interfaces/" + "", HTTP_POST, requestBody);
    assertResponse(HTTP_BAD_METHOD);

    // Test POST vBridge Interface expecting 400, specifying too long
    // interface name.
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname32, HTTP_POST, requestBody);
    assertResponse(HTTP_BAD_REQUEST);

    // Test POST vBridge Interface expecting 400, specifying invalid
    // interface name which starts with "_".
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + "_ifname", HTTP_POST, requestBody);
    assertResponse(HTTP_BAD_REQUEST);

    // Test POST vBridge Interface expecting 400, specifying invalid
    // interface name which includes "@".
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + "if@name", HTTP_POST, requestBody);
    assertResponse(HTTP_BAD_REQUEST);

    // Test GET vBridge Interface expecting 404, setting vtn that don't exist
    result = getJsonResult(
            url + "default/vtns/" + tnameDummy + "/vbridges/" + bname1 + "/interfaces/" + ifname1);
    assertResponse(HTTP_NOT_FOUND);

    // Test GET vBridge Interface expecting 404, setting vbridge that don't
    // exits
    result = getJsonResult(baseURL + bnameDummy + "/interfaces/" + ifname1);
    assertResponse(HTTP_NOT_FOUND);

    // Test GET vBridge Interface expecting 404, setting vbridgeIF that
    // don't exits
    result = getJsonResult(baseURL + bnameDummy + "/interfaces/" + ifname);
    assertResponse(HTTP_NOT_FOUND);

    // Test GET vBridge Interface2
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname2);
    jt = new JSONTokener(result);
    json = new JSONObject(jt);

    assertResponse(HTTP_OK);
    Assert.assertEquals(ifname2, json.getString("name"));
    Assert.assertEquals(desc1, json.getString("description"));
    Assert.assertEquals("true", json.getString("enabled"));
    Assert.assertEquals("0", json.getString("state"));
    Assert.assertEquals("-1", json.getString("entityState"));

    // Test PUT vBridge interface1
    requestBody = "{\"description\":\"" + desc1 + "\", \"enabled\":\"true\"}";
    String queryParameter = new QueryParameter("all", "true").getString();
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname1 + queryParameter, HTTP_PUT, requestBody);
    assertResponse(HTTP_OK);

    // Test GET vBridge Interface1
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname1);
    jt = new JSONTokener(result);
    json = new JSONObject(jt);

    assertResponse(HTTP_OK);
    Assert.assertEquals(desc1, json.getString("description"));
    Assert.assertEquals("true", json.getString("enabled"));

    // Test PUT vBridge interface1, setting description
    // (queryparameter is true)
    requestBody = "{\"description\":\"" + desc2 + "\"}";
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname1 + queryParameter, HTTP_PUT, requestBody);
    assertResponse(HTTP_OK);

    // Test GET vBridge Interface1
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname1);
    jt = new JSONTokener(result);
    json = new JSONObject(jt);

    assertResponse(HTTP_OK);
    Assert.assertEquals(desc2, json.getString("description"));
    Assert.assertEquals("true", json.getString("enabled"));

    // Test PUT vBridge interface1, setting enabled (queryparameter is true)
    requestBody = "{\"enabled\":\"false\"}";
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname1 + queryParameter, HTTP_PUT, requestBody);
    assertResponse(HTTP_OK);

    // Test GET vBridge Interface1
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname1);
    jt = new JSONTokener(result);
    json = new JSONObject(jt);

    assertResponse(HTTP_OK);
    Assert.assertFalse(json.has("description"));
    Assert.assertEquals("false", json.getString("enabled"));

    // Test PUT vBridge interface1, setting description and enabled
    requestBody = "{\"description\":\"" + desc3 + "\", \"enabled\":\"false\"}";
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname1 + queryParameter, HTTP_PUT, requestBody);
    assertResponse(HTTP_OK);

    // Test GET vBridge Interface1
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname1);
    jt = new JSONTokener(result);
    json = new JSONObject(jt);

    assertResponse(HTTP_OK);
    Assert.assertEquals(desc3, json.getString("description"));
    Assert.assertEquals("false", json.getString("enabled"));

    // Test PUT vBridge interface1, setting {}
    requestBody = "{}";
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname1 + queryParameter, HTTP_PUT, requestBody);
    assertResponse(HTTP_OK);

    // Test PUT vBridge Interface2 expecting not change
    queryParameter = new QueryParameter("all", "false").getString();
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname2 + queryParameter, HTTP_PUT, requestBody);
    assertResponse(HTTP_OK);

    // Test GET vBridge Interface2
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname2);
    jt = new JSONTokener(result);
    json = new JSONObject(jt);

    assertResponse(HTTP_OK);
    Assert.assertEquals(desc1, json.getString("description"));
    Assert.assertEquals("true", json.getString("enabled"));

    // Test PUT vBridge Interface2, setting enabled
    requestBody = "{\"enabled\":false}";
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname2 + queryParameter, HTTP_PUT, requestBody);
    assertResponse(HTTP_OK);

    // Test GET vBridge Interface2
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname2);
    jt = new JSONTokener(result);
    json = new JSONObject(jt);

    assertResponse(HTTP_OK);
    Assert.assertEquals(desc1, json.getString("description"));
    Assert.assertEquals("false", json.getString("enabled"));

    // Test PUT vBridge Interface2, setting enabled
    requestBody = "{\"description\":\"" + desc3 + "\"}";
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname2 + queryParameter, HTTP_PUT, requestBody);
    assertResponse(HTTP_OK);

    // Test GET vBridge Interface2
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname2);
    jt = new JSONTokener(result);
    json = new JSONObject(jt);

    assertResponse(HTTP_OK);
    Assert.assertEquals(desc3, json.getString("description"));
    Assert.assertEquals("false", json.getString("enabled"));

    // Test PUT vBridge Interface2, setting enabled
    requestBody = "{\"description\":\"" + desc1 + "\", \"enabled\":\"true\"}";
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname2 + queryParameter, HTTP_PUT, requestBody);
    assertResponse(HTTP_OK);

    // Test GET vBridge Interface2
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname2);
    jt = new JSONTokener(result);
    json = new JSONObject(jt);

    assertResponse(HTTP_OK);
    Assert.assertEquals(desc1, json.getString("description"));
    Assert.assertEquals("true", json.getString("enabled"));

    // Test PUT vBridge Interface expecting 404
    // setting dummy vtn
    result = getJsonResult(url + "default/vtns/" + tnameDummy + "/vbridges/" + bname1 + "/interfaces/" + ifname1
            + queryParameter, HTTP_PUT, requestBody);
    assertResponse(HTTP_NOT_FOUND);

    // setting dummy vbridge
    result = getJsonResult(baseURL + bnameDummy + "/interfaces/" + ifname1 + queryParameter, HTTP_PUT,
            requestBody);
    assertResponse(HTTP_NOT_FOUND);

    // setting  dummy vbridgeIF
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname + queryParameter, HTTP_PUT, requestBody);
    assertResponse(HTTP_NOT_FOUND);

    // Test GET all vBridge Interfaces in default container
    result = getJsonResult(baseURL + bname1 + "/interfaces");
    jt = new JSONTokener(result);
    json = new JSONObject(jt);
    vbridgeifArray = json.getJSONArray("interface");

    assertResponse(HTTP_OK);
    Assert.assertEquals(4, vbridgeifArray.length());

    // Test DELETE expecting 200
    // delete vBridge Interface2 on other tenant
    result = getJsonResult(baseURL2 + bname2 + "/interfaces/" + ifname2, HTTP_DELETE);
    assertResponse(HTTP_OK);

    // specify not supported Content-Type
    requestBody = "{}";
    result = getJsonResult(baseURL + bname2 + "/interfaces/" + ifname2, HTTP_POST, requestBody, "text/plain");
    assertResponse(HTTP_UNSUPPORTED_TYPE);

    requestBody = "{}";
    result = getJsonResult(baseURL + bname2 + "/interfaces/" + ifname3 + queryParameter, HTTP_PUT, requestBody,
            "text/plain");
    assertResponse(HTTP_UNSUPPORTED_TYPE);

    // Authentication failure.
    checkVBridgeInterfaceError(GlobalConstants.DEFAULT.toString(), tname1, bname1, bname2, ifname3, ifname2,
            false, HttpURLConnection.HTTP_UNAUTHORIZED);

    // Invalid container name.
    String[] invalid = { "bad_container", "version" };
    for (String inv : invalid) {
        checkVBridgeInterfaceError(inv, tname1, bname1, bname2, ifname3, ifname2, true,
                HttpURLConnection.HTTP_NOT_FOUND);
        checkVBridgeInterfaceError(inv, tname1, bname1, bname2, ifname3, ifname2, false,
                HttpURLConnection.HTTP_UNAUTHORIZED);
    }

    // delete  vBridge Interface3
    result = getJsonResult(baseURL + bname2 + "/interfaces/" + ifname3, HTTP_DELETE);
    assertResponse(HTTP_OK);

    // delete vBridge Interface4
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname4, HTTP_DELETE);
    assertResponse(HTTP_OK);

    // delete vBridge Interface5
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname5, HTTP_DELETE);
    assertResponse(HTTP_OK);
}