websrv_pub/hostlet_connection_api/tsrc/hostconnTester/src/testhostlet.cpp
changeset 0 62f9d29f7211
equal deleted inserted replaced
-1:000000000000 0:62f9d29f7211
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:      
       
    15 *
       
    16 */
       
    17 //  CLASS HEADER
       
    18 #include "TestHostlet.h"
       
    19 
       
    20 //  EXTERNAL INCLUDES
       
    21 #include <Stiftestinterface.h>
       
    22 
       
    23 #include <SenTransportProperties.h>
       
    24 
       
    25 //  INTERNAL INCLUDES
       
    26 #include "hostconnTester.h"
       
    27 
       
    28 namespace
       
    29     {
       
    30     //_LIT8(KHostletEndpoint, "hostlet://UnitTestHostlet");
       
    31     _LIT8(KHostletContract, "urn:nokia.com.serene:unit-test-hostlet");
       
    32     }
       
    33 
       
    34 CTestHostlet* CTestHostlet::NewL(ChostconnTester* aTestSuite,
       
    35                                  TInt (*ap2ServiceL) (ChostconnTester*, MSenHostletRequest&, MSenHostletResponse&),
       
    36                                  TInt (*ap2OnServiceCompleteL) (ChostconnTester*, const TInt, TInt, const TDesC8&),
       
    37                                  void (*ap2DescribeServiceL)(CSenXmlServiceDescription&))
       
    38     {
       
    39     CTestHostlet* pSelf = CTestHostlet::NewLC(aTestSuite, ap2ServiceL, ap2OnServiceCompleteL, ap2DescribeServiceL);
       
    40     CleanupStack::Pop(pSelf);
       
    41     return pSelf;
       
    42     }
       
    43 
       
    44 CTestHostlet* CTestHostlet::NewLC(ChostconnTester* aTestSuite,
       
    45                                   TInt (*ap2ServiceL) (ChostconnTester*, MSenHostletRequest&, MSenHostletResponse&),
       
    46                                   TInt (*ap2OnServiceCompleteL) (ChostconnTester*, const TInt, TInt, const TDesC8&),
       
    47                                   void (*ap2DescribeServiceL)(CSenXmlServiceDescription&))
       
    48     {
       
    49     CTestHostlet* pSelf = new (ELeave) CTestHostlet(aTestSuite, ap2ServiceL, ap2OnServiceCompleteL, ap2DescribeServiceL);
       
    50     CleanupStack::PushL(pSelf);
       
    51     pSelf->ConstructL();
       
    52 	    
       
    53     return pSelf;
       
    54     }
       
    55 
       
    56 CTestHostlet::CTestHostlet(ChostconnTester* aTestSuite,
       
    57                            TInt (*ap2ServiceL) (ChostconnTester*, MSenHostletRequest&, MSenHostletResponse&),
       
    58                            TInt (*ap2OnServiceCompleteL) (ChostconnTester*, const TInt, TInt, const TDesC8&),
       
    59                            void (*ap2DescribeServiceL)(CSenXmlServiceDescription&))
       
    60     : ipTestSuite(aTestSuite),
       
    61       ip2ServiceL(ap2ServiceL),
       
    62       ip2OnServiceCompleteL(ap2OnServiceCompleteL),
       
    63       ip2DescribeServiceL(ap2DescribeServiceL)
       
    64     {
       
    65     }
       
    66 
       
    67 CTestHostlet::~CTestHostlet()
       
    68     {
       
    69     delete ipHostletDescription;
       
    70     iRequests.ResetAndDestroy();
       
    71     iResponses.ResetAndDestroy();
       
    72     iRequestProps.ResetAndDestroy();
       
    73     iRequestPropTypes.Reset();
       
    74     iResponseProps.ResetAndDestroy();
       
    75     iResponseCodes.Reset();
       
    76     iTxnIds.Reset();
       
    77     iCompletionCodes.Reset();
       
    78     }
       
    79 
       
    80 void CTestHostlet::ConstructL()
       
    81     {
       
    82     ipHostletDescription = CSenXmlServiceDescription::NewL(KNullDesC8, KHostletContract);
       
    83 	//ipHostletDescription = CSenXmlServiceDescription::NewL(KHostletEndpoint, KNullDesC8);    	
       
    84     ipHostletDescription->SetFrameworkIdL(KDefaultRestServicesFrameworkID);
       
    85     }
       
    86     
       
    87 
       
    88 void CTestHostlet::DescribeServiceL(CSenXmlServiceDescription& aSD) 
       
    89     { 
       
    90     if( ip2DescribeServiceL ) 
       
    91     	{
       
    92    		(*ip2DescribeServiceL)(aSD);
       
    93     	}
       
    94         else
       
    95             {
       
    96             aSD.SetEndPointL(Endpoint());
       
    97             aSD.SetContractL(Contract()); 
       
    98             aSD.SetFrameworkIdL(FrameworkId());	
       
    99             }
       
   100     }
       
   101 
       
   102 TPtrC8 CTestHostlet::Contract() const
       
   103     {
       
   104     return ipHostletDescription->Contract();
       
   105     }
       
   106 
       
   107 TPtrC8 CTestHostlet::Endpoint() const
       
   108     {
       
   109     return ipHostletDescription->Endpoint();
       
   110     }
       
   111 
       
   112 TInt CTestHostlet::ServiceL(MSenHostletRequest& aRequest, MSenHostletResponse& aResponse)
       
   113     {
       
   114     
       
   115 
       
   116     iTxnIds.Append(aRequest.RequestId());
       
   117     iResponseCodes.Append(KErrNone);
       
   118     
       
   119 
       
   120     	
       
   121     TInt retVal(KErrNone);
       
   122     // Setup default response, can be overridden by test case:
       
   123     // Dummy response
       
   124     TPtrC8 request = aRequest.RequestUtf8();
       
   125 
       
   126 
       
   127     iRequests.Append(request.AllocL());
       
   128     
       
   129     MSenProperties::TSenPropertiesClassType propType;
       
   130     TPtrC8 requestProperties = aRequest.Properties(propType);
       
   131     iRequestProps.Append(requestProperties.AllocL());
       
   132     iRequestPropTypes.Append(propType);
       
   133     
       
   134 //    _LIT8(KResponseFmt, "<Response><ReplyMessage>ChostconnTester is alive.</ReplyMessage><Request>%S</Request></Response>");
       
   135   	_LIT8(KResponseFmt, "<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\"><S:Header></S:Header><S:Body>testing text hostlet</S:Body></S:Envelope>");  
       
   136     HBufC8* pResponse = HBufC8::NewLC(request.Length()+KResponseFmt().Length());
       
   137     TInt appendError = iResponses.Append(pResponse);
       
   138     if( appendError )
       
   139         {
       
   140         CleanupStack::PopAndDestroy(pResponse); // delete orphan HBufC8 / pResponse
       
   141         }
       
   142     else
       
   143         {
       
   144         CleanupStack::Pop(pResponse); // now owned by iResponses -array..
       
   145         }        
       
   146     
       
   147     TPtr8 response = iResponses[iTxnIds.Count()-1]->Des();
       
   148     response.Format(KResponseFmt, &request);
       
   149     aResponse.SetResponseUtf8L(response, iResponseCodes[iTxnIds.Count()-1]); // by default, all requests are "accepted"
       
   150       
       
   151     if ( ip2ServiceL )
       
   152         {
       
   153         // NOTE(!): if RESPONSE CODE in aResponse is modified in YOUR test case,
       
   154         // remember set iResponseCodes for this test hostlet instance in your test
       
   155         //  case code (ServiceL callback)
       
   156         retVal = (*ip2ServiceL)(ipTestSuite, aRequest, aResponse); 
       
   157         }
       
   158     TPtrC8 providedResponse = aResponse.ResponseUtf8();
       
   159     
       
   160     if ( iResponses[iTxnIds.Count()-1]->Compare(providedResponse) != 0 )
       
   161         {
       
   162         // response was altered by TEST CASE:
       
   163         iResponses.Insert(aResponse.ResponseUtf8().AllocL(),iTxnIds.Count()-1);
       
   164         }
       
   165         
       
   166         
       
   167     RDebug::Print(_L("CTestHostlet::ServiceL received:"));
       
   168     RDebug::RawPrint(request);
       
   169     if (ipTestSuite->ipUsedHostletConnection)
       
   170     {
       
   171     	iRespondRetCode = ipTestSuite->ipUsedHostletConnection->RespondL(aResponse);
       
   172     }
       
   173     else
       
   174 		iRespondRetCode = ipTestSuite->ipHostletConnection->RespondL(aResponse); 
       
   175    return retVal;
       
   176     }
       
   177 
       
   178 void CTestHostlet::OnServiceCompleteL(const TInt aTxnId, 
       
   179                                       TInt aCompletionCode, 
       
   180                                       const TDesC8& aDesc)
       
   181     {
       
   182   
       
   183     iCompletionCodes.Append(aCompletionCode); 
       
   184     if( ip2OnServiceCompleteL )
       
   185         {
       
   186         (*ip2OnServiceCompleteL)( ipTestSuite, aTxnId, aCompletionCode, aDesc );
       
   187         }
       
   188     ipTestSuite->StopActiveScheduler(); // service provided
       
   189     }
       
   190     
       
   191 //  END OF FILE