2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.testsuites.integration.context.utils;
24 import java.util.HashMap;
26 import java.util.concurrent.ExecutorService;
27 import java.util.concurrent.Executors;
29 import org.onap.policy.apex.context.ContextAlbum;
30 import org.onap.policy.apex.context.ContextException;
31 import org.onap.policy.apex.context.ContextRuntimeException;
32 import org.onap.policy.apex.context.Distributor;
33 import org.onap.policy.apex.context.impl.distribution.DistributorFactory;
34 import org.onap.policy.apex.context.test.concepts.TestContextLongItem;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
36 import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel;
37 import org.onap.policy.apex.testsuites.integration.context.factory.TestContextAlbumFactory;
38 import org.onap.policy.apex.testsuites.integration.context.lock.modifier.AlbumModifier;
39 import org.onap.policy.apex.testsuites.integration.context.lock.modifier.LockType;
40 import org.onap.policy.common.utils.validation.Assertions;
43 * The Class ConfigrationProviderImpl provides configuration information for a context test back to the caller.
45 public class ConfigrationProviderImpl implements ConfigrationProvider {
47 private final String testType;
48 private final int jvmCount;
49 private final int threadCount;
50 private final int loopSize;
51 private final int albumSize;
52 private final LockType lockType;
55 * The parameterized ConfigrationProviderImpl constructor.
57 * @param testType the test type
58 * @param jvmCount the JVM count
59 * @param threadCount the thread count
60 * @param loopSize the size of loop
61 * @param albumSize the size of album
62 * @param lockType the lock type
64 public ConfigrationProviderImpl(final String testType, final int jvmCount, final int threadCount,
65 final int loopSize, final int albumSize, final int lockType) {
66 this.testType = testType;
67 this.jvmCount = jvmCount;
68 this.threadCount = threadCount;
69 this.loopSize = loopSize;
70 this.albumSize = albumSize;
71 this.lockType = LockType.getLockType(lockType);
78 public String getTestName() {
86 public int getLoopSize() {
94 public int getThreadCount() {
102 public int getJvmCount() {
110 public int getAlbumSize() {
118 public ExecutorService getExecutorService() {
119 final String name = getThreadFactoryName(jvmCount, testType);
120 final IntegrationThreadFactory threadFactory = new IntegrationThreadFactory(name);
121 return Executors.newFixedThreadPool(threadCount, threadFactory);
128 public ExecutorService getExecutorService(final String threadFactoryName, final int threadPoolSize) {
129 final IntegrationThreadFactory threadFactory = new IntegrationThreadFactory(threadFactoryName);
130 return Executors.newFixedThreadPool(threadPoolSize, threadFactory);
137 public Distributor getDistributor(final AxArtifactKey key) {
139 return new DistributorFactory().getDistributor(key);
140 } catch (ContextException e) {
141 throw new ContextRuntimeException("Unable to create Distributor", e);
149 public Distributor getDistributor() {
150 final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor", "0.0.1");
151 return getDistributor(distributorKey);
158 public ContextAlbum getContextAlbum(final Distributor distributor) {
159 return getContextAlbum(distributor, Constants.L_TYPE_CONTEXT_ALBUM, Constants.getAxArtifactKeyArray());
167 public ContextAlbum getContextAlbum(final Distributor distributor, final AxArtifactKey axContextAlbumKey,
168 final AxArtifactKey[] artifactKeys) {
169 final AxContextModel axContextModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
171 distributor.registerModel(axContextModel);
172 final ContextAlbum contextAlbum = distributor.createContextAlbum(axContextAlbumKey);
173 Assertions.argumentNotNull(contextAlbum, "ContextAlbum should not be null");
174 contextAlbum.setUserArtifactStack(artifactKeys);
176 } catch (ContextException e) {
177 throw new ContextRuntimeException("Unable to create ContextAlbum", e);
185 public Map<String, Object> getContextAlbumInitValues() {
186 final Map<String, Object> values = new HashMap<>();
187 for (int i = 0; i < albumSize; i++) {
188 values.put(Integer.toString(i), new TestContextLongItem(0L));
197 public AlbumModifier getAlbumModifier() {
198 return lockType.getAlbumModifier();
205 public LockType getLockType() {
210 * Gets the thread factory name.
212 * @param jvmCount the jvm count
213 * @param testType the test type
214 * @return the thread factory name
216 private String getThreadFactoryName(final int jvmCount, final String testType) {
217 return jvmCount == 1 ? testType + ":TestConcurrentContextThread_0_"
218 : testType + ":TestConcurrentContextJVMThread_";
225 public String toString() {
226 return "ConfigrationProviderImpl [testType=" + testType + ", jvmCount=" + jvmCount + ", threadCount="
227 + threadCount + ", loopSize=" + loopSize + ", albumSize=" + albumSize + ", lockType=" + lockType