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