+
+ /**
+ * Tests a checkin testcase.
+ * @param testCaseNumber
+ * the test case number
+ * @param scenarioId
+ * the scenrio id
+ * @param userId
+ * the user id
+ * @param hasSource
+ * whether the scenario is supposed to have a source file in this test case befor checkin
+ * @param hasAttachments
+ * whether the scenario is supposed to have files attached to the source
+ * in this test case befor checkin
+ * @param checkinSource
+ * whether the "comm" file should be checked in
+ * @param checkinAttachment
+ * whether the "resu" and "mess" files should be checked in
+ * @return
+ * set of strings indicating which cases has occurred
+ * @throws IOException
+ * if something is wrong
+ * @throws InvalidPropertyException
+ * if something is wrong
+ * @throws MultiplyDefinedException
+ * if something is wrong
+ * @throws MissedPropertyException
+ * if something is wrong
+ * @throws SQLException
+ * if something is wrong
+ * @throws NotApplicableException
+ * if something is wrong
+ * @throws MismatchException
+ * if something is wrong
+ */
+ private Set<String> testCheckinTestcase(final long testCaseNumber,
+ final long scenarioId, final long userId,
+ final boolean hasSource, final boolean hasAttachments,
+ final boolean checkinSource, final boolean checkinAttachment)
+ throws IOException, InvalidPropertyException, MultiplyDefinedException,
+ MissedPropertyException, SQLException, NotApplicableException, MismatchException{
+
+ Scenario aScen = _scenarioDAO.get(scenarioId);
+ User user = _userDAO.get(userId);
+
+ List<StepDTO> steps = _scenarioService.getScenarioInfo(scenarioId);
+
+ // ////////////////////////////////////////////////////////
+ // Call checkin method for good prepared transient data.
+
+ // Simulate checkout
+ steps = _scenarioService.getScenarioInfo(scenarioId);
+ _scenarioService.checkout(aScen, user);
+
+ // Remember modification dates of all attached files
+ Map<Long, Date> dates = new HashMap<Long, Date>();
+ for (Publication p : aScen.getDocums()) {
+ for (Relation r : p.value().getRelations(ConvertsRelation.class)) {
+ org.splat.dal.bo.som.File attach = ((ConvertsRelation) r)
+ .getTo();
+ dates.put(attach.getIndex(), attach.getDate());
+ }
+ }
+
+ long targetDocId =
+ addMecaDocsToScenario(testCaseNumber, aScen, user, hasAttachments, hasSource);
+
+ List<StepDTO> stepsToCheckin = new ArrayList<StepDTO>();
+ for(StepDTO step : steps) {
+ createDocDTOForMeca(testCaseNumber, targetDocId, null, userId, step, stepsToCheckin,
+ checkinSource, checkinAttachment);
+ }
+
+ // /////////////////////////////////////////////////////////////////
+ // Do test checkin
+ _scenarioService.checkin(scenarioId, userId, stepsToCheckin);
+
+ // Check that scenario is no more marked as checked out
+ aScen = _scenarioDAO.get(scenarioId);
+ return assertCheckinValidity(testCaseNumber, stepsToCheckin, aScen, dates);
+ }
+
+ /**
+ * It will delete any "comm" publications in the scenario, and then create a new one,
+ * with attachments if specified.
+ * @param testCaseNumber
+ * the test case number
+ * @param aScen
+ * the scenario
+ * @param user
+ * user who will be used as an author for the added documents
+ * @param hasAttachments
+ * whether to add the "comm" doc to the scenario
+ * @param hasSource
+ * whether to add "resu" and "mess" docs to the scenario
+ * @return
+ * the source document id, if exists, 0 otherwise.
+ * @throws IOException
+ * if something is wrong
+ * @throws InvalidPropertyException
+ * if something is wrong
+ * @throws MissedPropertyException
+ * if something is wrong
+ * @throws MultiplyDefinedException
+ * if something is wrong
+ */
+ private long addMecaDocsToScenario(final long testCaseNumber,
+ final Scenario aScen, final User user,
+ final boolean hasAttachments, final boolean hasSource)
+ throws IOException, InvalidPropertyException, MissedPropertyException,
+ MultiplyDefinedException {
+
+ long res = 0;
+ org.splat.som.Step mecaStep = null;
+ for (org.splat.som.Step step : _projectElementService.getSteps(aScen)) {
+ if ("SALOME_MECA".equals(step.getStep().getModule())) {
+ mecaStep = step;
+ }
+ }
+
+ //remove comm documents
+ List<Publication> toRemove = new ArrayList<Publication>();
+ for (Publication pub : mecaStep.getDocuments()) {
+ if ("comm".equals(pub.value().getFormat())) {
+ toRemove.add(pub);
+ }
+ }
+ for (Publication pub : toRemove) {
+ // remove relations so the publication can be removed correctly
+ List<Relation> relations = new ArrayList<Relation>();
+ relations.addAll(pub.value().getAllRelations());
+ for (Relation rel : relations) {
+ pub.value().removeRelation(UsesRelation.class, rel.getTo());
+ }
+ aScen.remove(pub);
+ }
+
+ if (hasSource) {
+ // Select result document type for Meca step
+ List<DocumentType> dtypes = _documentTypeService
+ .selectTypesOf(mecaStep.getStep());
+ DocumentType resultType = null;
+ for (DocumentType doctype : dtypes) {
+ if(doctype.isResultOf(mecaStep.getStep())) {
+ resultType = doctype;
+ break;
+ }
+ }
+
+ Document.Properties dprop = new Document.Properties();
+ File directory = _repositoryService.getDownloadDirectory(user);
+ directory.mkdirs();
+ dprop.setName("commDoc" + testCaseNumber)
+ .setFormat("comm")
+ .setAuthor(user)
+ .setDate(new Date())
+ .setType(resultType)
+ .setLocalPath(dprop.getName() + "." + dprop.getFormat());
+
+ Publication commPub = createDoc(aScen, mecaStep, dprop, "", false);
+ // The following is necessary because createDoc does not do all required work
+ // (and PublicationServiceImpl.createDoc() is complicated so it's harder to make it work)
+ commPub.setStep(mecaStep);
+ aScen.getDocums().add(commPub);
+ mecaStep.getDocuments().add(commPub);
+
+ res = commPub.value().getIndex();
+
+ // add attachments
+ if (hasAttachments) {
+ // Create new "resu" file
+ FileDTO resuFileDTO = createDownloadedFile(user.getIndex(), "resuFile", "resu");
+ ConvertsRelation export = _publicationService.attach(commPub, "resu");
+ File resuFile = new File(resuFileDTO.getPath());
+ File dest = export.getTo().asFile();
+ dest.delete();
+ Assert.assertTrue(resuFile.renameTo(dest));
+
+ // Create new "mess" file
+ FileDTO messFileDTO = createDownloadedFile(user.getIndex(), "messFile", "mess");
+ export = _publicationService.attach(commPub, "mess");
+ File messFile = new File(messFileDTO.getPath());
+ dest = export.getTo().asFile();
+ dest.delete();
+ Assert.assertTrue(messFile.renameTo(dest));
+ }
+ }
+
+ return res;
+ }
+
+ /**
+ * Prepare a document with a file for check-in.
+ *
+ * @param stepTo
+ * step DTO with data for check-in
+ * @param userId
+ * download directory
+ * @param stepFrom
+ * checked out stepDTO
+ * @param stepsToCheckin
+ * DTO for check-in
+ * @param createSource
+ * whether to add the COMM file to the DTO
+ * @param createAttachment
+ * whether to add the RESU and MESS files to the DTO
+ * @throws IOException
+ * if file creation failed
+ * @return step DTO with data prepared for check-in (stepTo or new if stepTo is null)
+ */
+ private StepDTO createDocDTOForMeca(final long testCaseNumber,
+ final long targetDocId, final StepDTO stepTo,
+ final long userId, final StepDTO stepFrom, final List<StepDTO> stepsToCheckin,
+ final boolean createSource, final boolean createAttachment)
+ throws IOException {
+ StepDTO stepToCheckin = stepTo;
+ if (stepToCheckin == null) {
+ stepToCheckin = new StepDTO();
+ }
+ if ("SALOME_MECA".equals(stepFrom.getModule())) {
+
+ stepsToCheckin.add(stepToCheckin);
+ stepToCheckin.setNumber(stepFrom.getNumber());
+
+ DocumentDTO docToCheckin = stepToCheckin.addDoc(
+ targetDocId, "newCommDoc");
+
+ if(createSource) {
+ docToCheckin.addFile(createDownloadedFile(
+ userId, "newCommDoc" + testCaseNumber + "_result",
+ "comm"));
+ }
+ if(createAttachment) {
+ docToCheckin.addFile(createDownloadedFile(
+ userId, "newResuDoc" + testCaseNumber + "_result",
+ "resu"));
+ docToCheckin.addFile(createDownloadedFile(
+ userId, "newMessDoc" + testCaseNumber + "_result",
+ "mess"));
+ }
+ }
+ return stepToCheckin;
+ }