@Test public void delete_documents_indexes() throws Exception { insertNewProjectInIndexes(1); insertNewProjectInIndexes(2); newRequest().setParam(PARAM_KEY, "project-key-1").execute(); String remainingProjectUuid = "project-uuid-2"; assertThat( es.getDocumentFieldValues( IssueIndexDefinition.INDEX, IssueIndexDefinition.TYPE_ISSUE, IssueIndexDefinition.FIELD_ISSUE_PROJECT_UUID)) .containsOnly(remainingProjectUuid); assertThat( es.getDocumentFieldValues( IssueIndexDefinition.INDEX, IssueIndexDefinition.TYPE_AUTHORIZATION, IssueIndexDefinition.FIELD_AUTHORIZATION_PROJECT_UUID)) .containsOnly(remainingProjectUuid); assertThat( es.getDocumentFieldValues( TestIndexDefinition.INDEX, TestIndexDefinition.TYPE, TestIndexDefinition.FIELD_PROJECT_UUID)) .containsOnly(remainingProjectUuid); }
@Before public void setUp() { es.truncateIndices(); db.truncateTables(); underTest = new TestIndexer(new DbClient(db.database(), db.myBatis()), es.client()); underTest.setEnabled(true); }
@Test public void does_nothing_when_deleting_unknown_project() throws Exception { ComponentDto project = newProjectDto(); componentDbTester.insertProjectAndSnapshot(project); underTest.index(); authorizationIndexerTester.indexProjectPermission(project.uuid(), emptyList(), emptyList()); underTest.deleteProject("UNKNOWN"); assertThat(esTester.getIds(INDEX_PROJECT_MEASURES, TYPE_PROJECT_MEASURES)) .containsOnly(project.uuid()); assertThat(esTester.getIds(INDEX_PROJECT_MEASURES, TYPE_AUTHORIZATION)) .containsOnly(project.uuid()); }
@Test public void delete_views() throws Exception { ViewDoc view1 = new ViewDoc().setUuid("UUID1").setProjects(asList("P1")); ViewDoc view2 = new ViewDoc().setUuid("UUID2").setProjects(asList("P2", "P3", "P4")); ViewDoc view3 = new ViewDoc().setUuid("UUID3").setProjects(asList("P2", "P3", "P4")); esTester.putDocuments(INDEX, TYPE_VIEW, view1); esTester.putDocuments(INDEX, TYPE_VIEW, view2); esTester.putDocuments(INDEX, TYPE_VIEW, view3); index.delete(asList(view1.uuid(), view2.uuid())); assertThat(esTester.getDocumentFieldValues(INDEX, TYPE_VIEW, ViewIndexDefinition.FIELD_UUID)) .containsOnly(view3.uuid()); }
@Test public void no_trace_logs() { logTester.setLevel(LoggerLevel.DEBUG); SearchResponse response = esTester .client() .prepareSearch(FakeIndexDefinition.INDEX) .setSearchType(SearchType.SCAN) .setScroll(TimeValue.timeValueMinutes(1)) .get(); logTester.clear(); esTester.client().prepareSearchScroll(response.getScrollId()).get(); assertThat(logTester.logs()).isEmpty(); }
@Test public void list_based_on_source_file_key_and_line_number() throws Exception { String sourceFileUuid = "MAIN-FILE-UUID"; String sourceFileKey = "MAIN-FILE-KEY"; userSessionRule.addProjectUuidPermissions(UserRole.CODEVIEWER, TestFile1.PROJECT_UUID); dbClient .componentDao() .insert( db.getSession(), TestFile1.dto(), TestFile2.dto(), new ComponentDto() .setUuid(sourceFileUuid) .setUuidPath(TestFile1.PROJECT_UUID + "." + sourceFileUuid + ".") .setRootUuid(TestFile1.PROJECT_UUID) .setKey(sourceFileKey) .setProjectUuid(TestFile1.PROJECT_UUID)); db.getSession().commit(); es.putDocuments( TestIndexDefinition.INDEX, TestIndexDefinition.TYPE, TestFile1.doc(), TestFile2.doc()); WsTester.TestRequest request = ws.newGetRequest("api/tests", "list") .setParam(ListAction.SOURCE_FILE_KEY, sourceFileKey) .setParam(ListAction.SOURCE_FILE_LINE_NUMBER, "10"); request.execute().assertJson(getClass(), "list-main-file.json"); }
@Test public void delete_views() throws Exception { esTester.putDocuments( ViewIndexDefinition.INDEX, ViewIndexDefinition.TYPE_VIEW, this.getClass(), "view1.json", "view2.json"); index.delete( newArrayList( "fed0a543-9d9c-4af5-a4ec-450a8fe78ce7", "8d0bc2a5-bfba-464b-92de-bb170e9d978e")); assertThat(esTester.countDocuments(ViewIndexDefinition.INDEX, ViewIndexDefinition.TYPE_VIEW)) .isEqualTo(0L); }
@Test public void stats_for_all() { indexRules(newDoc(RULE_KEY_1), newDoc(RULE_KEY_2)); ActiveRuleKey activeRuleKey1 = ActiveRuleKey.of(QUALITY_PROFILE_KEY1, RULE_KEY_1); ActiveRuleKey activeRuleKey2 = ActiveRuleKey.of(QUALITY_PROFILE_KEY1, RULE_KEY_2); indexActiveRules( ActiveRuleDocTesting.newDoc(activeRuleKey1).setSeverity(BLOCKER), ActiveRuleDocTesting.newDoc(activeRuleKey2).setSeverity(MINOR), // Profile 2 is a child a profile 1 ActiveRuleDocTesting.newDoc(ActiveRuleKey.of(QUALITY_PROFILE_KEY2, RULE_KEY_1)) .setSeverity(MAJOR) .setInheritance(INHERITED.name()), ActiveRuleDocTesting.newDoc(ActiveRuleKey.of(QUALITY_PROFILE_KEY2, RULE_KEY_2)) .setSeverity(BLOCKER) .setInheritance(OVERRIDES.name())); // 0. Test base case assertThat(tester.countDocuments(INDEX, TYPE_ACTIVE_RULE)).isEqualTo(4); // 1. Assert by term aggregation; Map<String, Multimap<String, FacetValue>> stats = index.getStatsByProfileKeys(ImmutableList.of(QUALITY_PROFILE_KEY1, QUALITY_PROFILE_KEY2)); assertThat(stats).hasSize(2); }
@Test public void getNullableByTestUuid() throws Exception { es.putDocuments( TestIndexDefinition.INDEX, TestIndexDefinition.TYPE, newTestDoc( "1", "TESTFILE1", newCoveredFileDoc("3"), newCoveredFileDoc("4"), newCoveredFileDoc("5")), newTestDoc( "2", "TESTFILE1", newCoveredFileDoc("5"), newCoveredFileDoc("6"), newCoveredFileDoc("7"))); Optional<TestDoc> result = underTest.getNullableByTestUuid("1"); assertThat(result).isPresent(); TestDoc test = result.get(); assertThat(test.testUuid()).isEqualTo("1"); assertThat(test.fileUuid()).isEqualTo("TESTFILE1"); assertThat(test.name()).isEqualTo("name-1"); assertThat(test.durationInMs()).isEqualTo(1L); assertThat(test.status()).isEqualTo("status-1"); assertThat(test.message()).isEqualTo("message-1"); assertThat(test.coveredFiles()).hasSize(3); assertThat(test.coveredFiles()) .extractingResultOf("fileUuid") .containsOnly("main-uuid-3", "main-uuid-4", "main-uuid-5"); }
@Test public void searchBySourceFileUuidAndLineNumber() throws Exception { es.putDocuments( TestIndexDefinition.INDEX, TestIndexDefinition.TYPE, newTestDoc( "1", "TESTFILE1", newCoveredFileDoc("10"), newCoveredFileDoc("11"), newCoveredFileDoc("12")), newTestDoc( "2", "TESTFILE1", newCoveredFileDoc("3"), newCoveredFileDoc("4"), newCoveredFileDoc("5")), newTestDoc( "3", "TESTFILE1", newCoveredFileDoc("5"), newCoveredFileDoc("6"), newCoveredFileDoc("7"))); List<TestDoc> result = underTest.searchBySourceFileUuidAndLineNumber("main-uuid-5", 82, searchOptions()).getDocs(); assertThat(result).hasSize(2); assertThat(result).extractingResultOf("name").containsOnly("name-2", "name-3"); }
@Test public void coveredFiles() throws Exception { es.putDocuments( TestIndexDefinition.INDEX, TestIndexDefinition.TYPE, newTestDoc( "1", "TESTFILE1", newCoveredFileDoc("3"), newCoveredFileDoc("4"), newCoveredFileDoc("5")), newTestDoc( "2", "TESTFILE1", newCoveredFileDoc("5"), newCoveredFileDoc("6"), newCoveredFileDoc("7"))); List<CoveredFileDoc> result = underTest.coveredFiles("1"); assertThat(result).hasSize(3); assertThat(result) .extractingResultOf("fileUuid") .containsOnly("main-uuid-3", "main-uuid-4", "main-uuid-5"); assertThat(result.get(0).coveredLines()).containsOnly(25, 33, 82); }
@Test public void insert_and_index() { when(system.now()).thenReturn(1_500_000_000_000L); Activity activity = new Activity(); activity.setType(Activity.Type.ANALYSIS_REPORT); activity.setAction("THE_ACTION"); activity.setMessage("THE_MSG"); activity.setData("foo", "bar"); activity.setProfileKey("PROFILE_KEY"); service.save(activity); Map<String, Object> dbMap = db.selectFirst( "select " + " log_type as \"type\", " + " log_action as \"action\", " + " log_message as \"msg\", " + " data_field as \"data\", " + " profile_key as \"profileKey\" " + "from activities"); assertThat(dbMap).containsEntry("type", "ANALYSIS_REPORT"); assertThat(dbMap).containsEntry("action", "THE_ACTION"); assertThat(dbMap).containsEntry("msg", "THE_MSG"); assertThat(dbMap).containsEntry("profileKey", "PROFILE_KEY"); assertThat(dbMap.get("data")).isEqualTo("foo=bar"); List<ActivityDoc> docs = es.getDocuments("activities", "activity", ActivityDoc.class); assertThat(docs).hasSize(1); assertThat(docs.get(0).getKey()).isNotEmpty(); assertThat(docs.get(0).getAction()).isEqualTo("THE_ACTION"); assertThat(docs.get(0).getMessage()).isEqualTo("THE_MSG"); assertThat(docs.get(0).getDetails()) .containsOnly(MapEntry.entry("foo", "bar"), MapEntry.entry("profileKey", "PROFILE_KEY")); }
@Before public void before() { DbClient dbClient = db.getDbClient(); ActivityIndexer indexer = new ActivityIndexer(dbClient, es.client()); // indexers are disabled by default indexer.setEnabled(true); service = new ActivityService(dbClient, indexer, userSession); }
@Test public void get_with_time_value_timeout_is_not_yet_implemented() { try { esTester.client().prepareSearchScroll("scrollId").get(TimeValue.timeValueMinutes(1)); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); } }
@Test public void index_projects_even_when_no_analysis() { ComponentDto project = componentDbTester.insertProject(); underTest.index(); assertThat(esTester.getIds(INDEX_PROJECT_MEASURES, TYPE_PROJECT_MEASURES)) .containsOnly(project.uuid()); }
@Before public void setUp() { testIndex = new TestIndex(es.client()); ws = new WsTester( new TestsWs( new ListAction( dbClient, testIndex, userSessionRule, new ComponentFinder(dbClient)))); }
@Test public void get_with_string_timeout_is_not_yet_implemented() { try { esTester.client().prepareHealth().get("1"); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class).hasMessage("Not yet implemented"); } }
@Test public void index_all_project() { componentDbTester.insertProjectAndSnapshot(newProjectDto()); componentDbTester.insertProjectAndSnapshot(newProjectDto()); componentDbTester.insertProjectAndSnapshot(newProjectDto()); underTest.index(); assertThat(esTester.countDocuments(INDEX_PROJECT_MEASURES, TYPE_PROJECT_MEASURES)).isEqualTo(3); }
@Test public void trace_logs() { logTester.setLevel(LoggerLevel.TRACE); ClusterHealthRequestBuilder requestBuilder = esTester.client().prepareHealth(); ClusterHealthResponse state = requestBuilder.get(); assertThat(state.getStatus()).isEqualTo(ClusterHealthStatus.GREEN); assertThat(logTester.logs()).hasSize(1); }
@Test public void fail_to_search_bad_query() { try { esTester.client().prepareSearchScroll("unknown").get(); fail(); } catch (Exception e) { assertThat(e).isInstanceOf(IllegalStateException.class); assertThat(e.getMessage()) .contains("Fail to execute ES search scroll request for scroll id 'null'"); } }
@Test public void index_one_project() throws Exception { ComponentDto project = newProjectDto(); componentDbTester.insertProjectAndSnapshot(project); componentDbTester.insertProjectAndSnapshot(newProjectDto()); underTest.index(project.uuid()); assertThat(esTester.getIds(INDEX_PROJECT_MEASURES, TYPE_PROJECT_MEASURES)) .containsOnly(project.uuid()); }
@Test public void execute_should_throw_an_unsupported_operation_exception() { try { esTester.client().prepareSearchScroll("scrollId").execute(); fail(); } catch (Exception e) { assertThat(e) .isInstanceOf(UnsupportedOperationException.class) .hasMessage("execute() should not be called as it's used for asynchronous"); } }
@Test public void delete_project() { ComponentDto project1 = newProjectDto(); componentDbTester.insertProjectAndSnapshot(project1); ComponentDto project2 = newProjectDto(); componentDbTester.insertProjectAndSnapshot(project2); ComponentDto project3 = newProjectDto(); componentDbTester.insertProjectAndSnapshot(project3); underTest.index(); authorizationIndexerTester.indexProjectPermission(project1.uuid(), emptyList(), emptyList()); authorizationIndexerTester.indexProjectPermission(project2.uuid(), emptyList(), emptyList()); authorizationIndexerTester.indexProjectPermission(project3.uuid(), emptyList(), emptyList()); underTest.deleteProject(project1.uuid()); assertThat(esTester.getIds(INDEX_PROJECT_MEASURES, TYPE_PROJECT_MEASURES)) .containsOnly(project2.uuid(), project3.uuid()); assertThat(esTester.getIds(INDEX_PROJECT_MEASURES, TYPE_AUTHORIZATION)) .containsOnly(project2.uuid(), project3.uuid()); }
@Test public void find_all_view_uuids() throws Exception { ViewDoc view1 = new ViewDoc().setUuid("UUID1").setProjects(asList("P1")); ViewDoc view2 = new ViewDoc().setUuid("UUID2").setProjects(asList("P2")); esTester.putDocuments(INDEX, TYPE_VIEW, view1); esTester.putDocuments(INDEX, TYPE_VIEW, view2); List<String> result = newArrayList(index.findAllViewUuids()); assertThat(result).containsOnly(view1.uuid(), view2.uuid()); }
private void indexTest(String projectUuid, String fileUuid, String testName, String uuid) throws IOException { es.client() .prepareIndex(INDEX, TYPE) .setId(uuid) .setSource( FileUtils.readFileToString( TestUtils.getResource( this.getClass(), projectUuid + "_" + fileUuid + "_" + testName + ".json"))) .setRefresh(true) .get(); }
@Before public void setUp() { resourceType = mock(ResourceType.class); when(resourceType.getBooleanProperty(anyString())).thenReturn(true); ResourceTypes mockResourceTypes = mock(ResourceTypes.class); when(mockResourceTypes.get(anyString())).thenReturn(resourceType); ws = new WsTester( new ProjectsWs( new DeleteAction( new ComponentCleanerService( dbClient, new IssueAuthorizationIndexer(dbClient, es.client()), new IssueIndexer(dbClient, es.client()), new TestIndexer(dbClient, es.client()), mockResourceTypes, new ComponentFinder(dbClient)), new ComponentFinder(dbClient), dbClient, userSessionRule))); userSessionRule.login("login").setGlobalPermissions(UserRole.ADMIN); }
@Test public void list_based_on_test_file_key() throws Exception { userSessionRule.addComponentPermission( UserRole.CODEVIEWER, TestFile1.PROJECT_UUID, TestFile1.KEY); dbClient.componentDao().insert(db.getSession(), TestFile1.dto()); db.getSession().commit(); es.putDocuments(TestIndexDefinition.INDEX, TestIndexDefinition.TYPE, TestFile1.doc()); WsTester.TestRequest request = ws.newGetRequest("api/tests", "list").setParam(ListAction.TEST_FILE_KEY, TestFile1.KEY); request.execute().assertJson(getClass(), "list-test-uuid.json"); }
private void insertNewProjectInIndexes(int id) throws Exception { String suffix = String.valueOf(id); ComponentDto project = ComponentTesting.newProjectDto("project-uuid-" + suffix).setKey("project-key-" + suffix); dbClient.componentDao().insert(dbSession, project); dbSession.commit(); es.putDocuments( IssueIndexDefinition.INDEX, IssueIndexDefinition.TYPE_ISSUE, IssueTesting.newDoc("issue-key-" + suffix, project)); es.putDocuments( IssueIndexDefinition.INDEX, IssueIndexDefinition.TYPE_AUTHORIZATION, new IssueAuthorizationDoc().setProjectUuid(project.uuid())); TestDoc testDoc = new TestDoc() .setUuid("test-uuid-" + suffix) .setProjectUuid(project.uuid()) .setFileUuid(project.uuid()); es.putDocuments(TestIndexDefinition.INDEX, TestIndexDefinition.TYPE, testDoc); }
@Test public void count_all_by_quality_profile_key() { indexRules(RuleDocTesting.newDoc(RULE_KEY_1)); indexActiveRules( ActiveRuleDocTesting.newDoc(ActiveRuleKey.of(QUALITY_PROFILE_KEY1, RULE_KEY_1)), ActiveRuleDocTesting.newDoc(ActiveRuleKey.of(QUALITY_PROFILE_KEY2, RULE_KEY_1))); // 0. Test base case assertThat(tester.countDocuments(INDEX, TYPE_ACTIVE_RULE)).isEqualTo(2); // 1. Assert by term aggregation; assertThat(index.countAllByQualityProfileKey()) .containsOnly(entry(QUALITY_PROFILE_KEY1, 1L), entry(QUALITY_PROFILE_KEY2, 1L)); }
@Test public void find_all_view_uuids() throws Exception { esTester.putDocuments( ViewIndexDefinition.INDEX, ViewIndexDefinition.TYPE_VIEW, this.getClass(), "view1.json", "view2.json"); List<String> result = newArrayList(index.findAllViewUuids()); assertThat(result) .containsOnly( "fed0a543-9d9c-4af5-a4ec-450a8fe78ce7", "8d0bc2a5-bfba-464b-92de-bb170e9d978e"); }