2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.context.test.utils;
23 import java.util.HashMap;
25 import java.util.concurrent.ExecutorService;
26 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.context.test.factory.TestContextAlbumFactory;
35 import org.onap.policy.apex.context.test.lock.modifier.AlbumModifier;
36 import org.onap.policy.apex.context.test.lock.modifier.LockType;
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.model.utilities.Assertions;
41 public class ConfigrationProviderImpl implements ConfigrationProvider {
43 private final String testType;
44 private final int jvmCount;
45 private final int threadCount;
46 private final int loopSize;
47 private final int albumSize;
48 private final LockType lockType;
51 * The parameterized ConfigrationProviderImpl constructor.
52 * @param testType the test type
53 * @param jvmCount the JVM count
54 * @param threadCount the thread count
55 * @param loopSize the size of loop
56 * @param albumSize the size of album
57 * @param lockType the lock type
59 public ConfigrationProviderImpl(final String testType, final int jvmCount, final int threadCount,
60 final int loopSize, final int albumSize, final int lockType) {
61 this.testType = testType;
62 this.jvmCount = jvmCount;
63 this.threadCount = threadCount;
64 this.loopSize = loopSize;
65 this.albumSize = albumSize;
66 this.lockType = LockType.getLockType(lockType);
70 public String getTestName() {
75 public int getLoopSize() {
80 public int getThreadCount() {
85 public int getJvmCount() {
90 public int getAlbumSize() {
95 public ExecutorService getExecutorService() {
96 final String name = getThreadFactoryName(jvmCount, testType);
97 final IntegrationThreadFactory threadFactory = new IntegrationThreadFactory(name);
98 final ExecutorService executorService = Executors.newFixedThreadPool(threadCount, threadFactory);
99 return executorService;
103 public ExecutorService getExecutorService(final String threadFactoryName, final int threadPoolSize) {
104 final IntegrationThreadFactory threadFactory = new IntegrationThreadFactory(threadFactoryName);
105 final ExecutorService executorService = Executors.newFixedThreadPool(threadPoolSize, threadFactory);
106 return executorService;
110 public Distributor getDistributor(final AxArtifactKey key) {
112 return new DistributorFactory().getDistributor(key);
113 } catch (ContextException e) {
114 throw new ContextRuntimeException("Unable to create Distributor", e);
119 public Distributor getDistributor() {
120 final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor", "0.0.1");
121 return getDistributor(distributorKey);
125 public ContextAlbum getContextAlbum(final Distributor distributor) {
126 return getContextAlbum(distributor, Constants.L_TYPE_CONTEXT_ALBUM, Constants.getAxArtifactKeyArray());
130 public ContextAlbum getContextAlbum(final Distributor distributor, final AxArtifactKey axContextAlbumKey,
131 final AxArtifactKey[] artifactKeys) {
132 final AxContextModel axContextModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
134 distributor.registerModel(axContextModel);
135 final ContextAlbum contextAlbum = distributor.createContextAlbum(axContextAlbumKey);
136 Assertions.argumentNotNull(contextAlbum, "ContextAlbum should not be null");
137 contextAlbum.setUserArtifactStack(artifactKeys);
139 } catch (ContextException e) {
140 throw new ContextRuntimeException("Unable to create ContextAlbum", e);
145 public Map<String, Object> getContextAlbumInitValues() {
146 final Map<String, Object> values = new HashMap<>();
147 for (int i = 0; i < albumSize; i++) {
148 values.put(Integer.toString(i), new TestContextLongItem(0L));
154 public AlbumModifier getAlbumModifier() {
155 return lockType.getAlbumModifier();
159 public LockType getLockType() {
164 private String getThreadFactoryName(final int jvmCount, final String testType) {
165 return jvmCount == 1 ? testType + ":TestConcurrentContextThread_0_"
166 : testType + ":TestConcurrentContextJVMThread_";
170 public String toString() {
171 return "ConfigrationProviderImpl [testType=" + testType + ", jvmCount=" + jvmCount + ", threadCount="
172 + threadCount + ", loopSize=" + loopSize + ", albumSize=" + albumSize + ", lockType=" + lockType + "]";