Skip to content

Commit 47d9736

Browse files
committed
Modify unit tests
1 parent b69e3a6 commit 47d9736

File tree

2 files changed

+64
-3
lines changed
  • components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test

2 files changed

+64
-3
lines changed

components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/java/org/wso2/carbon/identity/application/authentication/framework/handler/sequence/impl/GraphBasedSequenceHandlerLongWaitTest.java

+51-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsFunctionRegistryImpl;
3131
import org.wso2.carbon.identity.application.authentication.framework.config.model.graph.JsGraphBuilder;
3232
import org.wso2.carbon.identity.application.authentication.framework.context.AuthenticationContext;
33+
import org.wso2.carbon.identity.application.authentication.framework.context.TransientObjectWrapper;
3334
import org.wso2.carbon.identity.application.authentication.framework.dao.impl.CacheBackedLongWaitStatusDAO;
3435
import org.wso2.carbon.identity.application.authentication.framework.dao.impl.LongWaitStatusDAOImpl;
3536
import org.wso2.carbon.identity.application.authentication.framework.internal.FrameworkServiceDataHolder;
@@ -45,13 +46,21 @@
4546
import org.wso2.carbon.user.core.util.UserCoreUtil;
4647
import org.wso2.carbon.utils.multitenancy.MultitenantConstants;
4748

49+
import java.util.ArrayList;
4850
import java.util.Collections;
51+
import java.util.HashMap;
52+
import java.util.List;
4953
import java.util.Map;
5054

5155
import javax.servlet.http.HttpServletRequest;
5256
import javax.servlet.http.HttpServletResponse;
5357

58+
import static org.mockito.ArgumentMatchers.any;
59+
import static org.mockito.ArgumentMatchers.anyString;
60+
import static org.mockito.Mockito.doAnswer;
5461
import static org.mockito.Mockito.mock;
62+
import static org.testng.Assert.assertTrue;
63+
import static org.wso2.carbon.identity.application.authentication.framework.util.FrameworkConstants.RequestAttribute.HTTP_REQUEST;
5564

5665
@Test
5766
@WithH2Database(jndiName = "jdbc/WSO2IdentityDB", files = {"dbScripts/h2.sql"})
@@ -100,13 +109,15 @@ public void testHandleLongWait() throws Exception {
100109
context.setSequenceConfig(sequenceConfig);
101110

102111
HttpServletRequest req = createMockHttpServletRequest();
112+
context.setProperty(HTTP_REQUEST, new TransientObjectWrapper<>(req));
103113

104114
HttpServletResponse resp = mock(HttpServletResponse.class);
105115

106116
UserCoreUtil.setDomainInThreadLocal("test_domain");
107117

108118
graphBasedSequenceHandler.handle(req, resp, context);
109119

120+
assertTrue(context.isRequestAuthenticated());
110121
}
111122

112123
@FunctionalInterface
@@ -122,10 +133,49 @@ public static class AsyncAnalyticsCbFunctionImpl implements Fn1 {
122133
public void publishEvent(String siddhiAppName, String inStreamName, String outStreamName,
123134
Map<String, Object> payloadData, Map<String, Object> eventHandlers) {
124135

136+
Map<String, Object> propMap = new HashMap<>();
137+
138+
Map<String, String> nestedMap1 = new HashMap<>();
139+
nestedMap1.put("key1", "value1");
140+
nestedMap1.put("key2", "value2");
141+
142+
Map<String, String> nestedMap2 = new HashMap<>();
143+
nestedMap2.put("key3", "value3");
144+
nestedMap2.put("key4", "value4");
145+
146+
Object[] arrayElement = new Object[2];
147+
arrayElement[0] = nestedMap1;
148+
arrayElement[1] = "arrayString";
149+
150+
List<Object> listElement = new ArrayList<>();
151+
listElement.add(nestedMap2);
152+
listElement.add("listString");
153+
154+
propMap.put("arrayKey", arrayElement);
155+
propMap.put("listKey", listElement);
156+
125157
AsyncProcess asyncProcess = new AsyncProcess((ctx, r) -> {
126-
r.accept(ctx, Collections.emptyMap(), "onSuccess");
158+
r.accept(ctx, propMap, "onSuccess");
127159
});
128160
JsGraphBuilder.addLongWaitProcess(asyncProcess, eventHandlers);
129161
}
130162
}
163+
164+
protected HttpServletRequest createMockHttpServletRequest() {
165+
166+
HttpServletRequest req = mock(HttpServletRequest.class);
167+
Map<String, Object> attributes = new HashMap<>();
168+
doAnswer(m -> attributes.put(m.getArgument(0), m.getArgument(1))).when(req)
169+
.setAttribute(anyString(), any());
170+
171+
doAnswer(m -> attributes.get(m.getArgument(0))).when(req).getAttribute(anyString());
172+
173+
Map<String, Object> parameters = new HashMap<>();
174+
parameters.put("stringAttribute", "stringAttributeValue");
175+
parameters.put("arrayAttribute", new String[]{"arrayValue1", "arrayValue2"});
176+
177+
doAnswer(m -> parameters).when(req).getParameterMap();
178+
179+
return req;
180+
}
131181
}

components/authentication-framework/org.wso2.carbon.identity.application.authentication.framework/src/test/resources/org/wso2/carbon/identity/application/authentication/framework/handler/sequence/impl/js-sp-longwait-1.xml

+13-2
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,19 @@ var onLoginRequest = function(context) {
8484
//TODO remove this executeStep and make it to work.
8585
executeStep(1);
8686
testLongWaitCall('a', 'b', 'c', {}, {
87-
"onSuccess" : function(){Log.info('OK Called');
88-
}
87+
"onSuccess" : function(context, data)
88+
{
89+
Log.info('OK Called');
90+
if (data.arrayKey[0].key1 !== 'value1') {
91+
fail();
92+
}
93+
if (data.listKey[0].key3 !== 'value3') {
94+
fail();
95+
}
96+
if (context.request.params.stringAttribute !== 'stringAttributeValue' || context.request.params.arrayAttribute[0] !== 'arrayValue1') {
97+
fail();
98+
}
99+
}
89100
});
90101
};
91102
]]></AuthenticationScript>

0 commit comments

Comments
 (0)