Skip to content

Commit

Permalink
Merge pull request sw360#208 from sw360/autosettingClearingState#127
Browse files Browse the repository at this point in the history
Autosetting clearing state#127
  • Loading branch information
alexbrdn authored Aug 30, 2016
2 parents 1f17cc0 + f27f1d7 commit 3c652fe
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.*;

import static com.google.common.base.Predicates.equalTo;
import static com.google.common.base.Strings.isNullOrEmpty;
Expand Down Expand Up @@ -107,6 +104,7 @@ private RequestStatus sendToFossologyNewUpload(String releaseId, User user, Stri
//to avoid race conditions, get the object again
Release release = assertNotNull(componentService.getReleaseById(releaseId, user));
setFossologyStatus(release, clearingTeam, FossologyStatus.SENT, Integer.toString(fossologyUploadId), attachmentContent.getId());
updateReleaseClearingState(release, FossologyStatus.SENT);
updateRelease(release, user, componentService);
return RequestStatus.SUCCESS;
}
Expand Down Expand Up @@ -144,6 +142,7 @@ private RequestStatus duplicateUploadFor(Release release, User user, String clea

private void updateFossologyStatus(Release release, User user, String clearingTeam, FossologyStatus currentStatus, ComponentService.Iface componentService) throws TException {
setFossologyStatus(release, clearingTeam, currentStatus);
updateReleaseClearingState(release, currentStatus);
updateRelease(release, user, componentService);
}

Expand Down Expand Up @@ -180,6 +179,11 @@ public Release getStatusInFossology(String releaseId, User user, String clearing
updateInDb = false;
}
}
Optional<FossologyStatus> maxStatus = nullToEmptyMap(release.getClearingTeamToFossologyStatus())
.values()
.stream()
.max(FossologyStatus::compareTo);
updateReleaseClearingState(release, maxStatus);

