unit tests - openecomp-be
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / unique-type-rest / unique-type-rest-services / src / main / java / org / openecomp / sdcrests / uniquevalue / rest / services / UniqueTypesImpl.java
1 /*
2  * Copyright © 2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  * ================================================================================
16  * Modifications copyright (c) 2019 Nokia
17  * ================================================================================
18  */
19 package org.openecomp.sdcrests.uniquevalue.rest.services;
20
21 import com.google.common.annotations.VisibleForTesting;
22 import org.openecomp.core.dao.UniqueValueDaoFactory;
23 import org.openecomp.core.util.UniqueValueUtil;
24 import org.openecomp.sdc.common.errors.ErrorCategory;
25 import org.openecomp.sdc.common.errors.ErrorCode;
26 import org.openecomp.sdc.common.errors.ErrorCodeAndMessage;
27 import org.openecomp.sdcrests.uniquevalue.rest.UniqueTypes;
28 import org.openecomp.sdcrests.uniquevalue.types.UniqueTypesProvider;
29 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
30 import org.springframework.context.annotation.Scope;
31 import org.springframework.stereotype.Service;
32
33 import javax.inject.Named;
34 import javax.ws.rs.core.Response;
35
36 import java.util.*;
37
38 import static javax.ws.rs.core.Response.Status.NOT_FOUND;
39
40 @Named
41 @Service("uniqueTypes")
42 @Scope(value = "prototype")
43 public class UniqueTypesImpl implements UniqueTypes {
44
45   private static final String UNIQUE_TYPE_NOT_FOUND_ERR_ID = "UNIQUE_TYPE_NOT_FOUND";
46   private static final String UNIQUE_TYPE_NOT_FOUND_MSG = "%s is not a supported unique type.";
47
48   private static final Map<String, String> UNIQUE_TYPE_TO_INTERNAL;
49   private UniqueValueUtil uniqueValueUtil;
50
51   static {
52     Map<String, String> uniqueTypes = new HashMap<>();
53     ServiceLoader.load(UniqueTypesProvider.class)
54         .forEach(typesProvider -> uniqueTypes.putAll(typesProvider.listUniqueTypes()));
55     UNIQUE_TYPE_TO_INTERNAL = Collections.unmodifiableMap(uniqueTypes);
56   }
57
58
59   @Override
60   public Response listUniqueTypes(String user) {
61     return Response.ok(
62         new GenericCollectionWrapper<>(new ArrayList<>(UNIQUE_TYPE_TO_INTERNAL.keySet())))
63         .build();
64   }
65
66   @Override
67   public Response getUniqueValue(String type, String value, String user) {
68     String internalType = UNIQUE_TYPE_TO_INTERNAL.get(type);
69
70     if (internalType == null) {
71       ErrorCode error = new ErrorCode.ErrorCodeBuilder()
72           .withCategory(ErrorCategory.APPLICATION)
73           .withId(UNIQUE_TYPE_NOT_FOUND_ERR_ID)
74           .withMessage(String.format(UNIQUE_TYPE_NOT_FOUND_MSG, type)).build();
75       return Response.status(NOT_FOUND).entity(new ErrorCodeAndMessage(NOT_FOUND, error)).build();
76     }
77
78     return Response.ok(Collections
79         .singletonMap("occupied", getUniqueValueUtil().isUniqueValueOccupied(internalType, value)))
80         .build();
81   }
82
83   @VisibleForTesting
84   void setUniqueValueUtil(UniqueValueUtil uniqueValueUtil) {
85     this.uniqueValueUtil = uniqueValueUtil;
86   }
87
88   private UniqueValueUtil getUniqueValueUtil() {
89     if (uniqueValueUtil == null){
90       uniqueValueUtil = new UniqueValueUtil(UniqueValueDaoFactory.getInstance().createInterface());
91     }
92     return uniqueValueUtil;
93   }
94 }