Skip to content

Commit

Permalink
WEBITOOLS-192: Add option to Preserve preconfigured module generation…
Browse files Browse the repository at this point in the history
… type
  • Loading branch information
leon2Meudon92 committed Jul 24, 2023
1 parent 86f9e89 commit 9438ffa
Show file tree
Hide file tree
Showing 12 changed files with 163 additions and 27 deletions.
2 changes: 1 addition & 1 deletion aip-console-jenkins/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<packaging>hpi</packaging>
<properties>
<!-- Console Tools version -->
<revision>2.9.0-SNAPSHOT</revision>
<revision>2.9.1-RC</revision>
<!-- Baseline Jenkins version you use to build the plugin. Users must have this version or newer to run. -->
<jenkins.version>2.60.3</jenkins.version>
<java.level>8</java.level>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
</f:entry>
<f:entry title="${%moduleGenerationType}" description="${%moduleGenerationType.descr}">
<select name="moduleGenerationType" field="moduleGenerationType">
<option value="preserve_configured">Preserve configured</option>
<option value="full_content">Full content</option>
<option value="one_per_au">Analysis unit module</option>
<option value="one_per_techno">Technology module</option>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public void setUp() throws Exception {
doReturn(ApiInfoDto.builder().apiVersion("1.12.0-DEV").build())
.when(restApiService).getAipConsoleApiInfo();
doReturn(BaseBuilderTest.TEST_APP).when(applicationService).getApplicationFromGuid(BaseBuilderTest.TEST_APP_NAME);
doReturn(BaseBuilderTest.TEST_APP).when(applicationService).getApplicationFromGuid(BaseBuilderTest.TEST_APP_GUID);
}

