websrv_pub/hostlet_connection_api/tsrc/bc/hostconn/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 
       
    18 
       
    19 
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 
       
    25 //  CLASS HEADER
       
    26 #include "testhostlet.h"
       
    27 
       
    28 //  EXTERNAL INCLUDES
       
    29 #include <StifTestInterface.h>
       
    30 
       
    31 #include <SenTransportProperties.h>
       
    32 
       
    33 //  INTERNAL INCLUDES
       
    34 #include "senhostletconnectionbctest.h"
       
    35 
       
    36 namespace
       
    37     {
       
    38     //_LIT8(KHostletEndpoint, "hostlet://UnitTestHostlet");
       
    39     _LIT8(KHostletContract, "urn:nokia.com.serene:unit-test-hostlet");
       
    40     }
       
    41 
       
    42 CTestHostlet* CTestHostlet::NewL(CSenHostletConnectionBCTest* aTestSuite,
       
    43                                  TInt (*ap2ServiceL) (CSenHostletConnectionBCTest*, MSenHostletRequest&, MSenHostletResponse&),
       
    44                                  TInt (*ap2OnServiceCompleteL) (CSenHostletConnectionBCTest*, const TInt, TInt, const TDesC8&),
       
    45                                  void (*ap2DescribeServiceL)(CSenXmlServiceDescription&))
       
    46     {
       
    47     CTestHostlet* pSelf = CTestHostlet::NewLC(aTestSuite, ap2ServiceL, ap2OnServiceCompleteL, ap2DescribeServiceL);
       
    48     CleanupStack::Pop(pSelf);
       
    49     return pSelf;
       
    50     }
       
    51 
       
    52 CTestHostlet* CTestHostlet::NewLC(CSenHostletConnectionBCTest* aTestSuite,
       
    53                                   TInt (*ap2ServiceL) (CSenHostletConnectionBCTest*, MSenHostletRequest&, MSenHostletResponse&),
       
    54                                   TInt (*ap2OnServiceCompleteL) (CSenHostletConnectionBCTest*, const TInt, TInt, const TDesC8&),
       
    55                                   void (*ap2DescribeServiceL)(CSenXmlServiceDescription&))
       
    56     {
       
    57     CTestHostlet* pSelf = new (ELeave) CTestHostlet(aTestSuite, ap2ServiceL, ap2OnServiceCompleteL, ap2DescribeServiceL);
       
    58     CleanupStack::PushL(pSelf);
       
    59     pSelf->ConstructL();
       
    60 	    
       
    61     return pSelf;
       
    62     }
       
    63 
       
    64 CTestHostlet::CTestHostlet(CSenHostletConnectionBCTest* aTestSuite,
       
    65                            TInt (*ap2ServiceL) (CSenHostletConnectionBCTest*, MSenHostletRequest&, MSenHostletResponse&),
       
    66                            TInt (*ap2OnServiceCompleteL) (CSenHostletConnectionBCTest*, const TInt, TInt, const TDesC8&),
       
    67                            void (*ap2DescribeServiceL)(CSenXmlServiceDescription&))
       
    68     : ipTestSuite(aTestSuite),
       
    69       ip2ServiceL(ap2ServiceL),
       
    70       ip2OnServiceCompleteL(ap2OnServiceCompleteL),
       
    71       ip2DescribeServiceL(ap2DescribeServiceL)
       
    72     {
       
    73     }
       
    74 
       
    75 CTestHostlet::~CTestHostlet()
       
    76     {
       
    77     delete ipHostletDescription;
       
    78     iRequests.ResetAndDestroy();
       
    79     iResponses.ResetAndDestroy();
       
    80     iRequestProps.ResetAndDestroy();
       
    81     iRequestPropTypes.Reset();
       
    82     iResponseProps.ResetAndDestroy();
       
    83     iResponseCodes.Reset();
       
    84     iTxnIds.Reset();
       
    85     iCompletionCodes.Reset();
       
    86     }
       
    87 
       
    88 void CTestHostlet::ConstructL()
       
    89     {
       
    90     ipHostletDescription = CSenXmlServiceDescription::NewL(KNullDesC8, KHostletContract);
       
    91 	//ipHostletDescription = CSenXmlServiceDescription::NewL(KHostletEndpoint, KNullDesC8);    	
       
    92     ipHostletDescription->SetFrameworkIdL(KDefaultRestServicesFrameworkID);
       
    93     }
       
    94     
       
    95 
       
    96 void CTestHostlet::DescribeServiceL(CSenXmlServiceDescription& aSD) 
       
    97     { 
       
    98     if( ip2DescribeServiceL ) 
       
    99     	{
       
   100    		(*ip2DescribeServiceL)(aSD);
       
   101     	}
       
   102         else
       
   103             {
       
   104             aSD.SetEndPointL(Endpoint());
       
   105             aSD.SetContractL(Contract()); 
       
   106             aSD.SetFrameworkIdL(FrameworkId());	
       
   107             }
       
   108     }
       
   109 
       
   110 TPtrC8 CTestHostlet::Contract() const
       
   111     {
       
   112     return ipHostletDescription->Contract();
       
   113     }
       
   114 
       
   115 TPtrC8 CTestHostlet::Endpoint() const
       
   116     {
       
   117     return ipHostletDescription->Endpoint();
       
   118     }
       
   119 
       
   120 TInt CTestHostlet::ServiceL(MSenHostletRequest& aRequest, MSenHostletResponse& aResponse)
       
   121     {
       
   122     
       
   123 
       
   124     iTxnIds.Append(aRequest.RequestId());
       
   125     iResponseCodes.Append(KErrNone);
       
   126     
       
   127 
       
   128     	
       
   129     TInt retVal(KErrNone);
       
   130     // Setup default response, can be overridden by test case:
       
   131     // Dummy response
       
   132     TPtrC8 request = aRequest.RequestUtf8();
       
   133 
       
   134 
       
   135     iRequests.Append(request.AllocL());
       
   136     
       
   137     MSenProperties::TSenPropertiesClassType propType;
       
   138     TPtrC8 requestProperties = aRequest.Properties(propType);
       
   139     iRequestProps.Append(requestProperties.AllocL());
       
   140     iRequestPropTypes.Append(propType);
       
   141     
       
   142 //    _LIT8(KResponseFmt, "<Response><ReplyMessage>CSenHostletConnectionBCTest is alive.</ReplyMessage><Request>%S</Request></Response>");
       
   143   	_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>");  
       
   144     HBufC8* pResponse = HBufC8::NewLC(request.Length()+KResponseFmt().Length());
       
   145     TInt appendError = iResponses.Append(pResponse);
       
   146     if( appendError )
       
   147         {
       
   148         CleanupStack::PopAndDestroy(pResponse); // delete orphan HBufC8 / pResponse
       
   149         }
       
   150     else
       
   151         {
       
   152         CleanupStack::Pop(pResponse); // now owned by iResponses -array..
       
   153         }        
       
   154     
       
   155     TPtr8 response = iResponses[iTxnIds.Count()-1]->Des();
       
   156     response.Format(KResponseFmt, &request);
       
   157     aResponse.SetResponseUtf8L(response, iResponseCodes[iTxnIds.Count()-1]); // by default, all requests are "accepted"
       
   158       
       
   159     if ( ip2ServiceL )
       
   160         {
       
   161         // NOTE(!): if RESPONSE CODE in aResponse is modified in YOUR test case,
       
   162         // remember set iResponseCodes for this test hostlet instance in your test
       
   163         //  case code (ServiceL callback)
       
   164         retVal = (*ip2ServiceL)(ipTestSuite, aRequest, aResponse); 
       
   165         }
       
   166     TPtrC8 providedResponse = aResponse.ResponseUtf8();
       
   167     
       
   168     if ( iResponses[iTxnIds.Count()-1]->Compare(providedResponse) != 0 )
       
   169         {
       
   170         // response was altered by TEST CASE:
       
   171         iResponses.Insert(aResponse.ResponseUtf8().AllocL(),iTxnIds.Count()-1);
       
   172         }
       
   173         
       
   174         
       
   175     RDebug::Print(_L("CTestHostlet::ServiceL received:"));
       
   176     RDebug::RawPrint(request);
       
   177     if (ipTestSuite->ipUsedHostletConnection)
       
   178     {
       
   179     	iRespondRetCode = ipTestSuite->ipUsedHostletConnection->RespondL(aResponse); 
       
   180     }
       
   181     else
       
   182 		iRespondRetCode = ipTestSuite->ipHostletConnection->RespondL(aResponse); 
       
   183    return retVal;
       
   184     }
       
   185 
       
   186 void CTestHostlet::OnServiceCompleteL(const TInt aTxnId, 
       
   187                                       TInt aCompletionCode, 
       
   188                                       const TDesC8& aDesc)
       
   189     {
       
   190   
       
   191     iCompletionCodes.Append(aCompletionCode); 
       
   192     if( ip2OnServiceCompleteL )
       
   193         {
       
   194         (*ip2OnServiceCompleteL)( ipTestSuite, aTxnId, aCompletionCode, aDesc );
       
   195         }
       
   196     ipTestSuite->StopActiveScheduler(); // service provided
       
   197     }
       
   198     
       
   199 //  END OF FILE