|
package dev.luke10x.easylogin.registration; |
|
|
|
import dev.luke10x.easylogin.common.FlashContainer; |
|
import jakarta.inject.Inject; |
|
import jakarta.mvc.binding.BindingResult; |
|
import jakarta.ws.rs.client.Entity; |
|
import jakarta.ws.rs.core.Application; |
|
import jakarta.ws.rs.core.Form; |
|
import jakarta.ws.rs.core.MediaType; |
|
import jakarta.ws.rs.core.Response; |
|
import jakarta.ws.rs.ext.ContextResolver; |
|
import jakarta.ws.rs.ext.Provider; |
|
import org.glassfish.jersey.client.ClientConfig; |
|
import org.glassfish.jersey.internal.inject.AbstractBinder; |
|
import org.glassfish.jersey.media.multipart.MultiPartFeature; |
|
import org.glassfish.jersey.server.ResourceConfig; |
|
import org.glassfish.jersey.server.ServerProperties; |
|
import org.glassfish.jersey.test.JerseyTest; |
|
import org.junit.jupiter.api.Test; |
|
import org.mockito.Mock; |
|
import org.mockito.MockitoAnnotations; |
|
|
|
import static jakarta.ws.rs.core.Response.Status.*; |
|
import static org.jboss.modules.management.ObjectProperties.property; |
|
import static org.junit.jupiter.api.Assertions.assertEquals; |
|
import static org.mockito.Mockito.when; |
|
|
|
public class RegistrationControllerJerseyTest extends JerseyTest { |
|
|
|
@Mock |
|
private RegistrationFormMapper registrationFormMapper; |
|
|
|
@Mock |
|
private RegistrationService registrationService; |
|
|
|
@Mock |
|
RegistrationModel registrationModel; |
|
|
|
@Inject |
|
BindingResult bindingResult; |
|
|
|
@Mock |
|
FlashContainer flashContainer; |
|
|
|
@Provider |
|
public static class BindingResultContextResolver implements ContextResolver<BindingResult> { |
|
|
|
private final BindingResult bindingResult; |
|
|
|
@Inject |
|
public BindingResultContextResolver(BindingResult bindingResult) { |
|
this.bindingResult = bindingResult; |
|
} |
|
|
|
@Override |
|
public BindingResult getContext(Class<?> type) { |
|
return bindingResult; |
|
} |
|
} |
|
|
|
@Override |
|
protected Application configure() { |
|
MockitoAnnotations.openMocks(this); |
|
|
|
// Configure the JAX-RS application with your controller and dependencies |
|
return new ResourceConfig(RegistrationController.class) |
|
.register(new AbstractBinder() { |
|
@Override |
|
protected void configure() { |
|
|
|
bind(registrationFormMapper).to(RegistrationFormMapper.class); |
|
bind(registrationService).to(RegistrationService.class); |
|
bind(registrationModel).to(RegistrationModel.class); |
|
// bind(bindingResult).to(BindingResult.class); |
|
bind(flashContainer).to(FlashContainer.class); |
|
} |
|
}) |
|
.packages("org.glassfish.jersey.examples.multipart") |
|
.register(MultiPartFeature.class); |
|
} |
|
// public ViewApplicationConfig() { |
|
// packages("com.baeldung.jersey.server"); |
|
// property(ServerProperties.BV_SEND_ERROR_IN_RESPONSE, Boolean.TRUE.toString()); |
|
// } |
|
// @Override |
|
// protected void configureClient(ClientConfig config) { |
|
// config.register(MultiPartFeature.class); |
|
// } |
|
|
|
|
|
@Test |
|
public void validationTest() { |
|
var response = target("register").request().get(Response.class); |
|
|
|
assertEquals(ACCEPTED, response.getStatus()); |
|
assertEquals("REDIRECTION", response.getStatusInfo().getFamily().name()); |
|
assertEquals("See Other", response.getStatusInfo().getReasonPhrase()); |
|
|
|
assertEquals("onboarding", response.getHeaders().getFirst("Location").toString()); |
|
} |
|
|
|
@Test |
|
public void validationTriesToBindErrors() { |
|
// when(bindingResult.isFailed()).thenReturn(true); |
|
|
|
|
|
final Form form = new Form(); |
|
form.param("handle", "this_is_me"); |
|
|
|
Response response = target("register").request().post(Entity.form(form)); |
|
|
|
assertEquals(BAD_REQUEST, response.getStatus()); |
|
assertEquals("REDIRECTION", response.getStatusInfo().getFamily().name()); |
|
assertEquals("See Other", response.getStatusInfo().getReasonPhrase()); |
|
|
|
assertEquals("onboarding", response.getHeaders().getFirst("Location").toString()); |
|
|
|
} |
|
|
|
@Test |
|
public void validationDifferently() { |
|
|
|
} |
|
} |