@Test
Expand Down Expand Up @@ -100,7 +101,7 @@ public void testBuildFreestyleDefaultOk() throws Exception {
doReturn(BaseBuilderTest.TEST_APP)
.when(applicationService).getApplicationFromName(BaseBuilderTest.TEST_APP_NAME);
doReturn(true)
.when(uploadService).uploadInputStream(eq(BaseBuilderTest.TEST_APP_NAME), anyString(), anyLong(), isA(InputStream.class));
.when(uploadService).uploadInputStream(eq(BaseBuilderTest.TEST_APP_GUID), anyString(), anyLong(), isA(InputStream.class));
doReturn(BaseBuilderTest.TEST_JOB_GUID)
.when(jobsService).startAddVersionJob(any(JobRequestBuilder.class));
doReturn(JobState.COMPLETED)
Expand Down Expand Up @@ -211,7 +212,7 @@ public void testBuildErrorCreatingAnalyzeJob() throws Exception {
doReturn(BaseBuilderTest.TEST_APP)
.when(applicationService).getApplicationFromName(BaseBuilderTest.TEST_APP_NAME);
doReturn(true)
.when(uploadService).uploadInputStream(eq(BaseBuilderTest.TEST_APP_NAME), anyString(), anyLong(), isA(InputStream.class));
.when(uploadService).uploadInputStream(eq(BaseBuilderTest.TEST_APP_GUID), anyString(), anyLong(), isA(InputStream.class));
doThrow(new JobServiceException("fake exception"))
.when(jobsService).startAddVersionJob(any(JobRequestBuilder.class));

Expand All @@ -230,7 +231,7 @@ public void testBuildAnalyseResultCancelled() throws Exception {
doReturn(BaseBuilderTest.TEST_APP)
.when(applicationService).getApplicationFromName(BaseBuilderTest.TEST_APP_NAME);
doReturn(true)
.when(uploadService).uploadInputStream(eq(BaseBuilderTest.TEST_APP_NAME), anyString(), anyLong(), isA(InputStream.class));
.when(uploadService).uploadInputStream(eq(BaseBuilderTest.TEST_APP_GUID), anyString(), anyLong(), isA(InputStream.class));
doReturn(BaseBuilderTest.TEST_JOB_GUID)
.when(jobsService).startAddVersionJob(any(JobRequestBuilder.class));
doReturn(JobState.CANCELED)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package io.jenkins.plugins.aipconsole;

import com.castsoftware.aip.console.tools.core.dto.ModuleGenerationType;
import com.castsoftware.aip.console.tools.core.dto.VersionDto;
import com.castsoftware.aip.console.tools.core.dto.VersionStatus;
import com.castsoftware.aip.console.tools.core.dto.jobs.JobRequestBuilder;
import com.castsoftware.aip.console.tools.core.dto.jobs.JobState;
import com.google.common.collect.Sets;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Result;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.Set;
import java.util.concurrent.Future;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class AnalyzeBuilderTest extends BaseBuilderTest {
@InjectMocks
private AnalyzeBuilder analyzeBuilder;

@Before
public void setUp() throws Exception {
super.startUp();
analyzeBuilder = new AnalyzeBuilder(BaseBuilderTest.TEST_APP_NAME);
MockitoAnnotations.initMocks(this);
doReturn(BaseBuilderTest.TEST_APP).when(applicationService).getApplicationFromGuid(BaseBuilderTest.TEST_APP_NAME);
when(applicationService.getApplicationFromGuid(TEST_APP_GUID)).thenReturn(BaseBuilderTest.TEST_APP);
}

@Test
public void testAnalyze_WithModulePreserveConfigured() throws Exception {
analyzeBuilder.setVersionName(TEST_VERSION_NAME);
analyzeBuilder.setModuleGenerationType(ModuleGenerationType.PRESERVE_CONFIGURED.toString());
FreeStyleProject project = getProjectWithBuilder(analyzeBuilder);

VersionDto versionDto = Mockito.mock(VersionDto.class);
when(versionDto.getName()).thenReturn(TEST_VERSION_NAME);
when(versionDto.getStatus()).thenReturn(VersionStatus.DELIVERED);
Set<VersionDto> versions = Sets.newHashSet(versionDto);
when(applicationService.getApplicationVersion(TEST_APP_GUID)).thenReturn(versions);

doReturn(BaseBuilderTest.TEST_APP_NAME).when(applicationService).getApplicationGuidFromName(BaseBuilderTest.TEST_APP_NAME);
doReturn(BaseBuilderTest.TEST_APP).when(applicationService).getApplicationFromName(BaseBuilderTest.TEST_APP_NAME);
when(jobsService.startJob(any(JobRequestBuilder.class))).thenReturn(BaseBuilderTest.TEST_JOB_GUID);

doReturn(JobState.COMPLETED).when(jobsService).pollAndWaitForJobFinished(eq(BaseBuilderTest.TEST_JOB_GUID), any(), any(), any(), eq(null));

Future<FreeStyleBuild> futureBuild = project.scheduleBuild2(0);
FreeStyleBuild build = jenkins.assertBuildStatus(Result.SUCCESS, futureBuild.get());

//jenkins.assertLogContains(OnbordingApplicationBuilder_DescriptorImpl_FastScanForbidden(), build);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class BaseBuilderTest {
protected static final String TEST_KEY = "key";
protected static final String TEST_CONTENT = "test";
protected static final String TEST_APP_NAME = "appName";
protected static final String TEST_VERSION_NAME = "versionName";
protected static final String TEST_APP_GUID = "app-GUID";
protected static final String TEST_ARCHIVE_NAME = "archive.zip";
protected static final String TEST_FOLDER_NAME = "testFolder";
Expand All @@ -36,7 +37,7 @@ public class BaseBuilderTest {

protected static final String TEST_UPLOAD_FILE_PATH = "C:\\Report\\model.CASTArchitect";
protected static final String TEST_REPORT_PATH = "C:\\Report";
protected static final ApplicationDto TEST_APP = ApplicationDto.builder().name(TEST_APP_NAME).guid(TEST_APP_NAME).inPlaceMode(false).build();
protected static final ApplicationDto TEST_APP = ApplicationDto.builder().name(TEST_APP_NAME).guid(TEST_APP_GUID).inPlaceMode(false).build();
@Rule
public JenkinsRule jenkins = new JenkinsRule();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public class AnalyzeCommand extends BasicCollable {
defaultValue = "true", fallbackValue = "true")
private boolean consolidation = true;

@CommandLine.Option(names = "--module-option", description = "Generates a user defined module option forr either technology module or analysis unit module. Possible value is one of: full_content, one_per_au, one_per_techno")
@CommandLine.Option(names = "--module-option", description = "Generates a user defined module option for either technology module or analysis unit module. Possible value is one of: full_content, one_per_au, one_per_techno")
private ModuleGenerationType moduleGenerationType;

public AnalyzeCommand(RestApiService restApiService, JobsService jobsService, ApplicationService applicationService) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.castsoftware.aip.console.tools.core.utils.Constants;
import com.castsoftware.aip.console.tools.factories.SpringAwareCommandFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
Expand Down Expand Up @@ -151,6 +152,18 @@ protected ApplicationDto getTestApplicationMock() {
return applicationDto;
}

protected CommandLine.Model.OptionSpec getCommandLineOption(CommandLine.Model.CommandSpec spec, String optionName) {
//Checks that the initial value set for the module type is full content
List<CommandLine.Model.ArgSpec> argsSpec = spec.args();
for (CommandLine.Model.ArgSpec cmdArg : argsSpec) {
CommandLine.Model.OptionSpec optionSpec = (CommandLine.Model.OptionSpec) cmdArg;
if (StringUtils.equalsAny(optionName, optionSpec.names())) {
return optionSpec;
}
}
return null;
}

protected void runStringArgs(Callable<Integer> command, String[] args) {
try {
cliToTest = new CommandLine(command, springAwareCommandFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.castsoftware.aip.console.tools.commands.AnalyzeCommand;
import com.castsoftware.aip.console.tools.core.dto.DebugOptionsDto;
import com.castsoftware.aip.console.tools.core.dto.ModuleGenerationType;
import com.castsoftware.aip.console.tools.core.dto.VersionDto;
import com.castsoftware.aip.console.tools.core.dto.VersionStatus;
import com.castsoftware.aip.console.tools.core.dto.jobs.JobExecutionDto;
Expand All @@ -28,8 +29,10 @@

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyString;
Expand Down Expand Up @@ -220,4 +223,51 @@ public void testAnalyzeCommand_JobCanceled() throws ApplicationServiceException,
assertThat(spec, is(notNullValue()));
assertThat(exitCode, is(Constants.RETURN_JOB_CANCELED));
}

@Test
public void testAnalyzeCommand_WithModulePreserveConfigured() throws ApplicationServiceException, UploadException, JobServiceException, PackagePathInvalidException {
boolean verbose = true;
String[] args = new String[]{"--apikey", TestConstants.TEST_API_KEY,
"--app-name=" + TestConstants.TEST_CREATRE_APP,
"--version-name", TestConstants.TEST_VERSION_NAME,
"-S", "--process-imaging",
"--module-option", ModuleGenerationType.PRESERVE_CONFIGURED.toString()};

when(applicationService.getApplicationFromName(TestConstants.TEST_CREATRE_APP)).thenReturn(TestConstants.TEST_APP);
//Set<VersionDto> versions =
VersionDto versionDto = new VersionDto();
versionDto.setName(TestConstants.TEST_VERSION_NAME);
versionDto.setStatus(VersionStatus.DELIVERED);
when(applicationService.getApplicationVersion(TestConstants.TEST_APP_GUID)).thenReturn(Sets.newSet(versionDto));
when(jobsService.startJob(any(JobRequestBuilder.class))).thenReturn(TestConstants.TEST_JOB_GUID);
DebugOptionsDto debugOptions = Mockito.mock(DebugOptionsDto.class);
when(debugOptions.isActivateAmtMemoryProfile()).thenReturn(false);
when(applicationService.getDebugOptions(TestConstants.TEST_APP_GUID)).thenReturn(debugOptions);

JobExecutionDto jobStatus = new JobExecutionDto();
jobStatus.setAppGuid(TestConstants.TEST_APP_GUID);
jobStatus.setState(JobState.COMPLETED);
jobStatus.setCreatedDate(new Date());
jobStatus.setAppName(TestConstants.TEST_CREATRE_APP);
when(jobsService.pollAndWaitForJobFinished(anyString(), any(Function.class), anyBoolean())).thenReturn(jobStatus);

runStringArgs(analyzeCommand, args);

CommandLine.Model.CommandSpec spec = cliToTest.getCommandSpec();
assertThat(spec, is(notNullValue()));

//Checks that the initial value set for the module type is full content
CommandLine.Model.OptionSpec optionSpec = getCommandLineOption(spec, "--module-option");
assertThat(optionSpec.hasInitialValue(), is(true));
assertThat(optionSpec.mapFallbackValue(), is("__unspecified__"));
assertThat(optionSpec.initialValue(), is(nullValue())); //not initialized in the command
assertThat(optionSpec.typedValues(), hasSize(1));
ModuleGenerationType typeInfo = (ModuleGenerationType) optionSpec.typedValues().get(0);
assertThat(typeInfo, is(ModuleGenerationType.PRESERVE_CONFIGURED));

// What you set is what you get
assertThat(analyzeCommand.getModuleGenerationType(), is(ModuleGenerationType.PRESERVE_CONFIGURED));
assertThat(exitCode, is(Constants.RETURN_OK));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.stream.Collectors;

public enum ModuleGenerationType {
PRESERVE_CONFIGURED,
ONE_PER_TECHNO,
ONE_PER_AU,
FULL_CONTENT;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ private JobRequestBuilder(String appGuid, String sourcePath, JobType jobType, St
this.sourcePath = sourcePath;
this.jobType = jobType;
this.caipVersion = caipVersion;
this.startStep = Constants.EXTRACT_STEP_NAME;
this.endStep = Constants.SNAPSHOT_INDICATOR;
this.objectives.add(GLOBAL_RISK_OBJECTIVE);
this.objectives.add(FUNCTIONAL_POINTS_OBJECTIVE);
startStep = Constants.EXTRACT_STEP_NAME;
endStep = Constants.SNAPSHOT_INDICATOR;
objectives.add(GLOBAL_RISK_OBJECTIVE);
objectives.add(FUNCTIONAL_POINTS_OBJECTIVE);

Date now = new Date();
this.versionName = String.format("v%s", VERSION_NAME_FORMATTER.format(now));
versionName = String.format("v%s", VERSION_NAME_FORMATTER.format(now));
String nowStr = RELEASE_DATE_FORMATTER.format(now);
this.releaseDateStr = nowStr;
this.snapshotDateStr = nowStr;
releaseDateStr = nowStr;
snapshotDateStr = nowStr;
}

public JobRequestBuilder nodeName(String nodeName) {
Expand Down Expand Up @@ -126,10 +126,10 @@ public JobRequestBuilder backupName(String backupName) {

public JobRequestBuilder securityObjective(boolean enable) {
if (enable) {
this.objectives.add(SECURITY_OBJECTIVE);
objectives.add(SECURITY_OBJECTIVE);
}
if (!enable && this.objectives.contains(SECURITY_OBJECTIVE)) {
this.objectives = new ArrayList<>();
if (!enable && objectives.contains(SECURITY_OBJECTIVE)) {
objectives = new ArrayList<>();
objectives.add(GLOBAL_RISK_OBJECTIVE);
objectives.add(FUNCTIONAL_POINTS_OBJECTIVE);
}
Expand Down Expand Up @@ -159,7 +159,7 @@ public JobRequestBuilder releaseAndSnapshotDate(Date date) {
return this;
}
String dateStr = RELEASE_DATE_FORMATTER.format(date);
return this.releaseDateStr(dateStr)
return releaseDateStr(dateStr)
.snapshotDateStr(dateStr);
}

Expand All @@ -168,15 +168,15 @@ public JobRequestBuilder snapshotDate(Date date) {
return this;
}
String dateStr = RELEASE_DATE_FORMATTER.format(date);
return this.snapshotDateStr(dateStr);
return snapshotDateStr(dateStr);
}

public JobRequestBuilder versionReleaseDate(Date releaseDate) {
if (releaseDate == null) {
return this;
}
String dateStr = RELEASE_DATE_FORMATTER.format(releaseDate);
return this.releaseDateStr(dateStr);
return releaseDateStr(dateStr);
}

public JobRequestBuilder snapshotDateStr(String snapshotDateStr) {
Expand All @@ -185,7 +185,7 @@ public JobRequestBuilder snapshotDateStr(String snapshotDateStr) {
}

public JobRequestBuilder versionGuid(String guid) {
this.versionGuid = guid;
versionGuid = guid;
return this;
}

Expand All @@ -195,14 +195,14 @@ public JobRequestBuilder snapshotName(String snapshotName) {
}

public JobRequestBuilder moduleGenerationType(ModuleGenerationType generationType) {
this.moduleGenerationType = generationType;
moduleGenerationType = generationType;
return this;
}

private Map<String, Object> getJobParameters() {
Map<String, Object> parameters = new HashMap<>();
parameters.put(Constants.PARAM_APP_GUID, this.appGuid);
parameters.put(Constants.PARAM_VERSION_NAME, this.versionName);
parameters.put(Constants.PARAM_APP_GUID, appGuid);
parameters.put(Constants.PARAM_VERSION_NAME, versionName);
if (StringUtils.isNotBlank(sourcePath)) {
parameters.put(Constants.PARAM_SOURCE_PATH, sourcePath);
// for 1.12 compatibility
Expand Down Expand Up @@ -247,7 +247,9 @@ private Map<String, Object> getJobParameters() {
parameters.put(Constants.PARAM_BACKUP_NAME, backupName);
}
parameters.put(Constants.PARAM_PROCESS_IMAGING, Boolean.toString(processImaging));
if (moduleGenerationType != null && moduleGenerationType != ModuleGenerationType.FULL_CONTENT) {
if (moduleGenerationType != null
&& moduleGenerationType != ModuleGenerationType.PRESERVE_CONFIGURED
&& moduleGenerationType != ModuleGenerationType.FULL_CONTENT) {
// Full_content manged by application service
parameters.put(Constants.PARAM_MODULE_GENERATION_TYPE, moduleGenerationType.toString());
}
Expand All @@ -258,7 +260,7 @@ private Map<String, Object> getJobParameters() {
public CreateJobsRequest buildJobRequest() {
CreateJobsRequest jobRequest = new CreateJobsRequest();
jobRequest.setJobType(jobType);
jobRequest.setJobParameters(this.getJobParameters());
jobRequest.setJobParameters(getJobParameters());
return jobRequest;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,9 @@ public String runDeepAnalysis(String applicationGuid, String targetNode, String
requestBuilder.snapshotName(snapshotName);
}
//The module parameter should be left empty or null when dealing with full content
if (moduleGenerationType != null && (moduleGenerationType != ModuleGenerationType.FULL_CONTENT)) {
if (moduleGenerationType != null
&& (moduleGenerationType != ModuleGenerationType.PRESERVE_CONFIGURED)
&& (moduleGenerationType != ModuleGenerationType.FULL_CONTENT)) {
requestBuilder.moduleGenerationType(moduleGenerationType.toString());
}
return runDeepAnalysis(requestBuilder.build(), logPollingProvider);
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</scm>

<properties>
<revision>2.9.0-SNAPSHOT</revision>
<revision>2.9.1-RC</revision>
<java.version>1.8</java.version>
<spring-boot.version>${project.parent.version}</spring-boot.version>
<picocli.version>4.6.1</picocli.version>
Expand Down

0 comments on commit 9438ffa

Please sign in to comment.