Skip to content

Commit

Permalink
feat: template import find product names from assignemnts (#59)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejpetras authored May 24, 2024
1 parent 24ca7d4 commit 0dea8f3
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void importData(DataImportConfig config) {

TemplateImportDTO data = objectMapper.readValue(config.getData(), TemplateImportDTO.class);

var existingData = importProducts(data.getProducts());
var existingData = importProducts(data);

List<String> tenants = templateConfig.tenants();
importRoles(tenants, data.getRoles(), existingData);
Expand All @@ -58,9 +58,17 @@ public void importData(DataImportConfig config) {
}
}

private TemplateCommonData importProducts(Map<String, TemplateProductValueDTO> products) {
private TemplateCommonData importProducts(TemplateImportDTO dto) {

var data = service.getCommonData(products.keySet());
Map<String, TemplateProductValueDTO> products = dto.getProducts();
Set<String> productNames = new HashSet<>(products.keySet());

dto.getRoles().forEach((k, v) -> {
if (v.getAssignments() != null) {
productNames.addAll(v.getAssignments().keySet());
}
});
var data = service.getCommonData(productNames);

// create or update apps
var applications = new ArrayList<Application>();
Expand All @@ -71,7 +79,9 @@ private TemplateCommonData importProducts(Map<String, TemplateProductValueDTO> p
// loop over products and create applications and permissions create/update request
for (Map.Entry<String, TemplateProductValueDTO> item : products.entrySet()) {
String productName = item.getKey();

if (item.getValue() == null) {
continue;
}
for (Map.Entry<String, TemplateApplicationValueDTO> entry : item.getValue().getApplications().entrySet()) {
var appId = entry.getKey();
var app = entry.getValue();
Expand All @@ -97,8 +107,12 @@ private TemplateCommonData importProducts(Map<String, TemplateProductValueDTO> p
private void importRoles(List<String> tenants, Map<String, TemplateRoleValueDTO> dto, TemplateCommonData commonData) {

var roleNames = dto.keySet();
var productNames = dto.values().stream()
.map(x -> x.getAssignments().keySet()).collect(HashSet::new, Set<String>::addAll, Set<String>::addAll);
Set<String> productNames = new HashSet<>();
dto.forEach((k, v) -> {
if (v.getAssignments() != null) {
productNames.addAll(v.getAssignments().keySet());
}
});

for (String tenant : tenants) {

Expand All @@ -114,7 +128,6 @@ private void importRoles(List<String> tenants, Map<String, TemplateRoleValueDTO>
dr.getValue().getDescription());
roles.add(role);
}

assignments.addAll(createAssignments(role, dr.getValue(), data, commonData));
}

Expand All @@ -126,6 +139,9 @@ private void importRoles(List<String> tenants, Map<String, TemplateRoleValueDTO>
private List<Assignment> createAssignments(Role role, TemplateRoleValueDTO dto, TemplateTenantData data,
TemplateCommonData commonData) {
List<Assignment> assignments = new ArrayList<>();
if (dto.getAssignments() == null) {
return assignments;
}
for (var aProduct : dto.getAssignments().entrySet()) {
for (var aApp : aProduct.getValue().entrySet()) {
for (var aResource : aApp.getValue().entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ public byte[] getData() {
try {
return mapper.writeValueAsBytes(request);
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
Expand All @@ -112,4 +111,39 @@ public byte[] getData() {
assertThat(data).isNotNull().hasSize(8);

}

@Test
void importDataExistSpecialCaseTest() {

TemplateImportDTO request = new TemplateImportDTO()
.putRolesItem("n_x", new TemplateRoleValueDTO().assignments(null))
.putRolesItem("n1", new TemplateRoleValueDTO().description("d1")
.putAssignmentsItem("test1", Map.of("app1", Map.of("o1", List.of("a1", "a2", "x1")))))
.putProductsItem("test_a", null)
.putProductsItem("test_x", new TemplateProductValueDTO()
.putApplicationsItem("app_x", new TemplateApplicationValueDTO()
.putPermissionsItem("o1", Map.of("a1", "d1", "x1", "xx1"))));

DataImportConfig config = new DataImportConfig() {
@Override
public Map<String, String> getMetadata() {
return Map.of();
}

@Override
public byte[] getData() {
try {
return mapper.writeValueAsBytes(request);
} catch (Exception ex) {
return null;
}
}
};

service.importData(config);

List<Permission> data = permissionDAO.findAll().toList();
assertThat(data).isNotNull().hasSize(10);

}
}

0 comments on commit 0dea8f3

Please sign in to comment.