Skip to content

Commit

Permalink
TACKLE-58: tag type and business service controls endpoint integration
Browse files Browse the repository at this point in the history
  • Loading branch information
m-brophy committed May 26, 2021
1 parent 18835a9 commit d7af407
Show file tree
Hide file tree
Showing 9 changed files with 234 additions and 17 deletions.
16 changes: 16 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-oidc</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-rest-client-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-keycloak-authorization</artifactId>
Expand Down Expand Up @@ -212,6 +220,14 @@
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>10</source>
<target>10</target>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.tackle.applicationinventory;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

@JsonIgnoreProperties(ignoreUnknown = true)
public class BusinessService {
public String id;
public String name;
}
17 changes: 17 additions & 0 deletions src/main/java/io/tackle/applicationinventory/TagType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.tackle.applicationinventory;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import java.util.List;

@JsonIgnoreProperties(ignoreUnknown = true)
public class TagType {
public String id;
public String name;
public List<Tag> tags;

public static class Tag {
public String id;
public String name;
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,83 @@
package io.tackle.applicationinventory.mapper;

import io.tackle.applicationinventory.BusinessService;
import io.tackle.applicationinventory.TagType;
import io.tackle.applicationinventory.entities.Application;
import io.tackle.applicationinventory.entities.ApplicationImport;
import org.eclipse.microprofile.config.inject.ConfigProperty;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.RequestScoped;
import javax.ws.rs.GET;
import javax.ws.rs.core.Response;
import java.util.*;


public class ApplicationInventoryAPIMapper extends ApplicationMapper{

public ApplicationInventoryAPIMapper( Set<TagType> tagTypes, Set<BusinessService> businessServices) {
super(tagTypes, businessServices);
}

@Override
public Response map(ApplicationImport importApp)
{
System.out.println("Call to Mapper");

Application newApp = new Application();
Set<String> tags = new HashSet<>();
try {
newApp.businessService = addBusinessService(importApp.getBusinessService());
newApp.comments = importApp.getComments();
newApp.description = importApp.getDescription();
newApp.name = importApp.getApplicationName();
if (importApp.getTagType1() != null && importApp.getTag1() != null) {
tags.add(addTag(importApp.getTag1(), importApp.getTagType1()));
}
if (importApp.getTagType2() != null && importApp.getTag2() != null) {
tags.add(addTag(importApp.getTag2(), importApp.getTagType2()));
}
if (importApp.getTagType3() != null && importApp.getTag3() != null) {
tags.add(addTag(importApp.getTag3(), importApp.getTagType3()));
}
if (importApp.getTagType4() != null && importApp.getTag4() != null) {
tags.add(addTag(importApp.getTag4(), importApp.getTagType4()));
}
}
catch(NoSuchElementException nsee)
{
nsee.printStackTrace();
return Response.serverError().build();
}

newApp.tags = tags;
newApp.persistAndFlush();
return Response.ok().build();
}


private String addBusinessService(String businessServiceName) throws NoSuchElementException
{
Optional<BusinessService> businessServiceOptional = businessServices.stream().filter(businessServiceControls -> businessServiceControls.name.equals(businessServiceName))
.findFirst();


return businessServiceOptional.orElseThrow().id;

}


private String addTag(String tagName, String tagTypeName) throws NoSuchElementException
{
Optional<TagType> tagTypeOptional = tagTypes.stream().filter(tagTypeControls -> tagTypeControls.name.equals(tagTypeName))
.findFirst();
if(!tagTypeOptional.isPresent())
{
return null;
}
Optional<TagType.Tag> tagOptional = tagTypeOptional.get().tags.stream().filter(tagControls -> tagControls.name.equals(tagName))
.findFirst();

return tagOptional.orElseThrow().id;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
package io.tackle.applicationinventory.mapper;

import io.tackle.applicationinventory.BusinessService;
import io.tackle.applicationinventory.TagType;
import io.tackle.applicationinventory.entities.ApplicationImport;

import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.core.Response;
import java.util.Set;

public abstract class ApplicationMapper {
@ApplicationScoped
Set<TagType> tagTypes;
@ApplicationScoped
Set<BusinessService> businessServices;

public ApplicationMapper(Set<TagType> tagTypes, Set<BusinessService> businessServices)
{
this.tagTypes = tagTypes;
this.businessServices = businessServices;
}

public abstract Response map(ApplicationImport importApp);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.tackle.applicationinventory.service;

import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

import io.tackle.applicationinventory.BusinessService;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

import java.util.Set;


@Path("/controls/business-service")
@RegisterRestClient
@ApplicationScoped
public interface BusinessServiceService {

@GET
@Path("?page=0&size=1000&sort=name")
Set<BusinessService> getListOfBusinessServices();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,18 @@
import com.fasterxml.jackson.dataformat.csv.CsvMapper;
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
import io.tackle.applicationimporter.MultipartImportBody;
import io.tackle.applicationinventory.BusinessService;
import io.tackle.applicationinventory.TagType;
import io.tackle.applicationinventory.entities.ApplicationImport;
import io.tackle.applicationinventory.exceptions.ApplicationsInventoryException;
import io.tackle.applicationinventory.mapper.ApplicationInventoryAPIMapper;
import io.tackle.applicationinventory.mapper.ApplicationMapper;
import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.jboss.resteasy.annotations.providers.multipart.MultipartForm;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.transaction.Transactional;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
Expand All @@ -22,13 +28,22 @@
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import static javax.transaction.Transactional.TxType.REQUIRED;

@Path("/file")
@ApplicationScoped
public class ImportService {

@Inject
@RestClient
TagTypeService tagTypeService;

@Inject
@RestClient
BusinessServiceService businessServiceService;

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
Expand All @@ -38,8 +53,20 @@ public Response importFile(@MultipartForm MultipartImportBody data) throws Excep

System.out.println("File: " + data.getFile());
System.out.println("FileName: " + data.getFileName());

Set<TagType> tagTypes =tagTypeService.getListOfTagTypes();
if (tagTypes == null)
{
throw new Exception("Unable to connect to remote resource to retrieve TagTypes");
}
Set<BusinessService> businessServices =businessServiceService.getListOfBusinessServices();
if (businessServices == null)
{
throw new Exception("Unable to connect to remote resource to retrieve BusinessServices");
}

List<ApplicationImport> importList = writeFile(data.getFile(), data.getFileName());
mapImportsToApplication(importList);
mapImportsToApplication(importList, tagTypes, businessServices);
} catch (Exception e) {

e.printStackTrace();
Expand Down Expand Up @@ -105,9 +132,9 @@ private String getFilePortionOfMessage(String content)
}
}

public void mapImportsToApplication(List<ApplicationImport> importList)
public void mapImportsToApplication(List<ApplicationImport> importList, Set<TagType> tagTypes, Set<BusinessService> businessServices)
{
ApplicationMapper mapper = new ApplicationInventoryAPIMapper();
ApplicationMapper mapper = new ApplicationInventoryAPIMapper(tagTypes, businessServices);
importList.forEach(importedApplication -> {
System.out.println("Mapping :" + importedApplication.id);
Response response = mapper.map(importedApplication);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.tackle.applicationinventory.service;

import javax.enterprise.context.ApplicationScoped;
import javax.ws.rs.GET;
import javax.ws.rs.Path;

import io.tackle.applicationinventory.TagType;
import org.eclipse.microprofile.rest.client.inject.RegisterRestClient;

import java.util.Set;


@Path("/controls/tag-type")
@RegisterRestClient
@ApplicationScoped
public interface TagTypeService {

@GET
@Path("?page=0&size=1000&sort=rank")
Set<TagType> getListOfTagTypes();
}


51 changes: 37 additions & 14 deletions src/test/java/io/tackle/applicationimporter/ImportServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,25 @@
import io.quarkus.test.common.ResourceArg;
import io.quarkus.test.junit.QuarkusMock;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.mockito.InjectMock;
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
import io.restassured.config.EncoderConfig;
import io.tackle.applicationinventory.BusinessService;
import io.tackle.applicationinventory.TagType;
import io.tackle.applicationinventory.entities.ApplicationImport;
import io.tackle.applicationinventory.mapper.ApplicationInventoryAPIMapper;
import io.tackle.applicationinventory.service.BusinessServiceService;
import io.tackle.applicationinventory.service.ImportService;
import io.tackle.applicationimporter.MultipartImportBody;
import io.tackle.applicationinventory.service.TagTypeService;
import io.tackle.commons.testcontainers.KeycloakTestResource;
import io.tackle.commons.testcontainers.PostgreSQLDatabaseTestResource;
import io.tackle.commons.tests.SecuredResourceTest;
import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mockito;

import javax.enterprise.context.ApplicationScoped;
Expand All @@ -29,9 +33,7 @@

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.*;

import static io.restassured.RestAssured.given;
import static javax.transaction.Transactional.TxType.REQUIRED;
Expand All @@ -54,16 +56,16 @@
)
public class ImportServiceTest extends SecuredResourceTest {

//@InjectMocks
//@ApplicationScoped
//ApplicationInventoryAPIMapper apiMapper;
@InjectMock
TagTypeService mockTagTypeService;

@InjectMock
BusinessServiceService mockBusinessServiceService;


@BeforeAll
public static void init() {
PATH = "/file/upload";


}


Expand All @@ -87,6 +89,29 @@ protected void testImportServicePost() {
}
importBody.setFilename("sample_application_import.csv");

//TagTypeService mockTagTypeService = Mockito.mock(TagTypeService.class);
Set<TagType> tagTypes = new HashSet<>() ;
TagType tagType1 = new TagType();
tagType1.id = "1";
tagType1.name = "Operating System";
TagType.Tag tag = new TagType.Tag();
tag.id = "1";
tag.name = "RHEL";
tagType1.tags = new ArrayList<>();
tagType1.tags.add(tag);
tagTypes.add(tagType1);
Mockito.when(mockTagTypeService.getListOfTagTypes()).thenReturn(tagTypes);
QuarkusMock.installMockForInstance(mockTagTypeService, TagTypeService.class);

//BusinessServiceService mockBusinessServiceService = Mockito.mock(BusinessServiceService.class);
Set<BusinessService> businessServices = new HashSet<>() ;
BusinessService businessService = new BusinessService();
businessService.id = "1";
businessService.name = "Food2Go";
businessServices.add(businessService);
Mockito.when(mockBusinessServiceService.getListOfBusinessServices()).thenReturn(businessServices);
QuarkusMock.installMockForInstance(mockBusinessServiceService, BusinessServiceService.class);

Response response = given()
.config(RestAssured.config().encoderConfig(EncoderConfig.encoderConfig().encodeContentTypeAs("multipart/form-data", ContentType.JSON)))
.contentType(MediaType.MULTIPART_FORM_DATA)
Expand Down Expand Up @@ -130,11 +155,9 @@ protected void testMapToApplicationRejected()
Long id = appImport1.id;
System.out.println("appImport1.id= " + id);

ApplicationInventoryAPIMapper
apiMapper = Mockito.mock(ApplicationInventoryAPIMapper.class);
Mockito.when(apiMapper.map(appImport1)).thenReturn(javax.ws.rs.core.Response.serverError().build());
QuarkusMock.installMockForInstance(apiMapper, ApplicationInventoryAPIMapper.class);
svc.mapImportsToApplication(appList);
Set<TagType> tagTypes = new HashSet<>();
Set<BusinessService> businessServices = new HashSet<>();
svc.mapImportsToApplication(appList, tagTypes, businessServices);

ApplicationImport refusedImport = ApplicationImport.findById(id);
assertEquals(Boolean.FALSE, refusedImport.getValid());
Expand Down

0 comments on commit d7af407

Please sign in to comment.