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