Changeset 441

Show
Ignore:
Timestamp:
03/29/07 17:09:23 (2 years ago)
Author:
richi
Message:

json rpc processor & test case refactored

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/main/php/net/stubbles/websites/processors/stubJsonRpcProcessor.php

    r435 r441  
    11<?php 
    2  
    3 stubClassLoader::load('net.stubbles.websites.processors.stubAbstractProcessor', 
    4                       'net.stubbles.util.validators.stubRegexValidator', 
    5                       'net.stubbles.ipo.request.filters.stubStringFilter', 
    6                       'net.stubbles.ipo.request.filters.stubPassThruFilter', 
    7                       'net.stubbles.ipo.request.stubXmlRequestValueErrorFactory'); 
    8                        
    92/** 
    10  * The JsonRpc Service 
     3 * JSON-RPC Processor (generic Proxy for Web Services) 
    114 * 
    125 * @author      Richard Sternagel 
     
    147 * @subpackage  websites_processors 
    158 */ 
     9stubClassLoader::load('net.stubbles.websites.processors.stubAbstractProcessor', 
     10                      'net.stubbles.util.validators.stubRegexValidator'); 
     11 
     12/** 
     13 * JSON-RPC Processor (generic Proxy for Web Services) 
     14 * 
     15 * @package     stubbles 
     16 * @subpackage  websites_processors 
     17 */ 
    1618class stubJsonRpcProcessor extends stubAbstractProcessor { 
    1719 
    18     const classAndMethodPattern = '^([a-zA-Z0-9_]*\.[a-zA-Z0-9_]*)$'; 
    19     // an paramAnnotation könnte Range gebunden werden, der den RegEx-Ausdruck (DefBereich) individuell bestimmt 
    20     const paramPattern  = '^[a-zA-Z0-9_]*$'; 
    21     const idPattern     = '^\d{6,7}$'; 
    22     const jsonPattern   = '^{\"method\":\"(.*)\",\"params\"(.*),\"id\":\"\d{6,7}\"}$'; 
     20    // TODO: catch exception from web service method and set error in JSON-Response 
     21    // TODO: paramAnnotation could define an individual Range for the RegExValidator 
     22     
     23    const CLASS_AND_METHOD_PATTERN = '^([a-zA-Z0-9_]*\.[a-zA-Z0-9_]*)$'; 
     24     
     25    const PARAM_PATTERN            = '^[a-zA-Z0-9_]*$'; 
     26    const ID_PATTERN               = '^\d{6,7}$'; 
     27    const JSON_PATTERN             = '^{\"method\":\"(.*)\",\"params\"(.*),\"id\":\"\d{6,7}\"}$'; 
    2328     
    2429    protected $jsonRpcResponse = array ( 
     
    2631                                        'result'    => null, 
    2732                                        'error'     => null, 
    28                                     ); 
     33                                       ); 
    2934 
    3035    protected $classMap = array( 
     
    3540    protected $classObj = null; 
    3641    protected $methodObj = null; 
    37      
    3842     
    3943    protected function getReflectionObject($class) { 
     
    7175     
    7276    public function getJsonResponseObject() { 
    73         return $this->jsonRpcResponse
     77        return json_encode($this->jsonRpcResponse)
    7478    } 
    7579     
     
    100104    protected function retrieveGETParams(){ 
    101105        $paramValues = array(); 
    102         $paramPattern = new stubRegexValidator(stubJsonRpcProcessor::paramPattern); 
     106        $paramPattern = new stubRegexValidator(self::PARAM_PATTERN); 
    103107        foreach ($this->methodObj->getParameters() as $param) { 
    104108            $paramName = $param->getName(); 
     
    145149     */ 
    146150    public function doGET() { 
    147         $classAndMethodPattern = new stubRegexValidator(stubJsonRpcProcessor::classAndMethodPattern); 
     151        $classAndMethodPattern = new stubRegexValidator(self::CLASS_AND_METHOD_PATTERN); 
    148152        $classAndMethod = $this->request->getValidatedValue($classAndMethodPattern, 'method'); 
    149153         
     
    161165        } 
    162166         
    163         $idPattern = new stubRegexValidator(stubJsonRpcProcessor::idPattern); 
     167        $idPattern = new stubRegexValidator(self::ID_PATTERN); 
    164168        $requestId = $this->request->getValidatedValue($idPattern, 'id'); 
    165169        $this->checkRequestId($requestId); 
     
    176180     */ 
    177181    public function doPOST() { 
    178         $jsonPattern     = new stubRegexValidator(stubJsonRpcProcessor::jsonPattern); 
     182        $jsonPattern     = new stubRegexValidator(self::JSON_PATTERN); 
    179183        $requestJsonObj  = $this->request->getValidatedRawData($jsonPattern); 
    180184        $phpJsonObj      = json_decode($requestJsonObj); 
    181185        $classAndMethod  = $phpJsonObj->method; 
    182186         
    183         if(isset($classAndMethod) && preg_match('/'.stubJsonRpcProcessor::classAndMethodPattern.'/', $classAndMethod)) { 
     187        if(isset($classAndMethod) && preg_match('/'.self::CLASS_AND_METHOD_PATTERN.'/', $classAndMethod)) { 
    184188            list($className, $methodName) = explode('.', $classAndMethod); 
    185189 
     
    206210        } 
    207211 
    208         $this->response->write(json_encode($this->getJsonResponseObject())); 
     212        $this->response->write($this->getJsonResponseObject()); 
    209213    } 
    210214} 
  • trunk/src/test/php/net/stubbles/websites/processors/stubJsonRpcProcessorTestCase.php

    r435 r441  
    11<?php 
     2/** 
     3 * Test for net.stubbles.websites.processors.stubJsonRpcProcessor 
     4 * 
     5 * @author          Richard Sternagel 
     6 * @package         stubbles 
     7 * @subpackage      websites_processors_test 
     8 */ 
    29stubClassLoader::load('net.stubbles.websites.processors.stubJsonRpcProcessor', 
    310                      '_test.BuddyQuoteService'); 
     
    512Mock::generate('stubRequest'); 
    613Mock::generate('stubSession'); 
    7                        
     14                      
    815/** 
    9  * Test for stubDomXMLStreamWrite
     16 * Tests for net.stubbles.websites.processors.stubJsonRpcProcesso
    1017 * 
    11  * @author          Richard Sternagel 
    12  * @package         stubbles 
    13  * @subpackage   
     18 * @package     stubbles 
     19 * @subpackage  websites_processors_test 
    1420 */ 
    1521class stubJsonRpcProcessorTestCase extends UnitTestCase { 
    1622     
     23    /** 
     24     * mocked page factory 
     25     * 
     26     * @var  SimpleMock 
     27     */ 
    1728    protected $mockPageFactory; 
    18     /** 
     29     
     30    /** 
    1931     * mocked request to use 
    2032     * 
     
    2234     */ 
    2335    protected $mockRequest; 
     36     
    2437    /** 
    2538     * mocked response instance 
     
    2841     */ 
    2942    protected $mockResponse; 
     43     
    3044    /** 
    3145     * mocked session to use 
     
    3347     * @var  SimpleMock 
    3448     */ 
     49    protected $mockSession; 
    3550     
    36     protected $mockSession       = null; 
    37     protected $jsonDummyReq      = null; 
    38     protected $jsonDummyResp     = null; 
    39     protected $bqService         = null; 
    40     protected $proc              = null; 
     51     /** 
     52     * dummy POST Request (JSON-RPC) 
     53     * 
     54     * @var  String  
     55     */ 
     56    protected $jsonDummyPOSTReq; 
    4157     
    4258    /** 
    43      * create enviroment 
     59     * dummy GET Request (Method) 
     60     * 
     61     * @var  String  
    4462     */ 
     63    protected $jsonDummyGETReqMethod; 
     64     
     65    /** 
     66     * dummy GET Request (Params) 
     67     * 
     68     * @var  String  
     69     */ 
     70    protected $jsonDummyGETReqParams; 
     71     
     72    /** 
     73     * dummy GET Request (Id) 
     74     * 
     75     * @var  String  
     76     */ 
     77    protected $jsonDummyGETReqId; 
     78     
     79    /** 
     80     * dummy JSON-RPC Response 
     81     * 
     82     * @var  String  
     83     */ 
     84    protected $jsonDummyResp; 
     85     
     86    /** 
     87     * mocked processor 
     88     * 
     89     * @var  stubJsonRpcProcessor 
     90     */ 
     91    protected $proc; 
     92     
     93    /** 
     94     * set up test environment 
     95     */ 
    4596    public function setUp() { 
    46         $this->jsonDummyReq     = '{"method":"BuddyQuoteService.getQuote","params":[5],"id":"186252"}'; 
    47         $this->jsonDummyResp    = '{"id":"186252","result":"An meinem Arm wird nicht gepuffert!","error":null}'; 
    48         $this->bqService        = new BuddyQuoteService(); 
     97        $this->jsonDummyPOSTReq      = '{"method":"BuddyQuoteService.getQuote","params":[5],"id":"186252"}'; 
    4998         
    50         $this->mockPageFactory = new MockstubPageFactory(); 
     99        $this->jsonDummyGETReqMethod = 'BuddyQuoteService.getQuote'; 
     100        $this->jsonDummyGETReqParams = '5'; 
     101        $this->jsonDummyGETReqId     = '186252'; 
     102         
     103        $this->jsonDummyResp         = '{"id":"186252","result":"An meinem Arm wird nicht gepuffert!","error":null}'; 
    51104         
    52         $this->mockRequest  = new MockstubRequest(); 
    53          
    54         $this->mockSession  = new MockstubSession(); 
    55         $this->mockResponse = new MockstubResponse(); 
    56          
    57         $this->proc         = new stubJsonRpcProcessor($this->mockRequest, 
    58                                                        $this->mockSession, 
    59                                                        $this->mockResponse, 
    60                                                        $this->mockPageFactory); 
     105        $this->mockRequest           = new MockstubRequest(); 
     106        $this->mockSession           = new MockstubSession(); 
     107        $this->mockResponse          = new MockstubResponse(); 
     108        $this->mockPageFactory       = new MockstubPageFactory(); 
     109        $this->proc                  = new stubJsonRpcProcessor($this->mockRequest, 
     110                                                                $this->mockSession, 
     111                                                                $this->mockResponse, 
     112                                                                $this->mockPageFactory); 
    61113    } 
    62114     
    63115    /** 
    64      * JSON-RPC GET request 
     116     * test HTTP-POST Request 
    65117     */ 
    66118    public function testDoPost() { 
    67         $buddyQuote = $this->bqService->getQuote(3); 
    68         $this->mockRequest->setReturnValue('getValidatedRawData', $this->jsonDummyReq); 
     119        $this->mockRequest->setReturnValue('getValidatedRawData', $this->jsonDummyPOSTReq); 
     120         
    69121        $this->proc->doPOST(); 
    70         $this->assertEqual($this->jsonDummyResp, json_encode($this->proc->getJsonResponseObject())); 
     122        $this->assertEqual($this->jsonDummyResp, $this->proc->getJsonResponseObject()); 
     123    } 
     124     
     125    /* 
     126    * test HTTP-GET Request 
     127    */ 
     128    public function testDoGet() { 
     129        $this->mockRequest->setReturnValue('getValidatedValue', $this->jsonDummyGETReqMethod, array('*', 'method')); 
     130        $this->mockRequest->setReturnValue('getValidatedValue', $this->jsonDummyGETReqParams, array('*', 'arrKey')); 
     131        $this->mockRequest->setReturnValue('getValidatedValue', $this->jsonDummyGETReqId,     array('*', 'id')); 
     132                                            
     133        $this->proc->doGET(); 
     134        $this->assertEqual($this->jsonDummyResp, $this->proc->getJsonResponseObject()); 
    71135    } 
    72136}