Backend support for operation milestones with activity inputs
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / tosca / ComponentCacheTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2020 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.be.tosca;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25 import static org.junit.Assert.fail;
26 import static org.openecomp.sdc.be.components.impl.utils.TestDataUtils.aUniqueId;
27 import static org.openecomp.sdc.be.components.impl.utils.TestDataUtils.alphaNum;
28 import static org.openecomp.sdc.be.tosca.ComponentCache.entry;
29
30 import io.vavr.collection.List;
31 import java.util.function.BinaryOperator;
32 import org.junit.jupiter.api.Test;
33 import org.openecomp.sdc.be.components.impl.utils.TestDataUtils;
34 import org.openecomp.sdc.be.model.Component;
35 import org.openecomp.sdc.be.tosca.ComponentCache.CacheEntry;
36
37 public class ComponentCacheTest {
38     private static final BinaryOperator<CacheEntry> RETURN_LATEST = (oldValue, newValue) -> newValue;
39     private static final BinaryOperator<CacheEntry> NO_MERGE = (oldValue, newValue) ->  {
40         fail("Should not merge");
41         return oldValue;
42     };
43
44     @Test
45     public void emptyCase() {
46         ComponentCache cache = ComponentCache.overwritable(NO_MERGE);
47         List<CacheEntry> actual = cache.all().toList();
48         assertTrue(actual.isEmpty());
49     }
50
51     @Test
52     public void emptyCacheShouldNotMerge() {
53         ComponentCache cache = ComponentCache.overwritable(NO_MERGE);
54
55         String id = alphaNum(10);
56         String fileName = alphaNum(10);
57         Component component = aComponent();
58
59         List<CacheEntry> actual = cache
60             .put(id, fileName, component)
61             .all().toList();
62
63         List<CacheEntry> expected = List.of(entry(id, fileName, component));
64         assertEquals(expected, actual);
65     }
66
67     @Test
68     public void nonEmptyCacheShouldMerge() {
69         ComponentCache cache = ComponentCache.overwritable(RETURN_LATEST);
70
71         String id = alphaNum(10);
72         String fileName = alphaNum(10);
73         String invariantId = aUniqueId();
74
75         Component oldComponent = aComponent(invariantId);
76         Component newComponent = aComponent(invariantId);
77
78         CacheEntry actual = cache
79             .put(id, fileName, oldComponent)
80             .put(id, fileName, newComponent)
81             .all().toList().head();
82
83         assertEquals(newComponent, actual.component);
84         assertEquals(1, cache.all().size());
85     }
86
87     private static Component aComponent() {
88         return aComponent(aUniqueId());
89     }
90
91     private static Component aComponent(String invariantId) {
92         Component component = TestDataUtils.aComponent();
93         component.setInvariantUUID(invariantId);
94         return component;
95     }
96 }
97