7a92c004056b46a7182eebf43d0043f4c230b632
[ccsdk/cds.git] /
1 /*
2  * Copyright © 2019 Bell Canada
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 package org.onap.ccsdk.cds.sdclistener.handler;
17
18 import com.google.protobuf.ByteString;
19 import io.grpc.ManagedChannel;
20 import io.grpc.inprocess.InProcessChannelBuilder;
21 import io.grpc.inprocess.InProcessServerBuilder;
22 import io.grpc.stub.StreamObserver;
23 import io.grpc.testing.GrpcCleanupRule;
24 import org.apache.commons.io.FileUtils;
25 import org.junit.Before;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.onap.ccsdk.cds.controllerblueprints.common.api.ActionIdentifiers;
30 import org.onap.ccsdk.cds.controllerblueprints.common.api.CommonHeader;
31 import org.onap.ccsdk.cds.controllerblueprints.common.api.Status;
32 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintManagementOutput;
33 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintManagementServiceGrpc.BluePrintManagementServiceImplBase;
34 import org.onap.ccsdk.cds.controllerblueprints.management.api.BluePrintUploadInput;
35 import org.onap.ccsdk.cds.controllerblueprints.management.api.FileChunk;
36 import org.onap.ccsdk.cds.controllerblueprints.management.api.UploadAction;
37 import org.onap.ccsdk.cds.sdclistener.client.SdcListenerAuthClientInterceptor;
38 import org.springframework.beans.factory.annotation.Autowired;
39 import org.springframework.boot.context.properties.EnableConfigurationProperties;
40 import org.springframework.boot.test.context.SpringBootTest;
41 import org.springframework.test.context.junit4.SpringRunner;
42
43 import java.io.File;
44 import java.io.IOException;
45 import java.nio.file.Paths;
46 import java.util.UUID;
47
48 import static org.junit.Assert.assertEquals;
49 import static org.junit.Assert.assertTrue;
50
51 @RunWith(SpringRunner.class)
52 @EnableConfigurationProperties({BluePrintProcesssorHandler.class, SdcListenerAuthClientInterceptor.class})
53 @SpringBootTest(classes = {BluePrintProcessorHandlerTest.class})
54 public class BluePrintProcessorHandlerTest {
55
56     @Autowired
57     private BluePrintProcesssorHandler bluePrintProcesssorHandler;
58
59     @Autowired
60     private SdcListenerAuthClientInterceptor sdcListenerAuthClientInterceptor;
61
62     @Rule
63     public GrpcCleanupRule grpcCleanup = new GrpcCleanupRule();
64
65     private static final String CBA_ARCHIVE = "src/test/resources/testcba.zip";
66     private static final String SUCCESS_MSG = "Successfully uploaded CBA";
67     private static final int SUCCESS_CODE = 200;
68     private ManagedChannel channel;
69
70     @Before
71     public void setUp() throws IOException {
72         final BluePrintManagementServiceImplBase serviceImplBase = new BluePrintManagementServiceImplBase() {
73             @Override
74             public void uploadBlueprint(BluePrintUploadInput request,
75                                         StreamObserver<BluePrintManagementOutput> responseObserver) {
76                 responseObserver.onNext(getBluePrintManagementOutput());
77                 responseObserver.onCompleted();
78             }
79         };
80
81         // Generate server name.
82         String serverName = InProcessServerBuilder.generateName();
83
84         // Create a server, add service, start, and register.
85         grpcCleanup.register(
86                 InProcessServerBuilder.forName(serverName).addService(serviceImplBase).directExecutor().build().start());
87
88         // Create a client channel.
89         channel = grpcCleanup.register(InProcessChannelBuilder.forName(serverName).directExecutor().build());
90     }
91
92     @Test
93     public void testApplicationEndPointSucess() throws IOException {
94         // Arrange
95         BluePrintUploadInput request = generateRequest();
96
97         // Act
98         Status output = bluePrintProcesssorHandler.sendRequest(request, channel);
99
100         // Verify
101         assertEquals(SUCCESS_CODE, output.getCode());
102         assertTrue(output.getMessage().contains(SUCCESS_MSG));
103     }
104
105     private BluePrintUploadInput generateRequest() throws IOException {
106         File file = Paths.get(CBA_ARCHIVE).toFile();
107         byte[] bytes = FileUtils.readFileToByteArray(file);
108         FileChunk fileChunk = FileChunk.newBuilder().setChunk(ByteString.copyFrom(bytes)).build();
109
110
111         return BluePrintUploadInput.newBuilder()
112                 .setCommonHeader(CommonHeader.newBuilder()
113                         .setRequestId(UUID.randomUUID().toString())
114                         .setSubRequestId(UUID.randomUUID().toString())
115                         .setOriginatorId("SDC-LISTENER")
116                         .build())
117                 .setActionIdentifiers(ActionIdentifiers.newBuilder()
118                         .setActionName(UploadAction.PUBLISH.toString()).build())
119                 .setFileChunk(fileChunk).build();
120     }
121
122     private BluePrintManagementOutput getBluePrintManagementOutput() {
123         return BluePrintManagementOutput.newBuilder()
124                 .setStatus(Status.newBuilder().setMessage(SUCCESS_MSG).setCode(200).build())
125                 .build();
126     }
127 }