Skip to content
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hadoop.hdds.utils.db;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import org.junit.jupiter.api.Test;
import org.rocksdb.RocksDBException;

/**
* Tests for RocksDatabaseException.
*/
public class TestRocksDatabaseException {

@Test
public void testConstructorWithMessage() {
RocksDatabaseException ex = new RocksDatabaseException("test error");
assertThat(ex.getMessage()).isEqualTo("test error");
assertThat(ex.getCause()).isNull();
assertThat(ex).isInstanceOf(IOException.class);
}

@Test
public void testDefaultConstructor() {
RocksDatabaseException ex = new RocksDatabaseException();
assertThat(ex.getMessage()).isNull();
assertThat(ex.getCause()).isNull();
}

@Test
public void testConstructorWithGenericCause() {
Exception cause = new RuntimeException("generic error");
RocksDatabaseException ex = new RocksDatabaseException(
"wrapped error", cause);
assertThat(ex.getMessage()).isEqualTo("wrapped error");
assertThat(ex.getCause()).isEqualTo(cause);
}

@Test
public void testConstructorWithRocksDBException() {
RocksDBException cause = new RocksDBException("rocksdb failure");
RocksDatabaseException ex = new RocksDatabaseException(
"operation failed", cause);
assertThat(ex.getMessage()).contains("operation failed");
assertThat(ex.getCause()).isEqualTo(cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.ozone.fs.http.server;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.io.PrintWriter;
import java.io.StringWriter;
import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.ozone.fs.http.HttpFSConstants;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
* Tests for CheckUploadContentTypeFilter.
*/
public class TestCheckUploadContentTypeFilter {

private CheckUploadContentTypeFilter filter;
private HttpServletRequest request;
private HttpServletResponse response;
private FilterChain chain;

@BeforeEach
public void setUp() {
filter = new CheckUploadContentTypeFilter();
request = mock(HttpServletRequest.class);
response = mock(HttpServletResponse.class);
chain = mock(FilterChain.class);
}

@Test
public void testGetRequestPassesThrough() throws Exception {
when(request.getMethod()).thenReturn("GET");
filter.doFilter(request, response, chain);
verify(chain).doFilter(request, response);
}

@Test
public void testDeleteRequestPassesThrough() throws Exception {
when(request.getMethod()).thenReturn("DELETE");
filter.doFilter(request, response, chain);
verify(chain).doFilter(request, response);
}

@Test
public void testPutWithNonUploadOpPassesThrough() throws Exception {
when(request.getMethod()).thenReturn("PUT");
when(request.getParameter(HttpFSConstants.OP_PARAM)).thenReturn("MKDIRS");
filter.doFilter(request, response, chain);
verify(chain).doFilter(request, response);
}

@Test
public void testPutCreateWithDataFalsePassesThrough() throws Exception {
when(request.getMethod()).thenReturn("PUT");
when(request.getParameter(HttpFSConstants.OP_PARAM)).thenReturn("CREATE");
when(request.getParameter(HttpFSParametersProvider.DataParam.NAME)).thenReturn("false");
filter.doFilter(request, response, chain);
verify(chain).doFilter(request, response);
}

@Test
public void testPutCreateWithDataTrueAndCorrectContentType() throws Exception {
when(request.getMethod()).thenReturn("PUT");
when(request.getParameter(HttpFSConstants.OP_PARAM)).thenReturn("CREATE");
when(request.getParameter(HttpFSParametersProvider.DataParam.NAME)).thenReturn("true");
when(request.getContentType()).thenReturn(HttpFSConstants.UPLOAD_CONTENT_TYPE);
filter.doFilter(request, response, chain);
verify(chain).doFilter(request, response);
}

@Test
public void testPutCreateWithDataTrueAndWrongContentType() throws Exception {
when(request.getMethod()).thenReturn("PUT");
when(request.getParameter(HttpFSConstants.OP_PARAM)).thenReturn("CREATE");
when(request.getParameter(HttpFSParametersProvider.DataParam.NAME)).thenReturn("true");
when(request.getContentType()).thenReturn("text/plain");

StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
when(response.getWriter()).thenReturn(pw);

filter.doFilter(request, response, chain);

verify(chain, never()).doFilter(request, response);
verify(response).setStatus(HttpServletResponse.SC_BAD_REQUEST);
}

@Test
public void testPostAppendWithDataTrueAndCorrectContentType() throws Exception {
when(request.getMethod()).thenReturn("POST");
when(request.getParameter(HttpFSConstants.OP_PARAM)).thenReturn("APPEND");
when(request.getParameter(HttpFSParametersProvider.DataParam.NAME)).thenReturn("true");
when(request.getContentType()).thenReturn(HttpFSConstants.UPLOAD_CONTENT_TYPE);
filter.doFilter(request, response, chain);
verify(chain).doFilter(request, response);
}

@Test
public void testPostAppendWithDataTrueAndWrongContentType() throws Exception {
when(request.getMethod()).thenReturn("POST");
when(request.getParameter(HttpFSConstants.OP_PARAM)).thenReturn("APPEND");
when(request.getParameter(HttpFSParametersProvider.DataParam.NAME)).thenReturn("true");
when(request.getContentType()).thenReturn("text/plain");

StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
when(response.getWriter()).thenReturn(pw);

filter.doFilter(request, response, chain);

verify(chain, never()).doFilter(request, response);
verify(response).setStatus(HttpServletResponse.SC_BAD_REQUEST);
}

@Test
public void testPutWithNullOpPassesThrough() throws Exception {
when(request.getMethod()).thenReturn("PUT");
when(request.getParameter(HttpFSConstants.OP_PARAM)).thenReturn(null);
filter.doFilter(request, response, chain);
verify(chain).doFilter(request, response);
}

@Test
public void testPutCreateCaseInsensitiveOp() throws Exception {
when(request.getMethod()).thenReturn("PUT");
when(request.getParameter(HttpFSConstants.OP_PARAM)).thenReturn("create");
when(request.getParameter(HttpFSParametersProvider.DataParam.NAME)).thenReturn("true");
when(request.getContentType()).thenReturn(HttpFSConstants.UPLOAD_CONTENT_TYPE);
filter.doFilter(request, response, chain);
verify(chain).doFilter(request, response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.ozone.fs.http.server;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.FileNotFoundException;
import java.io.IOException;
import javax.ws.rs.core.Response;
import org.apache.ozone.lib.service.FileSystemAccessException;
import org.apache.ozone.lib.wsrs.ExceptionProvider;
import org.junit.jupiter.api.Test;

/**
* Tests for HttpFSExceptionProvider and ExceptionProvider.
*/
public class TestHttpFSExceptionProvider {

@Test
public void testSecurityException() {
HttpFSExceptionProvider provider = new HttpFSExceptionProvider();
Response response = provider.toResponse(new SecurityException("denied"));
assertThat(response.getStatus()).isEqualTo(Response.Status.UNAUTHORIZED.getStatusCode());
}

@Test
public void testFileNotFoundException() {
HttpFSExceptionProvider provider = new HttpFSExceptionProvider();
Response response = provider.toResponse(new FileNotFoundException("missing"));
assertThat(response.getStatus()).isEqualTo(Response.Status.NOT_FOUND.getStatusCode());
}

@Test
public void testIOException() {
HttpFSExceptionProvider provider = new HttpFSExceptionProvider();
Response response = provider.toResponse(new IOException("io error"));
assertThat(response.getStatus()).isEqualTo(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
}

@Test
public void testUnsupportedOperationException() {
HttpFSExceptionProvider provider = new HttpFSExceptionProvider();
Response response = provider.toResponse(new UnsupportedOperationException("not supported"));
assertThat(response.getStatus()).isEqualTo(Response.Status.BAD_REQUEST.getStatusCode());
}

@Test
public void testIllegalArgumentException() {
HttpFSExceptionProvider provider = new HttpFSExceptionProvider();
Response response = provider.toResponse(new IllegalArgumentException("bad arg"));
assertThat(response.getStatus()).isEqualTo(Response.Status.BAD_REQUEST.getStatusCode());
}

@Test
public void testGenericException() {
HttpFSExceptionProvider provider = new HttpFSExceptionProvider();
Response response = provider.toResponse(new RuntimeException("unexpected"));
assertThat(response.getStatus()).isEqualTo(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
}

@Test
public void testFileSystemAccessExceptionUnwrapsToFileNotFound() {
HttpFSExceptionProvider provider = new HttpFSExceptionProvider();
FileSystemAccessException fsae = new FileSystemAccessException(
FileSystemAccessException.ERROR.H08, new FileNotFoundException("missing"));
Response response = provider.toResponse(fsae);
assertThat(response.getStatus()).isEqualTo(Response.Status.NOT_FOUND.getStatusCode());
}

@Test
public void testBaseExceptionProvider() {
ExceptionProvider provider = new ExceptionProvider();
Response response = provider.toResponse(new RuntimeException("test"));
assertThat(response.getStatus()).isEqualTo(Response.Status.BAD_REQUEST.getStatusCode());
}

@Test
public void testGetOneLineMessage() {
TestableExceptionProvider provider = new TestableExceptionProvider();
String message = provider.extractOneLineMessage(new RuntimeException("line1\nline2"));
assertThat(message).isEqualTo("line1");
}

@Test
public void testGetOneLineMessageNull() {
TestableExceptionProvider provider = new TestableExceptionProvider();
String message = provider.extractOneLineMessage(new RuntimeException((String) null));
assertThat(message).isNull();
}

private static class TestableExceptionProvider extends ExceptionProvider {
String extractOneLineMessage(Throwable throwable) {
return getOneLineMessage(throwable);
}
}
}
Loading