com.ibm.tivoli.maximo.oslc.provider.unittest

Class APIUnitTestUtils

  • java.lang.Object
    • com.ibm.tivoli.maximo.oslc.provider.unittest.APIUnitTestUtils


  • public class APIUnitTestUtils
    extends java.lang.Object
    • Constructor Summary

      Constructors 
      Constructor and Description
      APIUnitTestUtils() 
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method and Description
      static void assertHeaders(java.net.HttpURLConnection con, java.lang.String targetKey, java.lang.String targetValue)
      Assert header value Example: APIUnitTestUtils.assertHeaders(con, key, "value");
      static void assertJSON(com.ibm.json.java.JSONArtifact jsonData, java.lang.String jsonPath, java.lang.String value)
      Assert JSON value by path using ExpressionEvaluator Example: APIUnitTestUtils.assertJSON(response, "methodname", "UNITTEST_EXPMETHOD");
      static void assertJSONFalse(com.ibm.json.java.JSONArtifact jsonData, java.lang.String jsonPath)
      Assert JSON value false by path using ExpressionEvaluator Example: APIUnitTestUtils.assertJSONFalse(response, "weight<1");
      static void assertJSONTrue(com.ibm.json.java.JSONArtifact jsonData, java.lang.String jsonPath)
      Assert JSON value true by path using ExpressionEvaluator Example: APIUnitTestUtils.assertJSONTrue(response, "weight>1");
      static void assertResponseCode(java.net.HttpURLConnection con, int responseCode)
      Assert response code, when the response code is greater or equal to 400, read the input stream for error Examples: APIUnitTestUtils.assertResponseCode(con, 200);
      static void deleteResource(java.lang.String user, java.lang.String pwd, java.lang.String url)
      Delete unittest resource by url Examples: APIUnitTestUtils.deleteResource(userid, password, (String)UnitTestContext.getCurrentContext().getProperty("expressionURI"));
      static com.ibm.json.java.JSONObject getJSONDataSourceFromPath(java.lang.String path)
      Get JSON Data Source from file and case name, allow add new attribute or content in.
      static com.ibm.json.java.JSONObject getJSONDataSourceFromPathAndName(java.lang.String path, java.lang.String name)
      Get JSON Data Source from file and case name, allow add new attribute or content in.
      static com.ibm.json.java.JSONObject getResponse(java.net.HttpURLConnection con)
      Get response as JSONObject from HttpURLConnection Example: JSONObject response = APIUnitTestUtils.getResponse(con);
      static java.net.HttpURLConnection parseAndPrepareHttpConnection(java.lang.String user, java.lang.String pwd, com.ibm.json.java.JSONObject jsonDataSource, boolean fullUrl)
      Prepare HTTPConnection based on jsonDataSource
      static java.net.HttpURLConnection prepareGetHttpConnection(java.lang.String user, java.lang.String pwd, java.lang.String url, com.ibm.json.java.JSONObject headers)
      Prepare HttpURLConnection based on full url and headers - headers can be null Example: String url = (String)UnitTestContext.getCurrentContext().getProperty("expressionURI"); HttpURLConnection con = APIUnitTestUtils.prepareGetHttpConnection(userid, password, url, null);
      static java.net.HttpURLConnection prepareGetHttpConnection(java.lang.String user, java.lang.String pwd, java.lang.String url, com.ibm.json.java.JSONObject headers, boolean fullUrl)
      Prepare HttpURLConnection based on url and headers - headers can be null
      static java.net.HttpURLConnection prepareHttpConnection(java.lang.String user, java.lang.String pwd, java.lang.String path)
      Prepare HttpConnection based on JSONDataResource file Example: Path: /com/ibm/tivoli/maximo/assethealth/unittest/jsondatasource/expressionMethod.json The format of JSONDataResource: { "url":"os/mxapiahmethodology?_tenantcode=00&lean=1", "method": "POST", "headers": { "Content-Type": "application/json" }, "body": { "usewith": "ASSET", "worstscore": 5.0, "bestscore": 1.0, "expgroup": [ { "usewith": "ASSET", "expression": ":assettype='PUMP'", "egtype": "EXPRESSION", "intobjectname": "MXAPIASSET" } ], "autoconversion": true, "methodname": "UNITTEST_EXPMETHOD", "active": false } }
      static java.net.HttpURLConnection prepareHttpConnection(java.lang.String user, java.lang.String pwd, java.lang.String path, java.lang.String name)
      Prepare HttpConnection based on JSONDataResource file Example: Path: /com/ibm/tivoli/maximo/assethealth/unittest/jsondatasource/ahhistoryAsset.json The format of JSONDataResource: { "createAsset":{ "url":"os/mxapiasset?_tenantcode=00&lean=1", "method": "POST", "headers": { "Content-Type": "application/json" }, "body": { "assetnum":"UTAHASSET", "siteid":"BEDFORD" } }, "updateAsset":{ "method": "PATCH", "headers": { "Content-Type": "application/json", "properties": "description" }, "body": { "description":"UT Asset AH History" } } }
      static java.net.HttpURLConnection prepareHttpConnection(java.lang.String user, java.lang.String pwd, java.lang.String url, java.lang.String method, com.ibm.json.java.JSONObject headers, com.ibm.json.java.JSONArtifact body, boolean fullUrl)
      Prepare HttpURLConnection based on url, method, headers and body - support GET/POST/DELETE - working on PATCH and others Example: HttpURLConnection con = APIUnitTestUtils.prepareHttpConnection(user, pwd, url, method, headers, body, false);
      static java.net.HttpURLConnection prepareHttpConnection(java.lang.String user, java.lang.String pwd, java.lang.String path, java.lang.String name, java.lang.String url, boolean fullUrl)
      Prepare HttpConnection based on JSONDataResource file Example: Path: /com/ibm/tivoli/maximo/assethealth/unittest/jsondatasource/ahhistoryAsset.json The format of JSONDataResource: { "updateAsset":{ "method": "PATCH", "headers": { "Content-Type": "application/json", "properties": "description" }, "body": { "description":"UT Asset AH History" } } }
      static com.ibm.json.java.JSONArray readFileAsJSONArray(java.lang.String path)
      Read JSONDataResource file as JSONArray
      static com.ibm.json.java.JSONObject readFileAsJSONObject(java.lang.String path)
      Read JSONDataResource file as JSONObject
      static void setAuth(java.lang.String user, java.lang.String pwd, java.net.HttpURLConnection con)
      Set up authentication to Maximo, only support maxauth for now
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • APIUnitTestUtils

        public APIUnitTestUtils()
    • Method Detail

      • readFileAsJSONObject

        public static com.ibm.json.java.JSONObject readFileAsJSONObject(java.lang.String path)
                                                                 throws java.io.IOException
        Read JSONDataResource file as JSONObject
        Parameters:
        path -
        Returns:
        JSONObject
        Throws:
        java.io.IOException
      • readFileAsJSONArray

        public static com.ibm.json.java.JSONArray readFileAsJSONArray(java.lang.String path)
                                                               throws java.io.IOException
        Read JSONDataResource file as JSONArray
        Parameters:
        path -
        Returns:
        JSONArray
        Throws:
        java.io.IOException
      • prepareHttpConnection

        public static java.net.HttpURLConnection prepareHttpConnection(java.lang.String user,
                                                                       java.lang.String pwd,
                                                                       java.lang.String path)
                                                                throws java.io.IOException
        Prepare HttpConnection based on JSONDataResource file Example: Path: /com/ibm/tivoli/maximo/assethealth/unittest/jsondatasource/expressionMethod.json The format of JSONDataResource: { "url":"os/mxapiahmethodology?_tenantcode=00&lean=1", "method": "POST", "headers": { "Content-Type": "application/json" }, "body": { "usewith": "ASSET", "worstscore": 5.0, "bestscore": 1.0, "expgroup": [ { "usewith": "ASSET", "expression": ":assettype='PUMP'", "egtype": "EXPRESSION", "intobjectname": "MXAPIASSET" } ], "autoconversion": true, "methodname": "UNITTEST_EXPMETHOD", "active": false } }
        Parameters:
        user -
        pwd -
        path -
        Returns:
        HttpURLConnection
        Throws:
        java.io.IOException
      • prepareHttpConnection

        public static java.net.HttpURLConnection prepareHttpConnection(java.lang.String user,
                                                                       java.lang.String pwd,
                                                                       java.lang.String path,
                                                                       java.lang.String name)
                                                                throws java.io.IOException
        Prepare HttpConnection based on JSONDataResource file Example: Path: /com/ibm/tivoli/maximo/assethealth/unittest/jsondatasource/ahhistoryAsset.json The format of JSONDataResource: { "createAsset":{ "url":"os/mxapiasset?_tenantcode=00&lean=1", "method": "POST", "headers": { "Content-Type": "application/json" }, "body": { "assetnum":"UTAHASSET", "siteid":"BEDFORD" } }, "updateAsset":{ "method": "PATCH", "headers": { "Content-Type": "application/json", "properties": "description" }, "body": { "description":"UT Asset AH History" } } }
        Parameters:
        user -
        pwd -
        path -
        name -
        Returns:
        HttpURLConnection
        Throws:
        java.io.IOException
      • prepareHttpConnection

        public static java.net.HttpURLConnection prepareHttpConnection(java.lang.String user,
                                                                       java.lang.String pwd,
                                                                       java.lang.String path,
                                                                       java.lang.String name,
                                                                       java.lang.String url,
                                                                       boolean fullUrl)
                                                                throws java.io.IOException
        Prepare HttpConnection based on JSONDataResource file Example: Path: /com/ibm/tivoli/maximo/assethealth/unittest/jsondatasource/ahhistoryAsset.json The format of JSONDataResource: { "updateAsset":{ "method": "PATCH", "headers": { "Content-Type": "application/json", "properties": "description" }, "body": { "description":"UT Asset AH History" } } }
        Parameters:
        user -
        pwd -
        path -
        name -
        Returns:
        HttpURLConnection
        Throws:
        java.io.IOException
      • getJSONDataSourceFromPath

        public static com.ibm.json.java.JSONObject getJSONDataSourceFromPath(java.lang.String path)
                                                                      throws java.io.IOException
        Get JSON Data Source from file and case name, allow add new attribute or content in.
        Parameters:
        user -
        pwd -
        path -
        name -
        Returns:
        Throws:
        java.io.IOException
      • getJSONDataSourceFromPathAndName

        public static com.ibm.json.java.JSONObject getJSONDataSourceFromPathAndName(java.lang.String path,
                                                                                    java.lang.String name)
                                                                             throws java.io.IOException
        Get JSON Data Source from file and case name, allow add new attribute or content in.
        Parameters:
        user -
        pwd -
        path -
        name -
        Returns:
        Throws:
        java.io.IOException
      • parseAndPrepareHttpConnection

        public static java.net.HttpURLConnection parseAndPrepareHttpConnection(java.lang.String user,
                                                                               java.lang.String pwd,
                                                                               com.ibm.json.java.JSONObject jsonDataSource,
                                                                               boolean fullUrl)
                                                                        throws java.io.IOException
        Prepare HTTPConnection based on jsonDataSource
        Parameters:
        user -
        pwd -
        jsonDataSource -
        fullUrl -
        Returns:
        Throws:
        java.io.IOException
      • prepareGetHttpConnection

        public static java.net.HttpURLConnection prepareGetHttpConnection(java.lang.String user,
                                                                          java.lang.String pwd,
                                                                          java.lang.String url,
                                                                          com.ibm.json.java.JSONObject headers)
                                                                   throws java.io.IOException
        Prepare HttpURLConnection based on full url and headers - headers can be null Example: String url = (String)UnitTestContext.getCurrentContext().getProperty("expressionURI"); HttpURLConnection con = APIUnitTestUtils.prepareGetHttpConnection(userid, password, url, null);
        Parameters:
        user -
        pwd -
        url -
        headers -
        Returns:
        HttpURLConnection
        Throws:
        java.io.IOException
      • prepareGetHttpConnection

        public static java.net.HttpURLConnection prepareGetHttpConnection(java.lang.String user,
                                                                          java.lang.String pwd,
                                                                          java.lang.String url,
                                                                          com.ibm.json.java.JSONObject headers,
                                                                          boolean fullUrl)
                                                                   throws java.io.IOException
        Prepare HttpURLConnection based on url and headers - headers can be null
        Parameters:
        user -
        pwd -
        url -
        headers -
        fullUrl -
        Returns:
        HttpURLConnection
        Throws:
        java.io.IOException
      • prepareHttpConnection

        public static java.net.HttpURLConnection prepareHttpConnection(java.lang.String user,
                                                                       java.lang.String pwd,
                                                                       java.lang.String url,
                                                                       java.lang.String method,
                                                                       com.ibm.json.java.JSONObject headers,
                                                                       com.ibm.json.java.JSONArtifact body,
                                                                       boolean fullUrl)
                                                                throws java.io.IOException
        Prepare HttpURLConnection based on url, method, headers and body - support GET/POST/DELETE - working on PATCH and others Example: HttpURLConnection con = APIUnitTestUtils.prepareHttpConnection(user, pwd, url, method, headers, body, false);
        Parameters:
        user -
        pwd -
        url -
        method -
        headers -
        body -
        fullUrl -
        Returns:
        HttpURLConnection
        Throws:
        java.io.IOException
      • setAuth

        public static void setAuth(java.lang.String user,
                                   java.lang.String pwd,
                                   java.net.HttpURLConnection con)
        Set up authentication to Maximo, only support maxauth for now
        Parameters:
        user -
        pwd -
        con -
      • assertResponseCode

        public static void assertResponseCode(java.net.HttpURLConnection con,
                                              int responseCode)
                                       throws java.io.IOException,
                                              MXException
        Assert response code, when the response code is greater or equal to 400, read the input stream for error Examples: APIUnitTestUtils.assertResponseCode(con, 200);
        Parameters:
        con -
        responseCode -
        Throws:
        java.io.IOException
        MXException
      • assertHeaders

        public static void assertHeaders(java.net.HttpURLConnection con,
                                         java.lang.String targetKey,
                                         java.lang.String targetValue)
                                  throws java.io.IOException,
                                         MXException
        Assert header value Example: APIUnitTestUtils.assertHeaders(con, key, "value");
        Parameters:
        con -
        targetKey -
        targetValue -
        Throws:
        java.io.IOException
        MXException
      • getResponse

        public static com.ibm.json.java.JSONObject getResponse(java.net.HttpURLConnection con)
                                                        throws java.io.IOException,
                                                               MXException
        Get response as JSONObject from HttpURLConnection Example: JSONObject response = APIUnitTestUtils.getResponse(con);
        Parameters:
        con -
        Returns:
        JSONObject
        Throws:
        java.io.IOException
        MXException
      • assertJSON

        public static void assertJSON(com.ibm.json.java.JSONArtifact jsonData,
                                      java.lang.String jsonPath,
                                      java.lang.String value)
                               throws java.io.IOException,
                                      MXException
        Assert JSON value by path using ExpressionEvaluator Example: APIUnitTestUtils.assertJSON(response, "methodname", "UNITTEST_EXPMETHOD");
        Parameters:
        jsonData -
        jsonPath -
        value -
        Throws:
        java.io.IOException
        MXException
      • assertJSONTrue

        public static void assertJSONTrue(com.ibm.json.java.JSONArtifact jsonData,
                                          java.lang.String jsonPath)
                                   throws java.io.IOException,
                                          MXException
        Assert JSON value true by path using ExpressionEvaluator Example: APIUnitTestUtils.assertJSONTrue(response, "weight>1");
        Parameters:
        jsonData -
        jsonPath -
        value -
        Throws:
        java.io.IOException
        MXException
      • assertJSONFalse

        public static void assertJSONFalse(com.ibm.json.java.JSONArtifact jsonData,
                                           java.lang.String jsonPath)
                                    throws java.io.IOException,
                                           MXException
        Assert JSON value false by path using ExpressionEvaluator Example: APIUnitTestUtils.assertJSONFalse(response, "weight<1");
        Parameters:
        jsonData -
        jsonPath -
        value -
        Throws:
        java.io.IOException
        MXException
      • deleteResource

        public static void deleteResource(java.lang.String user,
                                          java.lang.String pwd,
                                          java.lang.String url)
                                   throws java.io.IOException,
                                          MXException
        Delete unittest resource by url Examples: APIUnitTestUtils.deleteResource(userid, password, (String)UnitTestContext.getCurrentContext().getProperty("expressionURI"));
        Parameters:
        user -
        pwd -
        url -
        Throws:
        java.io.IOException
        MXException