2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 Nordix Foundation.
5 * Modifications Copyright (C) 2021 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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.testsuites.integration.context.utils;
25 import java.util.HashMap;
27 import java.util.concurrent.ExecutorService;
28 import java.util.concurrent.Executors;
30 import lombok.ToString;
31 import org.onap.policy.apex.context.ContextAlbum;
32 import org.onap.policy.apex.context.ContextException;
33 import org.onap.policy.apex.context.ContextRuntimeException;
34 import org.onap.policy.apex.context.Distributor;
35 import org.onap.policy.apex.context.impl.distribution.DistributorFactory;
36 import org.onap.policy.apex.context.test.concepts.TestContextLongItem;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
38 import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel;
39 import org.onap.policy.apex.testsuites.integration.context.factory.TestContextAlbumFactory;
40 import org.onap.policy.apex.testsuites.integration.context.lock.modifier.AlbumModifier;
41 import org.onap.policy.apex.testsuites.integration.context.lock.modifier.LockType;
42 import org.onap.policy.common.utils.validation.Assertions;
45 * The Class ConfigrationProviderImpl provides configuration information for a context test back to the caller.
49 public class ConfigrationProviderImpl implements ConfigrationProvider {
51 private final String testName;
52 private final int jvmCount;
53 private final int threadCount;
54 private final int loopSize;
55 private final int albumSize;
56 private final LockType lockType;
59 * The parameterized ConfigrationProviderImpl constructor.
61 * @param testType the test type
62 * @param jvmCount the JVM count
63 * @param threadCount the thread count
64 * @param loopSize the size of loop
65 * @param albumSize the size of album
66 * @param lockType the lock type
68 public ConfigrationProviderImpl(final String testType, final int jvmCount, final int threadCount,
69 final int loopSize, final int albumSize, final int lockType) {
70 this.testName = testType;
71 this.jvmCount = jvmCount;
72 this.threadCount = threadCount;
73 this.loopSize = loopSize;
74 this.albumSize = albumSize;
75 this.lockType = LockType.getLockType(lockType);
82 public ExecutorService getExecutorService() {
83 final String name = getThreadFactoryName(jvmCount, testName);
84 final IntegrationThreadFactory threadFactory = new IntegrationThreadFactory(name);
85 return Executors.newFixedThreadPool(threadCount, threadFactory);
92 public ExecutorService getExecutorService(final String threadFactoryName, final int threadPoolSize) {
93 final IntegrationThreadFactory threadFactory = new IntegrationThreadFactory(threadFactoryName);
94 return Executors.newFixedThreadPool(threadPoolSize, threadFactory);
101 public Distributor getDistributor(final AxArtifactKey key) {
103 return new DistributorFactory().getDistributor(key);
104 } catch (ContextException e) {
105 throw new ContextRuntimeException("Unable to create Distributor", e);
113 public Distributor getDistributor() {
114 final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor", "0.0.1");
115 return getDistributor(distributorKey);
122 public ContextAlbum getContextAlbum(final Distributor distributor) {
123 return getContextAlbum(distributor, Constants.L_TYPE_CONTEXT_ALBUM, Constants.getAxArtifactKeyArray());
131 public ContextAlbum getContextAlbum(final Distributor distributor, final AxArtifactKey axContextAlbumKey,
132 final AxArtifactKey[] artifactKeys) {
133 final AxContextModel axContextModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
135 distributor.registerModel(axContextModel);
136 final ContextAlbum contextAlbum = distributor.createContextAlbum(axContextAlbumKey);
137 Assertions.argumentNotNull(contextAlbum, "ContextAlbum should not be null");
138 contextAlbum.setUserArtifactStack(artifactKeys);
140 } catch (ContextException e) {
141 throw new ContextRuntimeException("Unable to create ContextAlbum", e);
149 public Map<String, Object> getContextAlbumInitValues() {
150 final Map<String, Object> values = new HashMap<>();
151 for (int i = 0; i < albumSize; i++) {
152 values.put(Integer.toString(i), new TestContextLongItem(0L));
161 public AlbumModifier getAlbumModifier() {
162 return lockType.getAlbumModifier();
166 * Gets the thread factory name.
168 * @param jvmCount the jvm count
169 * @param testType the test type
170 * @return the thread factory name
172 private String getThreadFactoryName(final int jvmCount, final String testType) {
173 return jvmCount == 1 ? testType + ":TestConcurrentContextThread_0_"
174 : testType + ":TestConcurrentContextJVMThread_";