Skip to content

Commit

Permalink
Adapted method to get supply data from Datadis get data from supplies…
Browse files Browse the repository at this point in the history
… from owner and from authorized supplies
  • Loading branch information
viktorKhan committed Jun 16, 2024
1 parent c0eecc9 commit cf3bc18
Showing 3 changed files with 74 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
import org.lucoenergia.conluz.domain.admin.supply.DatadisSupply;
import org.lucoenergia.conluz.domain.admin.supply.get.GetSupplyRepositoryDatadis;
import org.lucoenergia.conluz.domain.admin.user.User;
import org.lucoenergia.conluz.domain.shared.UserPersonalId;
import org.lucoenergia.conluz.infrastructure.consumption.datadis.DatadisAuthorizer;
import org.lucoenergia.conluz.infrastructure.consumption.datadis.config.DatadisConfigEntity;
import org.lucoenergia.conluz.infrastructure.consumption.datadis.DatadisParams;
@@ -54,10 +55,11 @@ public List<DatadisSupply> getSuppliesByUser(User user) {
LOGGER.info("Getting all supplies from datadis.es of user {}", user.getId());

// Create the complete URL with the query parameter
final String url = UriComponentsBuilder.fromUriString(DatadisConfigEntity.BASE_URL + GET_SUPPLIES_PATH)
.queryParam(DatadisParams.AUTHORIZED_NIF, user.getPersonalId())
.build()
.toUriString();
UriComponentsBuilder urlBuilder = UriComponentsBuilder.fromUriString(DatadisConfigEntity.BASE_URL + GET_SUPPLIES_PATH);
if (datadisAuthorizer.isAuthorizedNif(UserPersonalId.of(user.getPersonalId()))) {
urlBuilder.queryParam(DatadisParams.AUTHORIZED_NIF, user.getPersonalId());
}
String url = urlBuilder.build().toUriString();

final Request request = new Request.Builder()
.url(url)
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@

import okhttp3.*;
import org.lucoenergia.conluz.domain.consumption.datadis.DatadisException;
import org.lucoenergia.conluz.domain.shared.UserPersonalId;
import org.lucoenergia.conluz.infrastructure.consumption.datadis.config.DatadisConfigEntity;
import org.lucoenergia.conluz.infrastructure.shared.security.auth.Authorizer;
import org.lucoenergia.conluz.infrastructure.shared.web.rest.ConluzRestClientBuilder;
@@ -63,4 +64,17 @@ public String getAuthToken() {
throw new DatadisException("Unable to make the request to datadis.es", e);
}
}

public boolean isOwner(UserPersonalId id) {
Optional<DatadisConfigEntity> optionalConfig = datadisConfigRepository.findFirstByOrderByIdAsc();
if (optionalConfig.isEmpty()) {
throw new DatadisException("Datadis configuration not found");
}
DatadisConfigEntity config = optionalConfig.get();
return config.getUsername().equals(id.getPersonalId());
}

public boolean isAuthorizedNif(UserPersonalId id) {
return !isOwner(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.lucoenergia.conluz.infrastructure.admin.supply.get;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.lucoenergia.conluz.domain.admin.supply.DatadisSupply;
import org.lucoenergia.conluz.domain.admin.user.User;
import org.lucoenergia.conluz.infrastructure.consumption.datadis.DatadisConfigRepository;
import org.lucoenergia.conluz.infrastructure.consumption.datadis.config.DatadisConfigEntity;
import org.lucoenergia.conluz.infrastructure.shared.BaseIntegrationTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertEquals;

@Disabled("These tests should not be included in a CI pipeline because connects with datadis.es, so, needs real data.")
@Transactional
class GetSupplyRepositoryDatadisRestIntegrationTest extends BaseIntegrationTest {

@Autowired
private GetSupplyRepositoryDatadisRest getSupplyRepositoryDatadisRest;
@Autowired
private DatadisConfigRepository datadisConfigRepository;

@Test
void testGetSuppliesByUser() {

// Set username and password you want to user for testing here
final String username = "username";
final String password = "password";

// Save Datadis config on the DB
final DatadisConfigEntity config = new DatadisConfigEntity();
config.setId(UUID.randomUUID());
config.setUsername(username);
config.setPassword(password);
datadisConfigRepository.save(config);

User user = new User.Builder()
.id(UUID.randomUUID())
.personalId("personalId")
.number(1)
.fullName("John Doe")
.email("username@email.com")
.enabled(true)
.build();

List<DatadisSupply> suppliesByUser = getSupplyRepositoryDatadisRest.getSuppliesByUser(user);

assertEquals(1, suppliesByUser.size());
}
}

0 comments on commit cf3bc18

Please sign in to comment.