Skip to content

Commit

Permalink
Merge pull request #453 from josdem/feature/430
Browse files Browse the repository at this point in the history
[small]Feature/430
  • Loading branch information
josdem authored Dec 21, 2024
2 parents f4159c0 + 1affc65 commit 7a02594
Show file tree
Hide file tree
Showing 12 changed files with 218 additions and 60 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ ext {
}

group = 'com.josdem.vetlog'
version = '2.1.6'
version = '2.1.7'

configurations {
compileOnly {
Expand Down
28 changes: 15 additions & 13 deletions src/main/java/com/josdem/vetlog/client/GoogleStorageWriter.java
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;

Expand All @@ -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;
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,6 @@ private void initChangeBinder(WebDataBinder binder) {
binder.addValidators(changePasswordValidator);
}

@GetMapping(value = "/activate/{token}")
public String create(@PathVariable String token) {
log.info("Calling activate token");
recoveryService.confirmAccountForToken(token);
return LOGIN_VIEW;
}

@PostMapping(value = "/password")
public ModelAndView generateTokenToChangePassword(
@Valid RecoveryPasswordCommand command, BindingResult bindingResult, HttpServletRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public ModelAndView save(
log.info("Saving adoption for pet: {}", telephoneCommand.getUuid());
if (bindingResult.hasErrors()) {
ModelAndView modelAndView = new ModelAndView("telephone/adopt");
modelAndView.addObject("errorMessage", localeService.getMessage("user.error.mobile", request));
return fillPetAndTelephoneCommand(modelAndView, telephoneCommand);
}
var user = userService.getCurrentUser();
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/com/josdem/vetlog/helper/StorageOptionsHelper.java
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();
}
}
27 changes: 11 additions & 16 deletions src/main/java/com/josdem/vetlog/util/UuidGenerator.java
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();
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/templates/telephone/adopt.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<div class="row">
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<div th:data-testid="errorMessage" th:if="${errorMessage}" class="alert alert-warning" th:text="${errorMessage}"/>
<div th:if="${!pet.images.isEmpty()}">
<img style="width:320px;height:240px;" th:attr="src=@{${gcpImageUrl} + ${pet.images.get(0).uuid}}"/>
</div>
Expand Down
111 changes: 111 additions & 0 deletions src/test/java/com/josdem/vetlog/client/GoogleStorageWriterTest.java
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,23 @@ class LoginControllerTest {
@Test
@DisplayName("getting login page")
void shouldGetLogin(TestInfo testInfo) throws Exception {
log.info("Running: {}", testInfo.getDisplayName());
log.info(testInfo.getDisplayName());
mockMvc.perform(get("/login")).andExpect(status().isOk()).andExpect(view().name("login/login"));
}

@Test
@DisplayName("getting login error")
void shouldGetLoginErrorMessage(TestInfo testInfo) throws Exception {
log.info("Running: {}", testInfo.getDisplayName());
log.info(testInfo.getDisplayName());
mockMvc.perform(get("/login").param("error", "invalid credentials"))
.andExpect(status().isOk())
.andExpect(view().name("login/login"));
}

@Test
@DisplayName("logging out")
void shouldLogout(TestInfo testInfo) throws Exception {
log.info(testInfo.getDisplayName());
mockMvc.perform(get("/logout")).andExpect(status().is3xxRedirection());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void shouldRegisterNewPet(TestInfo testInfo) throws Exception {
@DisplayName("showing edit pet form")
@WithMockUser(username = "josdem", password = "12345678", roles = "USER")
void shouldShowEditPetForm(TestInfo testInfo) throws Exception {
log.info("Running: {}", testInfo.getDisplayName());
log.info(testInfo.getDisplayName());
// Set up data before the test
registerPet(PetStatus.IN_ADOPTION);

Expand All @@ -104,7 +104,7 @@ void shouldShowEditPetForm(TestInfo testInfo) throws Exception {
@DisplayName("updating pet status")
@WithMockUser(username = "josdem", password = "12345678", roles = "USER")
void shouldUpdatePet(TestInfo testInfo) throws Exception {
log.info("Running: {}", testInfo.getDisplayName());
log.info(testInfo.getDisplayName());
// Set up data before the test
registerPet(PetStatus.IN_ADOPTION);

Expand Down Expand Up @@ -135,7 +135,7 @@ void shouldUpdatePet(TestInfo testInfo) throws Exception {
@DisplayName("listing pets")
@WithMockUser(username = "josdem", password = "12345678", roles = "USER")
void shouldListPets(TestInfo testInfo) throws Exception {
log.info("Running: {}", testInfo.getDisplayName());
log.info(testInfo.getDisplayName());
mockMvc.perform(get("/pet/list"))
.andExpect(status().isOk())
.andExpect(model().attributeExists("pets"))
Expand All @@ -147,7 +147,7 @@ void shouldListPets(TestInfo testInfo) throws Exception {
@DisplayName("listing for adoption")
@WithMockUser(username = "josdem", password = "12345678", roles = "USER")
void shouldListForAdoption(TestInfo testInfo) throws Exception {
log.info("Running: {}", testInfo.getDisplayName());
log.info(testInfo.getDisplayName());
mockMvc.perform(get("/pet/listForAdoption"))
.andExpect(status().isOk())
.andExpect(model().attributeExists("pets"))
Expand All @@ -158,7 +158,7 @@ void shouldListForAdoption(TestInfo testInfo) throws Exception {
@DisplayName("giving in adoption")
@WithMockUser(username = "josdem", password = "12345678", roles = "USER")
void shouldGiveForAdoption(TestInfo testInfo) throws Exception {
log.info("Running: {}", testInfo.getDisplayName());
log.info(testInfo.getDisplayName());
mockMvc.perform(get("/pet/giveForAdoption"))
.andExpect(status().isOk())
.andExpect(model().attributeExists("pets"))
Expand All @@ -170,7 +170,7 @@ void shouldGiveForAdoption(TestInfo testInfo) throws Exception {
@DisplayName("showing create pet form")
@WithMockUser(username = "josdem", password = "12345678", roles = "USER")
void shouldShowCreatePetForm(TestInfo testInfo) throws Exception {
log.info("Running: {}", testInfo.getDisplayName());
log.info(testInfo.getDisplayName());
mockMvc.perform(get("/pet/create"))
.andExpect(status().isOk())
.andExpect(model().attributeExists("petCommand"))
Expand All @@ -184,7 +184,7 @@ void shouldShowCreatePetForm(TestInfo testInfo) throws Exception {
@DisplayName("not deleting a pet due to is in adoption")
@WithMockUser(username = "josdem", password = "12345678", roles = "USER")
void shouldShowDeletePetForm(TestInfo testInfo) throws Exception {
log.info("Running: {}", testInfo.getDisplayName());
log.info(testInfo.getDisplayName());

registerPet(PetStatus.IN_ADOPTION);

Expand Down Expand Up @@ -217,7 +217,7 @@ private void registerPet(PetStatus status) throws Exception {
@DisplayName("deleting a pet successfully")
@WithMockUser(username = "josdem", password = "12345678", roles = "USER")
void shouldDeletePet(TestInfo testInfo) throws Exception {
log.info("Running: {}", testInfo.getDisplayName());
log.info(testInfo.getDisplayName());

// Register a pet with OWNED status
registerPet(PetStatus.OWNED);
Expand Down
Loading

0 comments on commit 7a02594

Please sign in to comment.