if (updateInDb) {
updateRelease(release, user, componentClient);
Expand All @@ -191,6 +195,26 @@ public Release getStatusInFossology(String releaseId, User user, String clearing
return release;
}

private void updateReleaseClearingState(Release release, FossologyStatus fossologyStatus) {
updateReleaseClearingState(release, Optional.of(fossologyStatus));
}
private void updateReleaseClearingState(Release release, Optional<FossologyStatus> fossologyStatus) {
Optional<ClearingState> newClearingState = fossologyStatus.flatMap(this::mapFossologyStatusToClearingState);
if (newClearingState.isPresent() && newClearingState.get().compareTo(release.getClearingState()) > 0){
release.setClearingState(newClearingState.get());
}
}

private Optional<ClearingState> mapFossologyStatusToClearingState(FossologyStatus fossologyStatus) {
if (fossologyStatus==FossologyStatus.IN_PROGRESS){
return Optional.of(ClearingState.UNDER_CLEARING);
} else if (fossologyStatus.compareTo(FossologyStatus.SENT) >= 0 &&
fossologyStatus.compareTo(FossologyStatus.IN_PROGRESS) < 0){
return Optional.of(ClearingState.SENT_TO_FOSSOLOGY);
}
return Optional.empty();
}

protected Release getReleaseAndUnlockIt(String releaseId, User user, ComponentService.Iface componentClient) throws TException {
final Release release = componentClient.getReleaseById(releaseId, user);

Expand Down Expand Up @@ -228,8 +252,6 @@ protected void setFossologyStatus(Release release, final String clearingTeam, Fo

if (!isNullOrEmpty(fossologyUploadId)) {
release.setFossologyId(fossologyUploadId);
release.setClearingState(ClearingState.SENT_TO_FOSSOLOGY);

release.setAttachmentInFossology(attachmentId);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public void testSendToFossologyDuplicates() throws Exception {

when(release.getFossologyId()).thenReturn("41");
when(release.isSetFossologyId()).thenReturn(true);
when(release.getClearingState()).thenReturn(ClearingState.SENT_TO_FOSSOLOGY);

spyGetFilledSourceAttachment(filledAttachment);

Expand Down Expand Up @@ -172,6 +173,7 @@ public void testSendToFossologySendsAnAttachment() throws Exception {

final InputStream inputStream = mock(InputStream.class);
when(release.isSetFossologyId()).thenReturn(false);
when(release.getClearingState()).thenReturn(ClearingState.NEW_CLEARING);
when(attachmentConnector.getAttachmentStream(attachmentContent)).thenReturn(inputStream);
when(fossologyUploader.uploadToFossology(inputStream, attachmentContent, clearingTeam)).thenReturn(1);

Expand Down Expand Up @@ -441,7 +443,6 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
verify(release).setClearingTeamToFossologyStatus(anyMapOf(String.class, FossologyStatus.class));

verify(release).setFossologyId("13");
verify(release).setClearingState(ClearingState.SENT_TO_FOSSOLOGY);
verify(release).setAttachmentInFossology("14");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ private void prepareReleaseEdit(RenderRequest request, RenderResponse response)
} else {
release = new Release();
release.setComponentId(id);
release.setClearingState(ClearingState.NEW_CLEARING);
request.setAttribute(RELEASE, release);
putDirectlyLinkedReleaseRelationsInRequest(request, release.getReleaseIdToRelationship());
setAttachmentsInRequest(request, release.getAttachments());
Expand Down Expand Up @@ -708,6 +709,7 @@ public void updateRelease(ActionRequest request, ActionResponse response) throws
} else {
release = new Release();
release.setComponentId(component.getId());
release.setClearingState(ClearingState.NEW_CLEARING);
ComponentPortletUtils.updateReleaseFromRequest(request, release);
releaseId = client.addRelease(release, user);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright Siemens AG, 2013-2015. Part of the SW360 Portal Project.
* Copyright Siemens AG, 2013-2016. Part of the SW360 Portal Project.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand All @@ -8,11 +8,8 @@
*/
package com.siemens.sw360.portal.portlets.components;

import com.siemens.sw360.datahandler.common.CommonUtils;
import com.siemens.sw360.datahandler.thrift.RequestStatus;
import com.siemens.sw360.datahandler.thrift.ThriftClients;
import com.siemens.sw360.datahandler.thrift.attachments.Attachment;
import com.siemens.sw360.datahandler.thrift.attachments.AttachmentService;
import com.siemens.sw360.datahandler.thrift.components.*;
import com.siemens.sw360.datahandler.thrift.users.RequestedAction;
import com.siemens.sw360.datahandler.thrift.users.User;
Expand All @@ -27,7 +24,6 @@
import javax.portlet.PortletRequest;
import javax.portlet.ResourceRequest;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -69,6 +65,9 @@ public static void updateReleaseFromRequest(PortletRequest request, Release rele
release.setReleaseIdToRelationship(new HashMap<String, ReleaseRelationship>());
updateLinkedReleaesFromRequest(request, release.releaseIdToRelationship);
break;
case CLEARING_STATE:
// skip setting CLEARING_STATE. it is supposed to be set only programmatically, never from user input.
break;
default:
setFieldValue(request, release, field);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,10 @@
<tr>
<td width="33%">
<label class="textlabel stackedLabel" for="clearing_state">Clearing State</label>
<select class="toplabelledInput" id="clearing_state"
<input id="clearing_state"
name="<portlet:namespace/><%=Release._Fields.CLEARING_STATE%>"
style="min-width: 162px; min-height: 28px;">

<sw360:DisplayEnumOptions type="<%=ClearingState.class%>" selected="${release.clearingState}"/>
</select>
type="text" readonly
value="<sw360:DisplayEnum value="${release.clearingState}"/>"/>
</td>
<td width="33%">
<label class="textlabel stackedLabel" for="mainline_state">Mainline State</label>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ public static ReleaseClearingStateSummary computeReleaseClearingStateSummary(Lis
}

static Void addReleaseWithStates(ReleaseClearingStateSummary summary, ViewedState globalState, ViewedState myTeamState, ViewedState otherTeamState) {
if (globalState.present()) { // if the release has its clearing state field set use it and ignore the rest
// if the release has its clearing state field set to REPORT_AVAILABLE or APPROVED use it and ignore the rest
// otherwise, look at what fossology has to say
// if all else fails, default to NEW
if (globalState.present() && globalState.compareTo(ViewedState.REPORT_AVAILABLE) >= 0) {
return addReleaseState(summary, globalState);
}
if (myTeamState.present()) { // if my team has something to say ignore the rest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright Siemens AG, 2013-2015. Part of the SW360 Portal Project.
* Copyright Siemens AG, 2013-2016. Part of the SW360 Portal Project.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand All @@ -19,6 +19,7 @@

/**
* @author [email protected]
* @author [email protected]
*/
public class ReleaseClearingStateSummaryComputerTest extends ScenarioTest<GivenReleasesWithFossologyStatus, WhenComputeClearingState, ThenReleaseClearingState> {

Expand Down Expand Up @@ -81,17 +82,18 @@ public void test2() throws Exception {
@Test
public void test3() throws Exception {
given()
.a_release_with_clearing_status(ClearingState.SENT_TO_FOSSOLOGY)
.a_release_with_clearing_status(ClearingState.NEW_CLEARING)
.and()
.a_release_with_fossology_status_$_for_$_and_$_for_$(
.a_release_with_clearing_status_$_and_fossology_status_$_for_$_and_$_for_$(
ClearingState.SENT_TO_FOSSOLOGY,
FossologyStatus.SCANNING, CLEARING_TEAM,
FossologyStatus.CLOSED, ANOTHER_CLEARING_TEAM);

when().the_clearing_state_is_computed_for(CLEARING_TEAM);

then()
.new_releases_should_be(0).and()
.under_clearing_should_be(1).and()
.new_releases_should_be(1).and()
.under_clearing_should_be(0).and()
.under_clearing_by_project_team_should_be(1).and()
.report_available_should_be(0);

Expand Down Expand Up @@ -170,7 +172,7 @@ public void test6() throws Exception {
@Test
public void test7() throws Exception {
given()
.a_release_with_clearing_status(ClearingState.SENT_TO_FOSSOLOGY);
.a_release_with_clearing_status_$_and_fossology_status_$_for_$(ClearingState.SENT_TO_FOSSOLOGY, FossologyStatus.OPEN, ANOTHER_CLEARING_TEAM);

when().the_clearing_state_is_computed_for(CLEARING_TEAM);

Expand Down Expand Up @@ -229,26 +231,26 @@ public void test90() throws Exception {
}

@Test
public void the_clearing_moves_to_the_right_following_clearing_team_but_is_reset_by_the_global_release_clearing_state() {
public void the_clearing_moves_to_the_right_following_clearing_team_but_is_overwritten_by_global_clearing_state_above_under_clearing() {
given().a_new_release();

when().the_clearing_state_is_computed_for(CLEARING_TEAM);

then().new_releases_should_be(1);

when().the_release_is_sent_for_clearing_to(CLEARING_TEAM);
then().new_releases_should_be(0).and().under_clearing_by_project_team_should_be(1);
then().new_releases_should_be(0).and().under_clearing_by_project_team_should_be(1).and().report_available_should_be(0).and().approved_should_be(0);

when().the_release_is_sent_for_clearing_to(ANOTHER_CLEARING_TEAM);
then().under_clearing_should_be(0).and().under_clearing_by_project_team_should_be(1);

when().team_$_sets_fossology_status_to(CLEARING_TEAM, FossologyStatus.CLOSED);
then().under_clearing_should_be(0).and().under_clearing_by_project_team_should_be(0).and().report_available_should_be(1);
then().under_clearing_should_be(0).and().under_clearing_by_project_team_should_be(1).and().report_available_should_be(0).and().approved_should_be(0);

when().the_release_clearing_state_is_set_to(ClearingState.UNDER_CLEARING);
then().under_clearing_should_be(1).and().under_clearing_by_project_team_should_be(0).and().report_available_should_be(0);
// when().team_$_sets_fossology_status_to(CLEARING_TEAM, FossologyStatus.CLOSED);
// then().under_clearing_should_be(0).and().under_clearing_by_project_team_should_be(0).and().report_available_should_be(1);
//
when().the_release_clearing_state_is_set_to(ClearingState.REPORT_AVAILABLE);
then().under_clearing_should_be(0).and().under_clearing_by_project_team_should_be(0).and().report_available_should_be(1).and().approved_should_be(0);

when().the_release_clearing_state_is_set_to(ClearingState.NEW_CLEARING);
then().under_clearing_should_be(0).and().under_clearing_by_project_team_should_be(0).and().report_available_should_be(1);
when().the_release_clearing_state_is_set_to(ClearingState.APPROVED);
then().under_clearing_should_be(0).and().under_clearing_by_project_team_should_be(0).and().report_available_should_be(0).and().approved_should_be(1);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright Siemens AG, 2013-2015. Part of the SW360 Portal Project.
* Copyright Siemens AG, 2013-2016. Part of the SW360 Portal Project.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
Expand Down Expand Up @@ -42,4 +42,9 @@ public ThenReleaseClearingState report_available_should_be(int i) {
assertThat(releaseClearingStateSummary.getReportAvailable(), is(i));
return self();
}

public ThenReleaseClearingState approved_should_be(int i) {
assertThat(releaseClearingStateSummary.getApproved(), is(i));
return self();
}
}

0 comments on commit 3c652fe

Please sign in to comment.