23bbc7c00cf8ee16f2d2f7e00293fabda03c64e1
[ccsdk/apps.git] / ms / neng / src / test / java / org / onap / ccsdk / apps / ms / neng / core / seq / SequenceGeneratorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : CCSDK.apps
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.ccsdk.apps.ms.neng.core.seq;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.Mockito.mock;
25
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.InjectMocks;
29 import org.mockito.Mock;
30 import org.mockito.Mockito;
31 import org.mockito.runners.MockitoJUnitRunner;
32 import org.onap.ccsdk.apps.ms.neng.core.policy.PolicySequence;
33 import org.onap.ccsdk.apps.ms.neng.persistence.entity.ServiceParameter;
34 import org.onap.ccsdk.apps.ms.neng.persistence.repository.GeneratedNameRespository;
35 import org.onap.ccsdk.apps.ms.neng.persistence.repository.ServiceParameterRepository;
36
37
38 @RunWith(MockitoJUnitRunner.class)
39 public class SequenceGeneratorTest {
40     @Mock
41     private GeneratedNameRespository genNameRepo = mock(GeneratedNameRespository.class);
42     @Mock
43     private ServiceParameterRepository servParamRepo = mock(ServiceParameterRepository.class);
44     @Mock
45     private PolicySequence params = mock(PolicySequence.class);
46     @Mock
47     private ServiceParameter sp = mock(ServiceParameter.class);
48     @InjectMocks
49     SequenceGenerator sg;
50
51     @Test
52     public void testGenerate() throws Exception {
53         assertEquals(0, sg.generate("zSSRX1234", null, params, null, 1));
54         
55         Mockito.when(params.getLastReleaseSeqNumTried()).thenReturn(null);
56         Mockito.when(genNameRepo.findMaxByPrefixAndSuffix("zSSRX1234", null)).thenReturn("4");
57
58         assertEquals(0, sg.generate("zSSRX1234", null, params, null, 1));
59
60         Mockito.when(genNameRepo.findMaxByPrefixAndSuffix("zSSRX1234", null)).thenReturn("2");
61         Mockito.when(genNameRepo.findMaxByPrefixAndSuffix("zSSRX1234", null)).thenReturn(null);
62         Mockito.when(servParamRepo.findByName("initial_increment")).thenReturn(sp);
63         Mockito.when(sp.getValue()).thenReturn("1");
64
65         assertEquals(0, sg.generate("zSSRX1234", null, params, 1L, 2));
66
67         Mockito.when(genNameRepo.findNextReleasedSeq(0L, "zSSRX1234", null)).thenReturn(null);
68         assertEquals(0, sg.generate("zSSRX1234", null, params, null, 1));
69     }
70
71     @Test(expected = Exception.class)
72     public void exceltionTest() throws Exception {
73         Mockito.when(genNameRepo.findNextReleasedSeq(1L, "zSSRX1234", null)).thenReturn(null);
74         Mockito.when(params.getLastReleaseSeqNumTried()).thenReturn(1L);
75         sg.generate("zSSRX1234", null, params, null, 1);
76     }
77
78     @Test
79     public void testAlreadyUsedSequesnce() throws Exception {
80         Mockito.when(genNameRepo.findMaxByPrefixAndSuffix("zSSRX1234", null)).thenReturn("1");
81         Mockito.when(sp.getValue()).thenReturn("4");
82         Mockito.when(params.getIncrement()).thenReturn(2L);
83         assertEquals(0L, sg.generate("zSSRX1234", null, params, 2L, 0));
84     }
85 }