Changes for checkstyle 8.32
[policy/apex-pdp.git] / testsuites / integration / integration-context-test / src / test / java / org / onap / policy / apex / testsuites / integration / context / utils / ConfigrationProviderImpl.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.testsuites.integration.context.utils;
23
24 import java.util.HashMap;
25 import java.util.Map;
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;
40
41 /**
42  * The Class ConfigrationProviderImpl provides configuration information for a context test back to the caller.
43  */
44 public class ConfigrationProviderImpl implements ConfigrationProvider {
45
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;
52
53     /**
54      * The parameterized ConfigrationProviderImpl constructor.
55      *
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
62      */
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);
71     }
72
73     /**
74      * {@inheritDoc}.
75      */
76     @Override
77     public String getTestName() {
78         return testType;
79     }
80
81     /**
82      * {@inheritDoc}.
83      */
84     @Override
85     public int getLoopSize() {
86         return loopSize;
87     }
88
89     /**
90      * {@inheritDoc}.
91      */
92     @Override
93     public int getThreadCount() {
94         return threadCount;
95     }
96
97     /**
98      * {@inheritDoc}.
99      */
100     @Override
101     public int getJvmCount() {
102         return jvmCount;
103     }
104
105     /**
106      * {@inheritDoc}.
107      */
108     @Override
109     public int getAlbumSize() {
110         return albumSize;
111     }
112
113     /**
114      * {@inheritDoc}.
115      */
116     @Override
117     public ExecutorService getExecutorService() {
118         final String name = getThreadFactoryName(jvmCount, testType);
119         final IntegrationThreadFactory threadFactory = new IntegrationThreadFactory(name);
120         return Executors.newFixedThreadPool(threadCount, threadFactory);
121     }
122
123     /**
124      * {@inheritDoc}.
125      */
126     @Override
127     public ExecutorService getExecutorService(final String threadFactoryName, final int threadPoolSize) {
128         final IntegrationThreadFactory threadFactory = new IntegrationThreadFactory(threadFactoryName);
129         return Executors.newFixedThreadPool(threadPoolSize, threadFactory);
130     }
131
132     /**
133      * {@inheritDoc}.
134      */
135     @Override
136     public Distributor getDistributor(final AxArtifactKey key) {
137         try {
138             return new DistributorFactory().getDistributor(key);
139         } catch (ContextException e) {
140             throw new ContextRuntimeException("Unable to create Distributor", e);
141         }
142     }
143
144     /**
145      * {@inheritDoc}.
146      */
147     @Override
148     public Distributor getDistributor() {
149         final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor", "0.0.1");
150         return getDistributor(distributorKey);
151     }
152
153     /**
154      * {@inheritDoc}.
155      */
156     @Override
157     public ContextAlbum getContextAlbum(final Distributor distributor) {
158         return getContextAlbum(distributor, Constants.L_TYPE_CONTEXT_ALBUM, Constants.getAxArtifactKeyArray());
159     }
160
161     /**
162      * {@inheritDoc}.
163      *[])
164      */
165     @Override
166     public ContextAlbum getContextAlbum(final Distributor distributor, final AxArtifactKey axContextAlbumKey,
167                     final AxArtifactKey[] artifactKeys) {
168         final AxContextModel axContextModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
169         try {
170             distributor.registerModel(axContextModel);
171             final ContextAlbum contextAlbum = distributor.createContextAlbum(axContextAlbumKey);
172             Assertions.argumentNotNull(contextAlbum, "ContextAlbum should not be null");
173             contextAlbum.setUserArtifactStack(artifactKeys);
174             return contextAlbum;
175         } catch (ContextException e) {
176             throw new ContextRuntimeException("Unable to create ContextAlbum", e);
177         }
178     }
179
180     /**
181      * {@inheritDoc}.
182      */
183     @Override
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));
188         }
189         return values;
190     }
191
192     /**
193      * {@inheritDoc}.
194      */
195     @Override
196     public AlbumModifier getAlbumModifier() {
197         return lockType.getAlbumModifier();
198     }
199
200     /**
201      * {@inheritDoc}.
202      */
203     @Override
204     public LockType getLockType() {
205         return lockType;
206     }
207
208     /**
209      * Gets the thread factory name.
210      *
211      * @param jvmCount the jvm count
212      * @param testType the test type
213      * @return the thread factory name
214      */
215     private String getThreadFactoryName(final int jvmCount, final String testType) {
216         return jvmCount == 1 ? testType + ":TestConcurrentContextThread_0_"
217                         : testType + ":TestConcurrentContextJVMThread_";
218     }
219
220     /**
221      * {@inheritDoc}.
222      */
223     @Override
224     public String toString() {
225         return "ConfigrationProviderImpl [testType=" + testType + ", jvmCount=" + jvmCount + ", threadCount="
226                         + threadCount + ", loopSize=" + loopSize + ", albumSize=" + albumSize + ", lockType=" + lockType
227                         + "]";
228     }
229
230 }