@Test void interactive() throws Exception { Request request = mock(Request.class); Job job = mock(Job.class); JobStatus status = mock(JobStatus.class); when(job.getRequest()).thenReturn(request); when(request.isInteractive()).thenReturn(true); when(job.getStatus()).thenReturn(status); Map<EntityReference, EntitySelection> concernedEntities = new HashMap<>(); DocumentReference xclass1 = new DocumentReference("wiki1", "a", "xclass1"); DocumentReference simplePage1 = new DocumentReference("wiki1", "b", "simplepage"); DocumentReference simplePage2 = new DocumentReference("wiki2", "a", "otherpage"); DocumentReference xclass2 = new DocumentReference("wiki2", "b", "xclass2"); EntitySelection xclass1Selection = new EntitySelection(xclass1); EntitySelection simplePage1Selection = new EntitySelection(simplePage1); EntitySelection simplePage2Selection = new EntitySelection(simplePage2); EntitySelection xclass2Selection = new EntitySelection(xclass2); concernedEntities.put(xclass1, xclass1Selection); concernedEntities.put(simplePage1, simplePage1Selection); concernedEntities.put(simplePage2, simplePage2Selection); concernedEntities.put(xclass2, xclass2Selection); DocumentReference docObjReference1 = new DocumentReference("wiki1", "a", "xobject1"); DocumentReference docObjReference2 = new DocumentReference("wiki1", "b", "xobject2"); DocumentReference docObjReference3 = new DocumentReference("wiki2", "b", "xobject3"); Query query = mock(Query.class); when(queryManager.createQuery(any(), eq(Query.HQL))).thenReturn(query); when(query.setLimit(anyInt())).thenReturn(query); Query query1 = mock(Query.class); when(query.bindValue("className", "a.xclass1")).thenReturn(query1); when(query1.setWiki("wiki1")).thenReturn(query1); when(query1.execute()) .thenReturn(Arrays.asList("a.xobject1", "b.xobject2")); Query query2 = mock(Query.class); when(query.bindValue("className", "b.simplepage")).thenReturn(query2); when(query2.setWiki("wiki1")).thenReturn(query2); when(query2.execute()).thenReturn(Collections.emptyList()); Query query3 = mock(Query.class); when(query.bindValue("className", "a.otherpage")).thenReturn(query3); when(query3.setWiki("wiki2")).thenReturn(query3); when(query3.execute()).thenReturn(Collections.emptyList()); Query query4 = mock(Query.class); when(query.bindValue("className", "b.xclass2")).thenReturn(query4); when(query4.setWiki("wiki2")).thenReturn(query4); when(query4.execute()).thenReturn(Arrays.asList("b.xobject3")); when(resolver.resolve("a.xobject1", EntityType.DOCUMENT, xclass1)).thenReturn(docObjReference1); when(resolver.resolve("b.xobject2", EntityType.DOCUMENT, xclass1)).thenReturn(docObjReference2); when(resolver.resolve("b.xobject3", EntityType.DOCUMENT, xclass2)).thenReturn(docObjReference3); when(documentAccessBridge.isAdvancedUser(any())).thenReturn(true); doAnswer(invocationOnMock -> { XClassBreakingQuestion question = invocationOnMock.getArgument(0); assertEquals(concernedEntities, question.getConcernedEntities()); Set<EntitySelection> expectedFreePages = new HashSet<>(); expectedFreePages.add(simplePage1Selection); expectedFreePages.add(simplePage2Selection); assertEquals(expectedFreePages, question.getFreePages()); Set<EntitySelection> expectedXPages = new HashSet<>(); expectedXPages.add(xclass1Selection); expectedXPages.add(xclass2Selection); assertEquals(expectedXPages, question.getXClassPages()); Map<EntityReference, Set<EntityReference>> expectedImpactedObjects = new HashMap<>(); Set<EntityReference> expectedSetClass1 = new HashSet<>(); expectedSetClass1.add(docObjReference1); expectedSetClass1.add(docObjReference2); expectedImpactedObjects.put(xclass1, expectedSetClass1); Set<EntityReference> expectedSetClass2 = new HashSet<>(); expectedSetClass2.add(docObjReference3); expectedImpactedObjects.put(xclass2, expectedSetClass2); assertEquals(expectedImpactedObjects, question.getImpactedObjects()); for (EntitySelection selection : question.getConcernedEntities().values()) { assertFalse(selection.isSelected(), selection.getEntityReference() + " should not be selected"); } return null; }).when(status).ask(any(), anyLong(), any()); DocumentsDeletingEvent event = new DocumentsDeletingEvent(); this.deletingListener.onEvent(event, job, concernedEntities); verify(status, times(1)).ask(any(), eq(5L), eq(TimeUnit.MINUTES)); this.logCapture.ignoreAllMessages(); }
@Override public void onEvent(Event event, Object source, Object data) { Job job = (Job) source; CancelableEvent cancelableEvent = (CancelableEvent) event; XClassBreakingQuestion question = this.buildQuestion(job, cancelableEvent, data); if (question == null) { return; } if (!question.getImpactedObjects().isEmpty()) { JobStatus jobStatus = job.getStatus(); try { if (this.documentAccessBridge.isAdvancedUser(job.getRequest().getProperty("user.reference"))) { question.unselectAll(); boolean ack = job.getStatus().ask(question, 5, TimeUnit.MINUTES); if (!ack) { String message = "The question has been asked, however no answer has been received."; this.logger.warn(message); cancelableEvent.cancel(message); } } else { question.setRefactoringForbidden(true); boolean ack = jobStatus.ask(question, 1, TimeUnit.MINUTES); if (!ack) { String message = "The question has been canceled because this refactoring is forbidden."; cancelableEvent.cancel(message); } } } catch (InterruptedException e) { this.logger.warn("Confirm question has been interrupted."); cancelableEvent.cancel("Question has been interrupted."); } if (jobStatus instanceof CancelableJobStatus) { CancelableJobStatus cancelableJobStatus = (CancelableJobStatus) jobStatus; if (cancelableJobStatus.isCanceled()) { cancelableEvent.cancel(); } if (cancelableEvent.isCanceled()) { cancelableJobStatus.cancel(); } } } }
XClassDeletingListener extends AbstractEventListener { @Override public void onEvent(Event event, Object source, Object data) { Job job = (Job) source; CancelableEvent cancelableEvent = (CancelableEvent) event; XClassBreakingQuestion question = this.buildQuestion(job, cancelableEvent, data); if (question == null) { return; } if (!question.getImpactedObjects().isEmpty()) { JobStatus jobStatus = job.getStatus(); try { if (this.documentAccessBridge.isAdvancedUser(job.getRequest().getProperty("user.reference"))) { question.unselectAll(); boolean ack = job.getStatus().ask(question, 5, TimeUnit.MINUTES); if (!ack) { String message = "The question has been asked, however no answer has been received."; this.logger.warn(message); cancelableEvent.cancel(message); } } else { question.setRefactoringForbidden(true); boolean ack = jobStatus.ask(question, 1, TimeUnit.MINUTES); if (!ack) { String message = "The question has been canceled because this refactoring is forbidden."; cancelableEvent.cancel(message); } } } catch (InterruptedException e) { this.logger.warn("Confirm question has been interrupted."); cancelableEvent.cancel("Question has been interrupted."); } if (jobStatus instanceof CancelableJobStatus) { CancelableJobStatus cancelableJobStatus = (CancelableJobStatus) jobStatus; if (cancelableJobStatus.isCanceled()) { cancelableEvent.cancel(); } if (cancelableEvent.isCanceled()) { cancelableJobStatus.cancel(); } } } } }
XClassDeletingListener extends AbstractEventListener { @Override public void onEvent(Event event, Object source, Object data) { Job job = (Job) source; CancelableEvent cancelableEvent = (CancelableEvent) event; XClassBreakingQuestion question = this.buildQuestion(job, cancelableEvent, data); if (question == null) { return; } if (!question.getImpactedObjects().isEmpty()) { JobStatus jobStatus = job.getStatus(); try { if (this.documentAccessBridge.isAdvancedUser(job.getRequest().getProperty("user.reference"))) { question.unselectAll(); boolean ack = job.getStatus().ask(question, 5, TimeUnit.MINUTES); if (!ack) { String message = "The question has been asked, however no answer has been received."; this.logger.warn(message); cancelableEvent.cancel(message); } } else { question.setRefactoringForbidden(true); boolean ack = jobStatus.ask(question, 1, TimeUnit.MINUTES); if (!ack) { String message = "The question has been canceled because this refactoring is forbidden."; cancelableEvent.cancel(message); } } } catch (InterruptedException e) { this.logger.warn("Confirm question has been interrupted."); cancelableEvent.cancel("Question has been interrupted."); } if (jobStatus instanceof CancelableJobStatus) { CancelableJobStatus cancelableJobStatus = (CancelableJobStatus) jobStatus; if (cancelableJobStatus.isCanceled()) { cancelableEvent.cancel(); } if (cancelableEvent.isCanceled()) { cancelableJobStatus.cancel(); } } } } XClassDeletingListener(); }
XClassDeletingListener extends AbstractEventListener { @Override public void onEvent(Event event, Object source, Object data) { Job job = (Job) source; CancelableEvent cancelableEvent = (CancelableEvent) event; XClassBreakingQuestion question = this.buildQuestion(job, cancelableEvent, data); if (question == null) { return; } if (!question.getImpactedObjects().isEmpty()) { JobStatus jobStatus = job.getStatus(); try { if (this.documentAccessBridge.isAdvancedUser(job.getRequest().getProperty("user.reference"))) { question.unselectAll(); boolean ack = job.getStatus().ask(question, 5, TimeUnit.MINUTES); if (!ack) { String message = "The question has been asked, however no answer has been received."; this.logger.warn(message); cancelableEvent.cancel(message); } } else { question.setRefactoringForbidden(true); boolean ack = jobStatus.ask(question, 1, TimeUnit.MINUTES); if (!ack) { String message = "The question has been canceled because this refactoring is forbidden."; cancelableEvent.cancel(message); } } } catch (InterruptedException e) { this.logger.warn("Confirm question has been interrupted."); cancelableEvent.cancel("Question has been interrupted."); } if (jobStatus instanceof CancelableJobStatus) { CancelableJobStatus cancelableJobStatus = (CancelableJobStatus) jobStatus; if (cancelableJobStatus.isCanceled()) { cancelableEvent.cancel(); } if (cancelableEvent.isCanceled()) { cancelableJobStatus.cancel(); } } } } XClassDeletingListener(); @Override void onEvent(Event event, Object source, Object data); }
XClassDeletingListener extends AbstractEventListener { @Override public void onEvent(Event event, Object source, Object data) { Job job = (Job) source; CancelableEvent cancelableEvent = (CancelableEvent) event; XClassBreakingQuestion question = this.buildQuestion(job, cancelableEvent, data); if (question == null) { return; } if (!question.getImpactedObjects().isEmpty()) { JobStatus jobStatus = job.getStatus(); try { if (this.documentAccessBridge.isAdvancedUser(job.getRequest().getProperty("user.reference"))) { question.unselectAll(); boolean ack = job.getStatus().ask(question, 5, TimeUnit.MINUTES); if (!ack) { String message = "The question has been asked, however no answer has been received."; this.logger.warn(message); cancelableEvent.cancel(message); } } else { question.setRefactoringForbidden(true); boolean ack = jobStatus.ask(question, 1, TimeUnit.MINUTES); if (!ack) { String message = "The question has been canceled because this refactoring is forbidden."; cancelableEvent.cancel(message); } } } catch (InterruptedException e) { this.logger.warn("Confirm question has been interrupted."); cancelableEvent.cancel("Question has been interrupted."); } if (jobStatus instanceof CancelableJobStatus) { CancelableJobStatus cancelableJobStatus = (CancelableJobStatus) jobStatus; if (cancelableJobStatus.isCanceled()) { cancelableEvent.cancel(); } if (cancelableEvent.isCanceled()) { cancelableJobStatus.cancel(); } } } } XClassDeletingListener(); @Override void onEvent(Event event, Object source, Object data);  }