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;
28 import org.onap.policy.apex.context.ContextAlbum;
29 import org.onap.policy.apex.context.ContextException;
30 import org.onap.policy.apex.context.ContextRuntimeException;
31 import org.onap.policy.apex.context.Distributor;
32 import org.onap.policy.apex.context.impl.distribution.DistributorFactory;
33 import org.onap.policy.apex.context.test.concepts.TestContextLongItem;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
35 import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel;
36 import org.onap.policy.apex.testsuites.integration.context.factory.TestContextAlbumFactory;
37 import org.onap.policy.apex.testsuites.integration.context.lock.modifier.AlbumModifier;
38 import org.onap.policy.apex.testsuites.integration.context.lock.modifier.LockType;
39 import org.onap.policy.common.utils.validation.Assertions;
42 * The Class ConfigrationProviderImpl provides configuration information for a context test back to the caller.
44 public class ConfigrationProviderImpl implements ConfigrationProvider {
46 private final String testType;
47 private final int jvmCount;
48 private final int threadCount;
49 private final int loopSize;
50 private final int albumSize;
51 private final LockType lockType;
54 * The parameterized ConfigrationProviderImpl constructor.
56 * @param testType the test type
57 * @param jvmCount the JVM count
58 * @param threadCount the thread count
59 * @param loopSize the size of loop
60 * @param albumSize the size of album
61 * @param lockType the lock type
63 public ConfigrationProviderImpl(final String testType, final int jvmCount, final int threadCount,
64 final int loopSize, final int albumSize, final int lockType) {
65 this.testType = testType;
66 this.jvmCount = jvmCount;
67 this.threadCount = threadCount;
68 this.loopSize = loopSize;
69 this.albumSize = albumSize;
70 this.lockType = LockType.getLockType(lockType);
77 public String getTestName() {
85 public int getLoopSize() {
93 public int getThreadCount() {
101 public int getJvmCount() {
109 public int getAlbumSize() {
117 public ExecutorService getExecutorService() {
118 final String name = getThreadFactoryName(jvmCount, testType);
119 final IntegrationThreadFactory threadFactory = new IntegrationThreadFactory(name);
120 return Executors.newFixedThreadPool(threadCount, threadFactory);
127 public ExecutorService getExecutorService(final String threadFactoryName, final int threadPoolSize) {
128 final IntegrationThreadFactory threadFactory = new IntegrationThreadFactory(threadFactoryName);
129 return Executors.newFixedThreadPool(threadPoolSize, threadFactory);
136 public Distributor getDistributor(final AxArtifactKey key) {
138 return new DistributorFactory().getDistributor(key);
139 } catch (ContextException e) {
140 throw new ContextRuntimeException("Unable to create Distributor", e);
148 public Distributor getDistributor() {
149 final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor", "0.0.1");
150 return getDistributor(distributorKey);
157 public ContextAlbum getContextAlbum(final Distributor distributor) {
158 return getContextAlbum(distributor, Constants.L_TYPE_CONTEXT_ALBUM, Constants.getAxArtifactKeyArray());
166 public ContextAlbum getContextAlbum(final Distributor distributor, final AxArtifactKey axContextAlbumKey,
167 final AxArtifactKey[] artifactKeys) {
168 final AxContextModel axContextModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
170 distributor.registerModel(axContextModel);
171 final ContextAlbum contextAlbum = distributor.createContextAlbum(axContextAlbumKey);
172 Assertions.argumentNotNull(contextAlbum, "ContextAlbum should not be null");
173 contextAlbum.setUserArtifactStack(artifactKeys);
175 } catch (ContextException e) {
176 throw new ContextRuntimeException("Unable to create ContextAlbum", e);
184 public Map<String, Object> getContextAlbumInitValues() {
185 final Map<String, Object> values = new HashMap<>();
186 for (int i = 0; i < albumSize; i++) {
187 values.put(Integer.toString(i), new TestContextLongItem(0L));
196 public AlbumModifier getAlbumModifier() {
197 return lockType.getAlbumModifier();
204 public LockType getLockType() {
209 * Gets the thread factory name.
211 * @param jvmCount the jvm count
212 * @param testType the test type
213 * @return the thread factory name
215 private String getThreadFactoryName(final int jvmCount, final String testType) {
216 return jvmCount == 1 ? testType + ":TestConcurrentContextThread_0_"
217 : testType + ":TestConcurrentContextJVMThread_";
224 public String toString() {
225 return "ConfigrationProviderImpl [testType=" + testType + ", jvmCount=" + jvmCount + ", threadCount="
226 + threadCount + ", loopSize=" + loopSize + ", albumSize=" + albumSize + ", lockType=" + lockType