Revert "Support SIP TLS"
[sdc.git] / common-app-api / src / test / java / org / openecomp / sdc / common / util / StreamUtilsTests.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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.openecomp.sdc.common.util;
22
23 import fj.data.Either;
24 import static org.junit.Assert.assertEquals;
25 import org.junit.Test;
26
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.List;
30 import java.util.function.Function;
31 import java.util.stream.Collectors;
32 import java.util.stream.Stream;
33
34 import static org.junit.Assert.assertTrue;
35
36 public class StreamUtilsTests {
37         @Test
38         public void testTakeWhilePredicateNotMet() {
39                 List<Either<Integer, Boolean>> list = buildListWith10Integers();
40
41         assertEquals(10, StreamUtils.takeWhile(list.stream(), Either::isLeft).count());
42         }
43
44         @Test
45         public void testTakeWhilePredicateIsMet() {
46                 List<Either<Integer, Boolean>> list = buildListWith10Integers();
47                 addToBooleansToList(list);
48
49                 final Stream<Either<Integer, Boolean>> takeWhileStream = StreamUtils.takeWhile(list.stream(), Either::isLeft);
50         assertEquals(0, takeWhileStream.filter(Either::isRight).count());
51         }
52
53         @Test
54         public <T> void testTakeErrorEvalOnlyOnce() {
55                 List<Integer> bucket = new ArrayList<>();
56                 // API
57                 Function<Integer, Either<Integer, Boolean>> cons = num -> {
58                         Either<Integer, Boolean> ret;
59                         bucket.add(num);
60                         if (num > 5) {
61                                 ret = Either.right(false);
62                         } else {
63                                 ret = Either.left(num);
64                         }
65                         ;
66                         return ret;
67                 };
68
69                 List<Integer> num1to10 = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
70                 Stream<Either<Integer, Boolean>> streamEithers = num1to10.stream().map(cons::apply);
71                 List<Either<Integer, Boolean>> collect = StreamUtils.takeWhilePlusOneNoEval(streamEithers, Either::isLeft)
72                                 .collect(Collectors.toList());
73                 assertTrue(bucket.size() <= 6);
74                 assertTrue(collect.size() <= 6);
75         assertEquals(1, collect.stream().filter(Either::isRight).count());
76
77         }
78
79         @Test
80         public void testTakeWhilePlusOnePredicateNotMet() {
81                 List<Either<Integer, Boolean>> list = buildListWith10Integers();
82
83         assertEquals(10, StreamUtils.takeWhilePlusOne(list.stream(), Either::isLeft).count());
84         }
85
86         @Test
87         public void testTakeWhilePlusOnePredicateIsMet() {
88                 List<Either<Integer, Boolean>> list = buildListWith10Integers();
89                 addToBooleansToList(list);
90
91                 final Stream<Either<Integer, Boolean>> takeWhilePlusOneStream = StreamUtils.takeWhilePlusOne(list.stream(),
92                 Either::isLeft);
93         assertEquals(1, takeWhilePlusOneStream.filter(Either::isRight).count());
94         }
95
96         private void addToBooleansToList(List<Either<Integer, Boolean>> list) {
97                 list.add(Either.right(false));
98                 list.add(Either.right(false));
99         }
100
101         private List<Either<Integer, Boolean>> buildListWith10Integers() {
102                 List<Either<Integer, Boolean>> list = new ArrayList<>();
103                 for (int i = 0; i < 10; i++) {
104                         list.add(Either.left(i));
105                 }
106                 return list;
107         }
108
109         @Test
110         public void myTest() {
111                 List<Integer> list = new ArrayList<>();
112                 for (int i = 0; i < 10; i++) {
113                         list.add(i);
114                 }
115
116                 List<Either<Integer, Boolean>> container = new ArrayList<>();
117                 list.stream().map(e -> myBusinessLogic(e, container)).filter(Either::isRight).findAny();
118                 // Actual Results are in container
119         assertEquals(6, container.size());
120
121         }
122
123         private Either<Integer, Boolean> myBusinessLogic(int e, List<Either<Integer, Boolean>> cobtainerList) {
124                 Either<Integer, Boolean> eitherElement = similuteDBAccess(e);
125                 // Keep The results in external List
126                 cobtainerList.add(eitherElement);
127
128                 return eitherElement;
129         }
130
131         private Either<Integer, Boolean> similuteDBAccess(int e) {
132                 Either<Integer, Boolean> eitherElement;
133                 if (e < 5) {
134                         // DB Success
135                         eitherElement = Either.left(e);
136                 } else {
137                         // DB Fail
138                         eitherElement = Either.right(true);
139                 }
140                 return eitherElement;
141         }
142 }