Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#12048] Migrate tests for ActionFactoryTest #13283

Merged
merged 2 commits into from
Mar 26, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/test/java/teammates/sqlui/webapi/ActionFactoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package teammates.sqlui.webapi;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.testng.annotations.Test;

import teammates.common.util.Config;
import teammates.common.util.Const;
import teammates.test.BaseTestCase;
import teammates.test.MockHttpServletRequest;
import teammates.ui.webapi.Action;
import teammates.ui.webapi.ActionFactory;
import teammates.ui.webapi.ActionMappingException;
import teammates.ui.webapi.GetAuthInfoAction;

/**
* SUT: {@link ActionFactory}.
*/
public class ActionFactoryTest extends BaseTestCase {

@Test
void testGetAction_existingAction_success() throws Exception {
MockHttpServletRequest existingActionServletRequest = new MockHttpServletRequest(
HttpGet.METHOD_NAME, Const.ResourceURIs.AUTH);
existingActionServletRequest.addHeader(Const.HeaderNames.BACKDOOR_KEY, Config.BACKDOOR_KEY);
Action existingAction = ActionFactory.getAction(existingActionServletRequest, HttpGet.METHOD_NAME);
assertTrue(existingAction instanceof GetAuthInfoAction);
}

@Test
void testGetAction_nonExistentAction_throwsActionMappingException() {
MockHttpServletRequest nonExistentActionServletRequest = new MockHttpServletRequest(
HttpGet.METHOD_NAME, "/blahblahblah");
nonExistentActionServletRequest.addHeader(Const.HeaderNames.BACKDOOR_KEY, Config.BACKDOOR_KEY);
ActionMappingException nonExistentActionException = assertThrows(ActionMappingException.class,
() -> ActionFactory.getAction(nonExistentActionServletRequest, HttpGet.METHOD_NAME));
assertEquals("Resource with URI /blahblahblah is not found.", nonExistentActionException.getMessage());
}

@Test
void testGetAction_methodDoesNotExistOnAction_throwsActionMappingException() {
MockHttpServletRequest nonExistentMethodOnActionServletRequest = new MockHttpServletRequest(
HttpGet.METHOD_NAME, Const.ResourceURIs.AUTH);
nonExistentMethodOnActionServletRequest.addHeader(Const.HeaderNames.BACKDOOR_KEY, Config.BACKDOOR_KEY);
ActionMappingException nonExistentMethodOnActionException = assertThrows(ActionMappingException.class,
() -> ActionFactory.getAction(nonExistentMethodOnActionServletRequest, HttpPost.METHOD_NAME));
assertTrue(nonExistentMethodOnActionException.getMessage()
.equals("Method [" + HttpPost.METHOD_NAME + "] is not allowed for URI "
+ Const.ResourceURIs.AUTH + "."));
}
}
Loading