e32bc88b7c84414a359da9b1cdf4b4dce3ece792
[so.git] / bpmn / MSOCoreBPMN / src / test / java / org / onap / so / bpmn / core / internal / VariableNameExtractorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 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.so.bpmn.core.internal;
22 import static org.hamcrest.CoreMatchers.containsString;
23 import static org.junit.Assert.*;
24 import static org.mockito.Matchers.contains;
25
26 import java.util.Optional;
27 import org.junit.Test;
28
29 public class VariableNameExtractorTest {
30
31     @Test
32     public void shouldExtractVariableName() throws Exception {
33         // given
34         String name = "A_different_NAME123";
35         String variable = "${A_different_NAME123}";
36         VariableNameExtractor extractor = new VariableNameExtractor(variable);
37         // when
38         Optional<String> extracted = extractor.extract();
39         // then
40         assertTrue(extracted.isPresent());
41         assertThat(extracted.get(), containsString(name));
42     }
43
44     @Test
45     public void shouldExtractVariableNameFromWhitespaces() throws Exception {
46         // given
47         String name = "name";
48         String variable = " \n\t$ \n\t{ \n\tname \n\t} \n\t";
49         VariableNameExtractor extractor = new VariableNameExtractor(variable);
50         // when
51         Optional<String> extracted = extractor.extract();
52         // then
53         assertTrue(extracted.isPresent());
54         assertThat(extracted.get(), containsString(name));
55     }
56
57     @Test
58     public void shouldReturnEmptyIfThereIsMoreThanVariable() throws Exception {
59         // given
60         String variable = "a ${test}";
61         VariableNameExtractor extractor = new VariableNameExtractor(variable);
62         // when
63         Optional<String> extracted = extractor.extract();
64         // then
65         assertFalse(extracted.isPresent());
66     }
67
68     @Test
69     public void shouldReturnEmptyIfVariableNameIsIncorrect() throws Exception {
70         // given
71         String variable = "${name with space}";
72         VariableNameExtractor extractor = new VariableNameExtractor(variable);
73         // when
74         Optional<String> extracted = extractor.extract();
75         // then
76         assertFalse(extracted.isPresent());
77     }
78
79     @Test
80     public void shouldReturnEmptyIfTwoVariablesPresent() throws Exception {
81         // given
82         String variable = "${var1} ${var2}";
83         VariableNameExtractor extractor = new VariableNameExtractor(variable);
84         // when
85         Optional<String> extracted = extractor.extract();
86         // then
87         assertFalse(extracted.isPresent());
88     }
89 }