2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.tosca;
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;
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;
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");
45 public void emptyCase() {
46 ComponentCache cache = ComponentCache.overwritable(NO_MERGE);
47 List<CacheEntry> actual = cache.all().toList();
48 assertTrue(actual.isEmpty());
52 public void emptyCacheShouldNotMerge() {
53 ComponentCache cache = ComponentCache.overwritable(NO_MERGE);
55 String id = alphaNum(10);
56 String fileName = alphaNum(10);
57 Component component = aComponent();
59 List<CacheEntry> actual = cache
60 .put(id, fileName, component)
63 List<CacheEntry> expected = List.of(entry(id, fileName, component));
64 assertEquals(expected, actual);
68 public void nonEmptyCacheShouldMerge() {
69 ComponentCache cache = ComponentCache.overwritable(RETURN_LATEST);
71 String id = alphaNum(10);
72 String fileName = alphaNum(10);
73 String invariantId = aUniqueId();
75 Component oldComponent = aComponent(invariantId);
76 Component newComponent = aComponent(invariantId);
78 CacheEntry actual = cache
79 .put(id, fileName, oldComponent)
80 .put(id, fileName, newComponent)
81 .all().toList().head();
83 assertEquals(newComponent, actual.component);
84 assertEquals(1, cache.all().size());
87 private static Component aComponent() {
88 return aComponent(aUniqueId());
91 private static Component aComponent(String invariantId) {
92 Component component = TestDataUtils.aComponent();
93 component.setInvariantUUID(invariantId);