+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-
-package org.sonar.server.debt;
-
-import org.apache.commons.io.IOUtils;
-import org.apache.ibatis.session.SqlSession;
-import org.sonar.api.ServerExtension;
-import org.sonar.api.server.debt.DebtCharacteristic;
-import org.sonar.core.persistence.MyBatis;
-import org.sonar.core.technicaldebt.TechnicalDebtModelRepository;
-import org.sonar.core.technicaldebt.db.CharacteristicDao;
-import org.sonar.core.technicaldebt.db.CharacteristicDto;
-
-import javax.annotation.Nullable;
-
-import java.io.Reader;
-import java.util.List;
-
-import static com.google.common.collect.Lists.newArrayList;
-
-// TODO replace this by DebtModelRestore
-public class DebtModelSynchronizer implements ServerExtension {
-
- private final MyBatis mybatis;
- private final CharacteristicDao dao;
- private final TechnicalDebtModelRepository languageModelFinder;
- private final DebtCharacteristicsXMLImporter importer;
-
- public DebtModelSynchronizer(MyBatis mybatis, CharacteristicDao dao, TechnicalDebtModelRepository modelRepository, DebtCharacteristicsXMLImporter importer) {
- this.mybatis = mybatis;
- this.dao = dao;
- this.languageModelFinder = modelRepository;
- this.importer = importer;
- }
-
- public List<CharacteristicDto> synchronize() {
- SqlSession session = mybatis.openSession();
-
- List<CharacteristicDto> characteristics = newArrayList();
- try {
- DebtModel defaultModel = loadModelFromXml(TechnicalDebtModelRepository.DEFAULT_MODEL);
- List<CharacteristicDto> existingCharacteristics = dao.selectEnabledCharacteristics();
- if (existingCharacteristics.isEmpty()) {
- return createDebtModel(defaultModel, session);
- }
- } finally {
- MyBatis.closeQuietly(session);
- }
- return characteristics;
- }
-
- private List<CharacteristicDto> createDebtModel(DebtModel defaultModel, SqlSession session) {
- List<CharacteristicDto> characteristics = newArrayList();
- for (DebtCharacteristic rootCharacteristic : defaultModel.rootCharacteristics()) {
- CharacteristicDto rootCharacteristicDto = toDto(rootCharacteristic, null);
- dao.insert(rootCharacteristicDto, session);
- characteristics.add(rootCharacteristicDto);
- for (DebtCharacteristic characteristic : defaultModel.subCharacteristics(rootCharacteristic.key())) {
- CharacteristicDto characteristicDto = toDto(characteristic, rootCharacteristicDto.getId());
- dao.insert(characteristicDto, session);
- characteristics.add(characteristicDto);
- }
- }
- session.commit();
- return characteristics;
- }
-
- private DebtModel loadModelFromXml(String pluginKey) {
- Reader xmlFileReader = null;
- try {
- xmlFileReader = languageModelFinder.createReaderForXMLFile(pluginKey);
- return importer.importXML(xmlFileReader);
- } finally {
- IOUtils.closeQuietly(xmlFileReader);
- }
- }
-
- private static CharacteristicDto toDto(DebtCharacteristic characteristic, @Nullable Integer parentId) {
- return new CharacteristicDto()
- .setKey(characteristic.key())
- .setName(characteristic.name())
- .setOrder(characteristic.order())
- .setParentId(parentId)
- .setEnabled(true)
- .setCreatedAt(characteristic.createdAt())
- .setUpdatedAt(characteristic.updatedAt());
- }
-
-}
import org.sonar.server.permission.InternalPermissionService;
import org.sonar.server.permission.InternalPermissionTemplateService;
import org.sonar.server.permission.PermissionFinder;
-import org.sonar.server.platform.ws.SystemWs;
import org.sonar.server.platform.ws.RestartHandler;
+import org.sonar.server.platform.ws.SystemWs;
import org.sonar.server.plugins.*;
import org.sonar.server.qualitygate.QgateProjectFinder;
import org.sonar.server.qualitygate.QualityGates;
pico.addSingleton(DebtModelLookup.class);
pico.addSingleton(DebtModelRestore.class);
pico.addSingleton(TechnicalDebtModelSynchronizer.class);
- pico.addSingleton(DebtModelSynchronizer.class);
pico.addSingleton(TechnicalDebtModelRepository.class);
pico.addSingleton(TechnicalDebtXMLImporter.class);
pico.addSingleton(DebtRulesXMLImporter.class);
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.utils.TimeProfiler;
-import org.sonar.server.debt.DebtModelSynchronizer;
+import org.sonar.core.technicaldebt.db.CharacteristicDao;
+import org.sonar.server.debt.DebtModelRestore;
public class RegisterDebtModel {
private static final Logger LOGGER = LoggerFactory.getLogger(RegisterDebtModel.class);
- private final DebtModelSynchronizer manager;
+ private final CharacteristicDao dao;
+ private final DebtModelRestore debtModelRestore;
- public RegisterDebtModel(DebtModelSynchronizer manager) {
- this.manager = manager;
+ public RegisterDebtModel(CharacteristicDao dao, DebtModelRestore debtModelRestore) {
+ this.dao = dao;
+ this.debtModelRestore = debtModelRestore;
}
public void start() {
TimeProfiler profiler = new TimeProfiler(LOGGER).start("Register technical debt model");
- manager.synchronize();
+ if (dao.selectEnabledCharacteristics().isEmpty()) {
+ debtModelRestore.restore();
+ }
profiler.stop();
}
+++ /dev/null
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * SonarQube is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-
-package org.sonar.server.debt;
-
-import com.google.common.collect.Lists;
-import org.apache.ibatis.session.SqlSession;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.ArgumentCaptor;
-import org.mockito.Mock;
-import org.mockito.invocation.InvocationOnMock;
-import org.mockito.runners.MockitoJUnitRunner;
-import org.mockito.stubbing.Answer;
-import org.sonar.api.server.debt.DebtCharacteristic;
-import org.sonar.api.server.debt.internal.DefaultDebtCharacteristic;
-import org.sonar.core.persistence.MyBatis;
-import org.sonar.core.technicaldebt.TechnicalDebtModelRepository;
-import org.sonar.core.technicaldebt.db.CharacteristicDao;
-import org.sonar.core.technicaldebt.db.CharacteristicDto;
-
-import java.io.Reader;
-import java.util.Collections;
-import java.util.List;
-
-import static com.google.common.collect.Lists.newArrayList;
-import static org.fest.assertions.Assertions.assertThat;
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.*;
-
-@RunWith(MockitoJUnitRunner.class)
-public class DebtModelSynchronizerTest {
-
- @Mock
- MyBatis myBatis;
-
- @Mock
- SqlSession session;
-
- @Mock
- TechnicalDebtModelRepository technicalDebtModelRepository;
-
- @Mock
- CharacteristicDao dao;
-
- @Mock
- DebtCharacteristicsXMLImporter xmlImporter;
-
- Integer currentId = 1;
-
- DebtModel defaultModel = new DebtModel();
-
- DebtModelSynchronizer manager;
-
- @Before
- public void initAndMerge() throws Exception {
- when(myBatis.openSession()).thenReturn(session);
-
- Reader defaultModelReader = mock(Reader.class);
- when(technicalDebtModelRepository.createReaderForXMLFile("technical-debt")).thenReturn(defaultModelReader);
- when(xmlImporter.importXML(eq(defaultModelReader))).thenReturn(defaultModel);
-
- doAnswer(new Answer() {
- public Object answer(InvocationOnMock invocation) {
- Object[] args = invocation.getArguments();
- CharacteristicDto dto = (CharacteristicDto) args[0];
- dto.setId(currentId++);
- return null;
- }
- }).when(dao).insert(any(CharacteristicDto.class), any(SqlSession.class));
-
-
- manager = new DebtModelSynchronizer(myBatis, dao, technicalDebtModelRepository, xmlImporter);
- }
-
- @Test
- public void create_default_model_on_first_execution_when_no_plugin() throws Exception {
- DebtCharacteristic characteristic = new DefaultDebtCharacteristic().setKey("PORTABILITY");
- DebtCharacteristic subCharacteristic = new DefaultDebtCharacteristic().setKey("COMPILER_RELATED_PORTABILITY");
- defaultModel.addRootCharacteristic(characteristic);
- defaultModel.addSubCharacteristic(subCharacteristic, "PORTABILITY");
-
- when(technicalDebtModelRepository.getContributingPluginList()).thenReturn(Collections.<String>emptyList());
- when(dao.selectEnabledCharacteristics()).thenReturn(Lists.<CharacteristicDto>newArrayList());
-
- manager.synchronize();
-
- verify(dao).selectEnabledCharacteristics();
- ArgumentCaptor<CharacteristicDto> characteristicCaptor = ArgumentCaptor.forClass(CharacteristicDto.class);
- verify(dao, times(2)).insert(characteristicCaptor.capture(), eq(session));
-
- List<CharacteristicDto> result = characteristicCaptor.getAllValues();
- assertThat(result.get(0).getKey()).isEqualTo("PORTABILITY");
- assertThat(result.get(1).getKey()).isEqualTo("COMPILER_RELATED_PORTABILITY");
- verifyNoMoreInteractions(dao);
- }
-
- @Test
- public void not_create_default_model_if_already_exists() throws Exception {
- DebtCharacteristic characteristic = new DefaultDebtCharacteristic().setKey("PORTABILITY");
- DebtCharacteristic subCharacteristic = new DefaultDebtCharacteristic().setKey("COMPILER_RELATED_PORTABILITY");
- defaultModel.addRootCharacteristic(characteristic);
- defaultModel.addSubCharacteristic(subCharacteristic, "PORTABILITY");
-
- when(technicalDebtModelRepository.getContributingPluginList()).thenReturn(Collections.<String>emptyList());
- when(dao.selectEnabledCharacteristics()).thenReturn(newArrayList(new CharacteristicDto()));
-
- manager.synchronize();
-
- verify(dao, never()).insert(any(CharacteristicDto.class), eq(session));
- }
-
-}
package org.sonar.server.startup;
+import org.junit.Before;
import org.junit.Test;
-import org.sonar.server.debt.DebtModelSynchronizer;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.sonar.core.technicaldebt.db.CharacteristicDao;
+import org.sonar.core.technicaldebt.db.CharacteristicDto;
+import org.sonar.server.debt.DebtModelRestore;
+import java.util.Collections;
+
+import static com.google.common.collect.Lists.newArrayList;
import static org.mockito.Mockito.*;
+@RunWith(MockitoJUnitRunner.class)
public class RegisterDebtModelTest {
+ @Mock
+ CharacteristicDao dao;
+
+ @Mock
+ DebtModelRestore debtModelRestore;
+
+ RegisterDebtModel registerDebtModel;
+
+ @Before
+ public void setUp() throws Exception {
+ registerDebtModel = new RegisterDebtModel(dao, debtModelRestore);
+ }
+
+ @Test
+ public void create_debt_model() throws Exception {
+ when(dao.selectEnabledCharacteristics()).thenReturn(Collections.<CharacteristicDto>emptyList());
+
+ registerDebtModel.start();
+
+ verify(debtModelRestore).restore();
+ }
+
@Test
- public void create_model() throws Exception {
- DebtModelSynchronizer synchronizer = mock(DebtModelSynchronizer.class);
- RegisterDebtModel sqaleDefinition = new RegisterDebtModel(synchronizer);
+ public void not_create_debt_model_if_already_exists() throws Exception {
+ when(dao.selectEnabledCharacteristics()).thenReturn(newArrayList(new CharacteristicDto()));
- sqaleDefinition.start();
+ registerDebtModel.start();
- verify(synchronizer, times(1)).synchronize();
+ verifyZeroInteractions(debtModelRestore);
}
}