1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
/*
* 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.technicaldebt.batch.internal.DefaultCharacteristic;
import org.sonar.api.utils.ValidationMessages;
import org.sonar.core.persistence.MyBatis;
import org.sonar.core.technicaldebt.DefaultTechnicalDebtModel;
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 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;
private DefaultTechnicalDebtModel defaultModel;
private DebtModelSynchronizer manager;
@Before
public void initAndMerge() throws Exception {
when(myBatis.openSession()).thenReturn(session);
defaultModel = new DefaultTechnicalDebtModel();
Reader defaultModelReader = mock(Reader.class);
when(technicalDebtModelRepository.createReaderForXMLFile("technical-debt")).thenReturn(defaultModelReader);
when(xmlImporter.importXML(eq(defaultModelReader), any(ValidationMessages.class))).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 {
DefaultCharacteristic rootCharacteristic = new DefaultCharacteristic().setKey("PORTABILITY");
new DefaultCharacteristic().setKey("COMPILER_RELATED_PORTABILITY").setParent(rootCharacteristic);
defaultModel.addRootCharacteristic(rootCharacteristic);
when(technicalDebtModelRepository.getContributingPluginList()).thenReturn(Collections.<String>emptyList());
when(dao.selectEnabledCharacteristics()).thenReturn(Lists.<CharacteristicDto>newArrayList());
manager.synchronize(ValidationMessages.create());
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 {
DefaultCharacteristic rootCharacteristic = new DefaultCharacteristic().setKey("PORTABILITY");
new DefaultCharacteristic().setKey("COMPILER_RELATED_PORTABILITY").setParent(rootCharacteristic);
defaultModel.addRootCharacteristic(rootCharacteristic);
when(technicalDebtModelRepository.getContributingPluginList()).thenReturn(Collections.<String>emptyList());
when(dao.selectEnabledCharacteristics()).thenReturn(Lists.newArrayList(new CharacteristicDto()));
manager.synchronize(ValidationMessages.create());
verify(dao, never()).insert(any(CharacteristicDto.class), eq(session));
}
}
|