forked from apache/hadoop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
HDDS-1482. Use strongly typed codec implementations for the S3Table
Closes apache#789
- Loading branch information
1 parent
e2f0f72
commit 4605db3
Showing
7 changed files
with
191 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/om/codec/S3SecretValueCodec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* 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 | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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.ozone.om.codec; | ||
|
||
|
||
import java.io.IOException; | ||
|
||
import com.google.common.base.Preconditions; | ||
import com.google.protobuf.InvalidProtocolBufferException; | ||
|
||
import org.apache.hadoop.ozone.om.helpers.S3SecretValue; | ||
import org.apache.hadoop.ozone.protocol.proto.OzoneManagerProtocolProtos; | ||
import org.apache.hadoop.utils.db.Codec; | ||
|
||
|
||
/** | ||
* Codec to encode S3SecretValue as byte array. | ||
*/ | ||
public class S3SecretValueCodec implements Codec<S3SecretValue> { | ||
|
||
@Override | ||
public byte[] toPersistedFormat(S3SecretValue object) throws IOException { | ||
Preconditions | ||
.checkNotNull(object, "Null object can't be converted to byte array."); | ||
return object.getProtobuf().toByteArray(); | ||
} | ||
|
||
@Override | ||
public S3SecretValue fromPersistedFormat(byte[] rawData) throws IOException { | ||
Preconditions | ||
.checkNotNull(rawData, | ||
"Null byte array can't converted to real object."); | ||
try { | ||
return S3SecretValue.fromProtobuf( | ||
OzoneManagerProtocolProtos.S3Secret.parseFrom(rawData)); | ||
} catch (InvalidProtocolBufferException e) { | ||
throw new IllegalArgumentException( | ||
"Can't encode the the raw data from the byte array", e); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
...p-ozone/common/src/test/java/org/apache/hadoop/ozone/om/codec/TestS3SecretValueCodec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/** | ||
* 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 | ||
* <p> | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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.ozone.om.codec; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.UUID; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
|
||
import org.apache.hadoop.ozone.om.helpers.S3SecretValue; | ||
import org.apache.hadoop.test.GenericTestUtils; | ||
|
||
import static org.junit.Assert.fail; | ||
|
||
/** | ||
* This class test S3SecretValueCodec. | ||
*/ | ||
public class TestS3SecretValueCodec { | ||
|
||
@Rule | ||
public ExpectedException thrown = ExpectedException.none(); | ||
|
||
private S3SecretValueCodec codec; | ||
|
||
@Before | ||
public void initialize() { | ||
codec = new S3SecretValueCodec(); | ||
} | ||
@Test | ||
public void testCodecWithCorrectData() throws Exception { | ||
|
||
S3SecretValue s3SecretValue = | ||
new S3SecretValue(UUID.randomUUID().toString(), | ||
UUID.randomUUID().toString()); | ||
|
||
byte[] data = codec.toPersistedFormat(s3SecretValue); | ||
Assert.assertNotNull(data); | ||
|
||
S3SecretValue docdedS3Secret = codec.fromPersistedFormat(data); | ||
|
||
Assert.assertEquals(s3SecretValue, docdedS3Secret); | ||
|
||
} | ||
|
||
@Test | ||
public void testCodecWithIncorrectValues() throws Exception { | ||
try { | ||
codec.fromPersistedFormat("random".getBytes(StandardCharsets.UTF_8)); | ||
fail("testCodecWithIncorrectValues failed"); | ||
} catch (IllegalArgumentException ex) { | ||
GenericTestUtils.assertExceptionContains("Can't encode the the raw " + | ||
"data from the byte array", ex); | ||
} | ||
} | ||
|
||
@Test | ||
public void testCodecWithNullDataFromTable() throws Exception { | ||
thrown.expect(NullPointerException.class); | ||
codec.fromPersistedFormat(null); | ||
} | ||
|
||
|
||
@Test | ||
public void testCodecWithNullDataFromUser() throws Exception { | ||
thrown.expect(NullPointerException.class); | ||
codec.toPersistedFormat(null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters