Skip to content

Commit

Permalink
chang firebase sdk version
Browse files Browse the repository at this point in the history
  • Loading branch information
dfernandos committed Nov 8, 2023
1 parent 6769b7c commit 00009c9
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 17 deletions.
36 changes: 34 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@
<scope>provided</scope>
</dependency>



<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
Expand Down Expand Up @@ -76,6 +74,40 @@
<scope>test</scope>
</dependency>


<dependency>
<groupId>com.google.firebase</groupId>
<artifactId>firebase-admin</artifactId>
<version>9.1.1</version>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.8</version>
</dependency>

<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-core</artifactId>
<version>2.0.9</version> <!-- Adjust the version as needed -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.9</version> <!-- Adjust the version as needed -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.9</version> <!-- Adjust the version as needed -->
<scope>test</scope>
</dependency>



</dependencies>

<build>
Expand Down
59 changes: 57 additions & 2 deletions src/main/java/com/mapeando/territory/TerritoryApplication.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,69 @@
package com.mapeando.territory;

import com.google.auth.oauth2.GoogleCredentials;
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.gson.Gson;
import com.mapeando.territory.config.FirebaseConfig;
import com.mapeando.territory.config.FirebaseInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class TerritoryApplication {
import java.io.ByteArrayInputStream;
import java.io.IOException;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

// ...

@SpringBootApplication
@ComponentScan(basePackages = {"com.mapeando.territory", "com.mapeando.territory.config"})
public class TerritoryApplication implements WebMvcConfigurer {

public static void main(String[] args) {
SpringApplication.run(TerritoryApplication.class, args);

}

@Autowired
private FirebaseConfig firebaseConfig;

@Override
public void addInterceptors(InterceptorRegistry registry){
registry.addInterceptor(new FirebaseInterceptor()).addPathPatterns("territory/create");
registry.addInterceptor(new FirebaseInterceptor()).addPathPatterns("territory/update/{territoryId}");
registry.addInterceptor(new FirebaseInterceptor()).addPathPatterns("territory/delete/{id}");

}

@Bean
public FirebaseOptions firebaseOptions() throws IOException {

byte[] serviceAccountBytes = convertFirebaseConfigToJson(firebaseConfig).getBytes();

ByteArrayInputStream serviceAccountStream = new ByteArrayInputStream(serviceAccountBytes);

return new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(serviceAccountStream))
.build();
}

@Bean
public FirebaseApp firebaseApp(FirebaseOptions firebaseOptions) {
if (FirebaseApp.getApps().isEmpty()) {
return FirebaseApp.initializeApp(firebaseOptions);
}

return FirebaseApp.getInstance();
}

private String convertFirebaseConfigToJson(FirebaseConfig config) {
Gson gson = new Gson();
config.setPrivate_key(config.getPrivate_key());
return gson.toJson(config);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ public class TerritoryController {
@Autowired
TerritoryService territoryService;


@PostMapping("/territory/create")
public ResponseEntity<?> createTerritory(@RequestParam("file") MultipartFile file,
public ResponseEntity<?> createTerritory(@RequestHeader("Authorization") String idToken, @RequestParam("file") MultipartFile file,
@ModelAttribute Territory territory) {
try {
// Trate a imagem (converta para Base64 ou salve em disco, se preferir)

// String idTokenParsed = idToken.substring(7);
// FirebaseToken decodedToken = firebaseAuthentication.validateFirebaseToken(idTokenParsed);

byte[] imageData = file.getBytes();
territory.setMainImage(imageData);

// Salve o restante dos dados do território
Territory savedTerritory = territoryService.createTerritory(territory);
return new ResponseEntity<>(savedTerritory, HttpStatus.CREATED);
} catch (IOException e) {
Expand All @@ -41,7 +44,7 @@ public ResponseEntity<?> createTerritory(@RequestParam("file") MultipartFile fil
}

@GetMapping("/territory/{id}")
public ResponseEntity<?> getTerritoryById(@PathVariable String id){
public ResponseEntity<?> getTerritoryById(@PathVariable String id){
try{

Optional<Territory> territoryDB = territoryService.getTerritoryById(id);
Expand All @@ -66,6 +69,7 @@ public List<Coordinates> getCoordinates(){
@PutMapping(value = "/territory/update/{territoryId}", consumes = "multipart/form-data")
public ResponseEntity<?> updateTerritory(
@PathVariable String territoryId,
@RequestHeader("Authorization") String idToken,
@ModelAttribute Territory territory,
@RequestParam(value = "file", required = false) MultipartFile file) {

Expand Down Expand Up @@ -114,15 +118,13 @@ public ResponseEntity<?> handleOptionsRequest() {
return ResponseEntity.ok().build();
}

@DeleteMapping("/territory/{id}")
public HttpStatus deleteTerritoryById(@PathVariable String id){
@DeleteMapping("/territory/delete/{id}")
public HttpStatus deleteTerritoryById(@PathVariable String id, @RequestHeader("Authorization") String idToken){

try{
return territoryService.deleteTerritory(id);
}catch (Exception ex){
return HttpStatus.INTERNAL_SERVER_ERROR;
}

}

}
12 changes: 12 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,14 @@
spring.data.mongodb.uri=${MONGODB_URI}

# Firebase Configuration
firebase.type=${FIREBASE_TYPE}
firebase.project_id=${FIREBASE_PROJECT_ID}
firebase.private_key_id=${FIREBASE_PRIVATE_KEY_ID}
firebase.private_key=${FIREBASE_PRIVATE_KEY}
firebase.client_email=${FIREBASE_CLIENT_EMAIL}
firebase.client_id=${FIREBASE_CLIENT_ID}
firebase.auth_uri=${FIREBASE_AUTH_URI}
firebase.token_uri=${FIREBASE_TOKEN_URI}
firebase.auth_provider_x509_cert_url=${FIREBASE_AUTH_PROVIDER_X509_CERT_URL}
firebase.client_x509_cert_url=${FIREBASE_CLIENT_X509_CERT_URL}
firebase.universe_domain=${FIREBASE_UNIVERSE_DOMAIN}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ void shouldAddATerritoryAndReturn201() throws Exception {

ResultActions result = mockMvc.perform(fileUpload("/api/territory-svc/territory/create")
.file(file)
.param("name", objectMapper.writeValueAsString(territory)))
.param("name", objectMapper.writeValueAsString(territory))
.header("Authorization", "Bearer myToken"))
.andExpect(status().isCreated());
}

Expand All @@ -86,7 +87,8 @@ void shouldNotAddATerritoryAndReturn400Error() throws Exception {

ResultActions result = mockMvc.perform(fileUpload("/api/territory-svc/territory/create")
.file(file)
.param("name", objectMapper.writeValueAsString(territory)))
.param("name", objectMapper.writeValueAsString(territory))
.header("Authorization", "Bearer myToken"))
.andExpect(status().is4xxClientError());
}

Expand All @@ -98,7 +100,8 @@ void getAllTerritories() throws Exception {

@Test
void deleteTerritory() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.delete("/api/territory-svc/territory/{id}", "1"))
mockMvc.perform(MockMvcRequestBuilders.delete("/api/territory-svc/territory/delete/{id}", "1")
.header("Authorization", "Bearer myToken"))
.andExpect(status().isOk());
}

Expand Down Expand Up @@ -128,7 +131,8 @@ void testUpdateTerritory() throws Exception {

ResultActions result = mockMvc.perform(put("/api/territory-svc/territory/update/{id}", "1")
.contentType(MediaType.MULTIPART_FORM_DATA)
.content(objectMapper.writeValueAsString(territory)))
.content(objectMapper.writeValueAsString(territory))
.header("Authorization", "Bearer myToken"))
.andExpect(status().isCreated());
}

Expand All @@ -146,4 +150,4 @@ void getAllTerritoriesCoordinates() throws Exception {
}


}
}

0 comments on commit 00009c9

Please sign in to comment.