Files
SCM-Manager/scm-webapp/src/test/java/sonia/scm/web/protocol/HttpProtocolServletTest.java

161 lines
5.0 KiB
Java
Raw Normal View History

/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.web.protocol;
2019-07-22 14:26:15 +02:00
import org.junit.jupiter.api.BeforeEach;
2019-07-22 14:41:12 +02:00
import org.junit.jupiter.api.Nested;
2019-07-22 14:26:15 +02:00
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
2019-07-22 14:26:15 +02:00
import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.NotFoundException;
import sonia.scm.PushStateDispatcher;
import sonia.scm.repository.DefaultRepositoryProvider;
import sonia.scm.repository.NamespaceAndName;
import sonia.scm.repository.Repository;
2019-07-22 14:41:12 +02:00
import sonia.scm.repository.RepositoryTestData;
import sonia.scm.repository.api.RepositoryService;
import sonia.scm.repository.api.RepositoryServiceFactory;
import sonia.scm.repository.spi.HttpScmProtocol;
import sonia.scm.web.UserAgent;
import sonia.scm.web.UserAgentParser;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
2019-07-22 14:41:12 +02:00
import java.util.Optional;
2019-07-22 14:41:12 +02:00
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
2019-07-22 14:26:15 +02:00
@ExtendWith(MockitoExtension.class)
class HttpProtocolServletTest {
@Mock
private RepositoryServiceFactory serviceFactory;
2019-07-22 14:41:12 +02:00
@Mock
private NamespaceAndNameFromPathExtractor extractor;
@Mock
private PushStateDispatcher dispatcher;
2019-07-22 14:41:12 +02:00
@Mock
private UserAgentParser userAgentParser;
@InjectMocks
private HttpProtocolServlet servlet;
@Mock
private RepositoryService repositoryService;
2019-07-22 14:41:12 +02:00
@Mock
private UserAgent userAgent;
@Mock
private HttpServletRequest request;
2019-07-22 14:41:12 +02:00
@Mock
private HttpServletResponse response;
2019-07-22 14:41:12 +02:00
@Mock
private HttpScmProtocol protocol;
2019-07-22 14:41:12 +02:00
@Nested
class Browser {
@BeforeEach
void prepareMocks() {
when(userAgentParser.parse(request)).thenReturn(userAgent);
when(userAgent.isBrowser()).thenReturn(true);
when(request.getRequestURI()).thenReturn("uri");
}
2019-07-22 14:41:12 +02:00
@Test
void shouldDispatchBrowserRequests() throws ServletException, IOException {
when(userAgent.isBrowser()).thenReturn(true);
when(request.getRequestURI()).thenReturn("uri");
2019-07-22 14:41:12 +02:00
servlet.service(request, response);
verify(dispatcher).dispatch(request, response, "uri");
}
}
2019-07-22 14:41:12 +02:00
@Nested
class ScmClient {
2019-07-22 14:41:12 +02:00
@BeforeEach
void prepareMocks() {
when(userAgentParser.parse(request)).thenReturn(userAgent);
when(userAgent.isBrowser()).thenReturn(false);
}
2019-07-22 14:41:12 +02:00
@Test
void shouldHandleBadPaths() throws IOException, ServletException {
when(request.getPathInfo()).thenReturn("/illegal");
2019-07-22 14:41:12 +02:00
servlet.service(request, response);
2019-07-22 14:41:12 +02:00
verify(response).setStatus(400);
}
2019-07-22 14:41:12 +02:00
@Test
void shouldHandleNotExistingRepository() throws IOException, ServletException {
when(request.getPathInfo()).thenReturn("/not/exists");
NamespaceAndName repo = new NamespaceAndName("not", "exists");
when(extractor.fromUri("/not/exists")).thenReturn(Optional.of(repo));
when(serviceFactory.create(repo)).thenThrow(new NotFoundException("Test", "a"));
servlet.service(request, response);
verify(response).setStatus(404);
}
@Test
void shouldDelegateToProvider() throws IOException, ServletException {
NamespaceAndName repo = new NamespaceAndName("space", "name");
when(extractor.fromUri("/space/name")).thenReturn(Optional.of(repo));
when(serviceFactory.create(repo)).thenReturn(repositoryService);
when(request.getPathInfo()).thenReturn("/space/name");
Repository repository = RepositoryTestData.createHeartOfGold();
when(repositoryService.getRepository()).thenReturn(repository);
when(repositoryService.getProtocol(HttpScmProtocol.class)).thenReturn(protocol);
2019-07-22 14:41:12 +02:00
servlet.service(request, response);
2019-07-22 14:41:12 +02:00
verify(request).setAttribute(DefaultRepositoryProvider.ATTRIBUTE_NAME, repository);
verify(protocol).serve(request, response, null);
verify(repositoryService).close();
}
}
}