Skip to content

Commit

Permalink
Merge pull request #173 from catenax-ng/release/v2.1.0-unit-tests
Browse files Browse the repository at this point in the history
[ 2º ] - Release/v2.1.0 unit tests: Added more detailed unit tests to the application
  • Loading branch information
saudkhan116 authored Feb 2, 2024
2 parents fc0543a + 7cbfb74 commit f65f6c5
Show file tree
Hide file tree
Showing 43 changed files with 3,718 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public Response create(@Valid @RequestBody DiscoverySearch searchBody) {
}
List<String> bpnList = new ArrayList<>();
for(BpnDiscovery bpnDiscovery : bpnDiscoveries){
bpnList.addAll(bpnDiscovery.getBpnNumbers());
bpnList.addAll(bpnDiscovery.mapBpnNumbers());
}
if(bpnList.size() == 0){
response.message = "The asset was not found in the BPN Discovery!";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,6 @@ public Process createProcess(String processId,Boolean childrenCondition, HttpSer
* @throws ManagerException
* if unable to create the status file.
*/

public String newStatusFile(String processId, String connectorAddress, Long created, Boolean childrenCondition){
try {
String path = this.getProcessFilePath(processId, this.metaFileName);
Expand Down Expand Up @@ -647,6 +646,7 @@ public String setBpn(String processId, String bpn) {
throw new ManagerException(this.getClass().getName(), e, "It was not possible to create/update the status file");
}
}

/**
* Set the children condition in the status file
* <p>
Expand All @@ -660,6 +660,7 @@ public String setBpn(String processId, String bpn) {
* @throws ManagerException
* if unable to update the status file.
*/
@SuppressWarnings("Unused")
public String setChildrenCondition(String processId, Boolean childrenCondition) {
try {
String path = this.getProcessFilePath(processId, this.metaFileName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@

package org.eclipse.tractusx.digitalproductpass.models.catenax;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;

import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -61,7 +65,8 @@ public void setBpns(List<BpnEndpoint> bpns) {
}

/** METHODS **/
public List<String> getBpnNumbers(){

public List<String> mapBpnNumbers(){
return bpns.stream().map(
BpnEndpoint::getValue
).collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public void setUpdated(Long updated) {
public Dtr getDtr(String id) {
return this.dtrs.get(id);
}

/**
* Gets a new DTR into the DTR map object.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public class AasService extends BaseService {
private DtrSearchManager dtrSearchManager;
private final ProcessManager processManager;
private DataTransferService dataService;

private PassportConfig passportConfig;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,7 @@ public class NegotiateContract implements Runnable {
private String processId;

/** CONSTRUCTOR(S) **/
public NegotiateContract() {};
public NegotiateContract(ProcessDataModel dataModel, String processId, String bpn, Dataset dataset, Status status) {
this.dataModel = dataModel;
this.processId = processId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,7 @@ public class IrsService extends BaseService {

/** ATTRIBUTES **/
HttpUtil httpUtil;

JsonUtil jsonUtil;

String irsEndpoint;
String irsJobPath;
String callbackUrl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ public URI buildUri(String url, Map<String, ?> params, Boolean encoded){
* the base URL.
* @param params
* the Map with key/value pair from each parameter.
* @param encode
* @param encoded
* if true will encode the value of each parameter.
*
* @return a {@code String} with the build URL.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

package utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.json.JsonWriteFeature;
import com.fasterxml.jackson.core.type.TypeReference;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
package managers;

import org.eclipse.tractusx.digitalproductpass.config.DtrConfig;
import org.eclipse.tractusx.digitalproductpass.config.ProcessConfig;
import org.eclipse.tractusx.digitalproductpass.managers.DtrSearchManager;
import org.eclipse.tractusx.digitalproductpass.managers.ProcessManager;
import org.eclipse.tractusx.digitalproductpass.models.catenax.Dtr;
import org.eclipse.tractusx.digitalproductpass.services.DataTransferService;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.core.env.Environment;
import utils.DateTimeUtil;
import utils.FileUtil;
import utils.HttpUtil;
import utils.JsonUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.when;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class DtrSearchManagerTest {

private DtrSearchManager dtrSearchManager;
private final String testAssetPath = "/dpp/assets/TestAsset.json";
@Mock
private ProcessManager processManager;
@Mock
private DataTransferService dataTransferService;
@Mock
private DtrConfig dtrConfig;
@Mock
Environment env;
@Mock
private FileUtil fileUtil;
@Mock
private JsonUtil jsonUtil;

@BeforeAll
void setUpAll() {
fileUtil = new FileUtil();
jsonUtil = new JsonUtil(fileUtil);
dtrConfig = initDtrConfig();

env = Mockito.mock(Environment.class);
HttpUtil httpUtil = new HttpUtil(env);
dataTransferService = Mockito.mock(DataTransferService.class);
ProcessConfig processConfig = new ProcessConfig();
processConfig.setDir("process");
processManager = new ProcessManager(httpUtil, jsonUtil, fileUtil, processConfig);
dtrSearchManager = new DtrSearchManager(fileUtil, jsonUtil, dataTransferService, dtrConfig, processManager);

fileUtil.deleteFile(dtrSearchManager.getDataModelPath());
dtrSearchManager.loadDataModel();

when(dtrConfig.getTemporaryStorage().getEnabled()).thenReturn(true);
}

private DtrConfig initDtrConfig() {
DtrConfig dtrConfig = new DtrConfig();
DtrConfig.Timeouts timeouts = new DtrConfig.Timeouts();
timeouts.setSearch(10);
timeouts.setDtrRequestProcess(40);
timeouts.setNegotiation(10);
timeouts.setTransfer(10);
timeouts.setDigitalTwin(20);
dtrConfig.setTimeouts(timeouts);
dtrConfig.setTemporaryStorage(new DtrConfig.TemporaryStorage(true, 12));
return dtrConfig;
}

@Test
void createDataModelFile() {
Map<String, List<Dtr>> initialDtrDataModel = dtrSearchManager.getDtrDataModel();

assertTrue(initialDtrDataModel.isEmpty());

String dtrDataModelPath = dtrSearchManager.createDataModelFile();
Map<String, List<Dtr>> newDtrDataModel = dtrSearchManager.getDtrDataModel();

assertNotNull(dtrDataModelPath);
assertNotNull(newDtrDataModel);
assertTrue(newDtrDataModel.isEmpty());
}

@Test
void addConnectionToBpnEntry() {
Map<String, List<Dtr>> initialDtrDataModel = dtrSearchManager.getDtrDataModel();

assertTrue(initialDtrDataModel.isEmpty());

String bpn = "BPN001";
String contractId = UUID.randomUUID().toString();
String assetId = UUID.randomUUID().toString();
String endpoint = "dtr.endpoint";
Long validUntil = DateTimeUtil.getTimestamp();
Dtr newDtr = new Dtr(contractId, endpoint, assetId, bpn, validUntil);

dtrSearchManager.addConnectionToBpnEntry(bpn, newDtr);

ConcurrentHashMap<String, List<Dtr>> updatedDtrDataModel = dtrSearchManager.getDtrDataModel();
Dtr updatedDtr = updatedDtrDataModel.get(bpn).get(0);

assertEquals(1, updatedDtrDataModel.get(bpn).size());
assertEquals(newDtr, updatedDtr);
assertEquals(bpn, updatedDtr.getBpn());
assertEquals(contractId, updatedDtr.getContractId());
assertEquals(assetId, updatedDtr.getAssetId());

dtrSearchManager.deleteBpns(dtrSearchManager.getDtrDataModel(), List.of(bpn));
dtrSearchManager.saveDtrDataModel();
}

@Test
void deleteBpns() {
int maxDtrs = 4;
int count = 0;
List<String> bpnsList = new ArrayList<>();
while (count < maxDtrs) {
String bpn = UUID.randomUUID().toString();
String endpoint = UUID.randomUUID().toString();
Dtr dtr = new Dtr();
dtr.setBpn(bpn);
dtr.setEndpoint(endpoint);
bpnsList.add(bpn);
dtrSearchManager.addConnectionToBpnEntry(bpn, dtr);
++count;
}

ConcurrentHashMap<String, List<Dtr>> dtrDataModel = dtrSearchManager.getDtrDataModel();

assertEquals(4, dtrDataModel.size());

dtrSearchManager.deleteBpns(dtrDataModel, bpnsList);

assertEquals(0 , dtrDataModel.size());
}

@Test
void saveAndLoadDataModel() {
ConcurrentHashMap<String, List<Dtr>> dtrDataModel = dtrSearchManager.getDtrDataModel();

assertTrue(dtrDataModel.isEmpty());

String bpn1 = "BPN001";
String contractId1 = UUID.randomUUID().toString();
String assetId1 = UUID.randomUUID().toString();
String endpoint1 = "dtr.endpoint1";
Long validUntil1 = DateTimeUtil.getTimestamp();
Dtr dtr1 = new Dtr(contractId1, endpoint1, assetId1, bpn1, validUntil1);
String bpn2 = "BPN002";
String contractId2 = UUID.randomUUID().toString();
String assetId2 = UUID.randomUUID().toString();
String endpoint2 = "dtr.endpoint2";
Long validUntil2 = DateTimeUtil.getTimestamp();
Dtr dtr2 = new Dtr(contractId2, endpoint2, assetId2, bpn2, validUntil2);
String contractId3 = UUID.randomUUID().toString();
String assetId3 = UUID.randomUUID().toString();
String endpoint3 = "dtr.endpoint3";
Long validUntil3 = DateTimeUtil.getTimestamp();
Dtr dtr3 = new Dtr(contractId3, endpoint3, assetId3, bpn2, validUntil3);

dtrSearchManager.addConnectionToBpnEntry(bpn1, dtr1);
dtrSearchManager.addConnectionToBpnEntry(bpn2, dtr2);
dtrSearchManager.addConnectionToBpnEntry(bpn2, dtr3);
dtrSearchManager.saveDtrDataModel(dtrDataModel);
dtrDataModel = dtrSearchManager.loadDataModel();

assertFalse(dtrDataModel.isEmpty());
assertEquals(2, dtrDataModel.size());
assertEquals(1, dtrDataModel.get(bpn1).size());
assertEquals(2, dtrDataModel.get(bpn2).size());

dtrSearchManager.deleteBpns(dtrSearchManager.getDtrDataModel(), List.of(bpn1, bpn2));
dtrSearchManager.saveDtrDataModel();
}
}
Loading

0 comments on commit f65f6c5

Please sign in to comment.