-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #453 from josdem/feature/430
[small]Feature/430
- Loading branch information
Showing
12 changed files
with
218 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,18 @@ | ||
/* | ||
Copyright 2024 Jose Morales [email protected] | ||
Copyright 2024 Jose Morales [email protected] | ||
Licensed 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 | ||
Licensed 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 | ||
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. | ||
*/ | ||
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 com.josdem.vetlog.client; | ||
|
||
|
@@ -21,8 +21,8 @@ | |
import com.google.cloud.storage.BlobId; | ||
import com.google.cloud.storage.BlobInfo; | ||
import com.google.cloud.storage.Storage; | ||
import com.google.cloud.storage.StorageOptions; | ||
import com.josdem.vetlog.exception.BusinessException; | ||
import com.josdem.vetlog.helper.StorageOptionsHelper; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import javax.annotation.PostConstruct; | ||
|
@@ -35,11 +35,13 @@ public class GoogleStorageWriter { | |
|
||
private final CredentialsProvider credentialsProvider; | ||
private final GcpProjectIdProvider gcpProjectIdProvider; | ||
private final StorageOptionsHelper storageOptionsHelper; | ||
private Storage storage; | ||
|
||
@PostConstruct | ||
void setup() throws IOException { | ||
storage = StorageOptions.newBuilder() | ||
storage = storageOptionsHelper | ||
.getStorageOptions() | ||
.setProjectId(gcpProjectIdProvider.getProjectId()) | ||
.setCredentials(credentialsProvider.getCredentials()) | ||
.build() | ||
|
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
28 changes: 28 additions & 0 deletions
28
src/main/java/com/josdem/vetlog/helper/StorageOptionsHelper.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,28 @@ | ||
/* | ||
Copyright 2024 Jose Morales [email protected] | ||
Licensed 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 com.josdem.vetlog.helper; | ||
|
||
import com.google.cloud.storage.StorageOptions; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class StorageOptionsHelper { | ||
|
||
public StorageOptions.Builder getStorageOptions() { | ||
return StorageOptions.newBuilder(); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,29 +1,24 @@ | ||
/* | ||
Copyright 2024 Jose Morales [email protected] | ||
Copyright 2024 Jose Morales [email protected] | ||
Licensed 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 | ||
Licensed 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 | ||
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. | ||
*/ | ||
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 com.josdem.vetlog.util; | ||
|
||
import java.util.UUID; | ||
|
||
public class UuidGenerator { | ||
|
||
private UuidGenerator() { | ||
throw new IllegalStateException("Utility class"); | ||
} | ||
|
||
public static String generateUuid() { | ||
return UUID.randomUUID().toString(); | ||
} | ||
|
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
111 changes: 111 additions & 0 deletions
111
src/test/java/com/josdem/vetlog/client/GoogleStorageWriterTest.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,111 @@ | ||
/* | ||
Copyright 2024 Jose Morales [email protected] | ||
Licensed 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 com.josdem.vetlog.client; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import com.google.api.gax.core.CredentialsProvider; | ||
import com.google.auth.Credentials; | ||
import com.google.cloud.spring.core.GcpProjectIdProvider; | ||
import com.google.cloud.storage.Storage; | ||
import com.google.cloud.storage.StorageOptions; | ||
import com.josdem.vetlog.exception.BusinessException; | ||
import com.josdem.vetlog.helper.StorageOptionsHelper; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.TestInfo; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
@Slf4j | ||
class GoogleStorageWriterTest { | ||
|
||
private GoogleStorageWriter googleStorageWriter; | ||
|
||
@Mock | ||
private CredentialsProvider credentialsProvider; | ||
|
||
@Mock | ||
private GcpProjectIdProvider gcpProjectIdProvider; | ||
|
||
@Mock | ||
private StorageOptionsHelper storageOptionsHelper; | ||
|
||
@Mock | ||
private InputStream inputStream; | ||
|
||
@Mock | ||
private Storage storage; | ||
|
||
@Mock | ||
private Credentials credentials; | ||
|
||
@Mock | ||
private StorageOptions storageOptions; | ||
|
||
@Mock | ||
private StorageOptions.Builder builder; | ||
|
||
@BeforeEach | ||
void setup() { | ||
MockitoAnnotations.openMocks(this); | ||
googleStorageWriter = new GoogleStorageWriter(credentialsProvider, gcpProjectIdProvider, storageOptionsHelper); | ||
} | ||
|
||
@Test | ||
@DisplayName("Should upload to bucket") | ||
void shouldUploadToBucket(TestInfo testInfo) throws Exception { | ||
log.info(testInfo.getDisplayName()); | ||
|
||
setExpectations(); | ||
|
||
googleStorageWriter.setup(); | ||
googleStorageWriter.uploadToBucket("bucket", "fileName", inputStream, "contentType"); | ||
verify(inputStream).readAllBytes(); | ||
} | ||
|
||
@Test | ||
@DisplayName("not upload to bucket due to exception") | ||
void shouldNotUploadToBucket(TestInfo testInfo) throws Exception { | ||
log.info(testInfo.getDisplayName()); | ||
|
||
setExpectations(); | ||
|
||
googleStorageWriter.setup(); | ||
when(inputStream.readAllBytes()).thenThrow(new IllegalStateException("Error")); | ||
assertThrows( | ||
BusinessException.class, | ||
() -> googleStorageWriter.uploadToBucket("bucket", "fileName", inputStream, "contentType")); | ||
} | ||
|
||
private void setExpectations() throws IOException { | ||
when(gcpProjectIdProvider.getProjectId()).thenReturn("projectId"); | ||
when(credentialsProvider.getCredentials()).thenReturn(credentials); | ||
|
||
when(storageOptionsHelper.getStorageOptions()).thenReturn(builder); | ||
when(builder.setProjectId("projectId")).thenReturn(builder); | ||
when(builder.setCredentials(credentials)).thenReturn(builder); | ||
when(builder.build()).thenReturn(storageOptions); | ||
when(builder.build().getService()).thenReturn(storage); | ||
} | ||
} |
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
Oops, something went wrong.