Fix checkstyle warnings in context-test-utils
[policy/apex-pdp.git] / context / context-test-utils / src / main / java / org / onap / policy / apex / context / test / utils / ConfigrationProviderImpl.java
1 /*-
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.context.test.utils;
22
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.concurrent.ExecutorService;
26 import java.util.concurrent.Executors;
27
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;
40
41 public class ConfigrationProviderImpl implements ConfigrationProvider {
42
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;
49
50     public ConfigrationProviderImpl(final String testType, final int jvmCount, final int threadCount,
51             final int loopSize, final int albumSize, final int lockType) {
52         this.testType = testType;
53         this.jvmCount = jvmCount;
54         this.threadCount = threadCount;
55         this.loopSize = loopSize;
56         this.albumSize = albumSize;
57         this.lockType = LockType.getLockType(lockType);
58     }
59
60     @Override
61     public String getTestName() {
62         return testType;
63     }
64
65     @Override
66     public int getLoopSize() {
67         return loopSize;
68     }
69
70     @Override
71     public int getThreadCount() {
72         return threadCount;
73     }
74
75     @Override
76     public int getJvmCount() {
77         return jvmCount;
78     }
79
80     @Override
81     public int getAlbumSize() {
82         return albumSize;
83     }
84
85     @Override
86     public ExecutorService getExecutorService() {
87         final String name = getThreadFactoryName(jvmCount, testType);
88         final IntegrationThreadFactory threadFactory = new IntegrationThreadFactory(name);
89         final ExecutorService executorService = Executors.newFixedThreadPool(threadCount, threadFactory);
90         return executorService;
91     }
92
93     @Override
94     public ExecutorService getExecutorService(final String threadFactoryName, final int threadPoolSize) {
95         final IntegrationThreadFactory threadFactory = new IntegrationThreadFactory(threadFactoryName);
96         final ExecutorService executorService = Executors.newFixedThreadPool(threadPoolSize, threadFactory);
97         return executorService;
98     }
99
100     @Override
101     public Distributor getDistributor(final AxArtifactKey key) {
102         try {
103             return new DistributorFactory().getDistributor(key);
104         } catch (ContextException e) {
105             throw new ContextRuntimeException("Unable to create Distributor", e);
106         }
107     }
108
109     @Override
110     public Distributor getDistributor() {
111         final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor", "0.0.1");
112         return getDistributor(distributorKey);
113     }
114
115     @Override
116     public ContextAlbum getContextAlbum(final Distributor distributor) {
117         return getContextAlbum(distributor, Constants.L_TYPE_CONTEXT_ALBUM, Constants.getAxArtifactKeyArray());
118     }
119
120     @Override
121     public ContextAlbum getContextAlbum(final Distributor distributor, final AxArtifactKey axContextAlbumKey,
122             final AxArtifactKey[] artifactKeys) {
123         final AxContextModel axContextModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
124         try {
125             distributor.registerModel(axContextModel);
126             final ContextAlbum contextAlbum = distributor.createContextAlbum(axContextAlbumKey);
127             Assertions.argumentNotNull(contextAlbum, "ContextAlbum should not be null");
128             contextAlbum.setUserArtifactStack(artifactKeys);
129             return contextAlbum;
130         } catch (ContextException e) {
131             throw new ContextRuntimeException("Unable to create ContextAlbum", e);
132         }
133     }
134
135     @Override
136     public Map<String, Object> getContextAlbumInitValues() {
137         final Map<String, Object> values = new HashMap<>();
138         for (int i = 0; i < albumSize; i++) {
139             values.put(Integer.toString(i), new TestContextLongItem(0L));
140         }
141         return values;
142     }
143
144     @Override
145     public AlbumModifier getAlbumModifier() {
146         return lockType.getAlbumModifier();
147     }
148
149     @Override
150     public LockType getLockType() {
151         return lockType;
152     }
153
154
155     private String getThreadFactoryName(final int jvmCount, final String testType) {
156         return jvmCount == 1 ? testType + ":TestConcurrentContextThread_0_"
157                 : testType + ":TestConcurrentContextJVMThread_";
158     }
159
160     @Override
161     public String toString() {
162         return "ConfigrationProviderImpl [testType=" + testType + ", jvmCount=" + jvmCount + ", threadCount="
163                 + threadCount + ", loopSize=" + loopSize + ", albumSize=" + albumSize + ", lockType=" + lockType + "]";
164     }
165
166
167 }